blob: 25877fb0316e0b1d2cc3cd11daaf115fb3715d21 [file] [log] [blame]
//! Helper functions for implementing Display for our types.
use crate::valueas::ValueAs;
use coset::cbor::value::Value;
use std::fmt::{self, Formatter};
pub fn write_bytes_in_hex(f: &mut Formatter, bytes: &[u8]) -> Result<(), fmt::Error> {
for b in bytes {
write!(f, "{:02x}", b)?
}
Ok(())
}
pub fn write_value(f: &mut Formatter, value: &Value) -> Result<(), fmt::Error> {
if let Some(bytes) = value.as_bytes() {
write_bytes_in_hex(f, bytes)
} else if let Some(text) = value.as_text() {
write!(f, "\"{}\"", text)
} else if let Ok(integer) = value.as_i64() {
write!(f, "{}", integer)
} else {
write!(f, "{:?}", value)
}
}