#[derive(Debug, PartialEq)]
pub enum OutputData<'a> {
String(&'a str),
SignedInteger(i64),
UnsignedInteger(u64),
Float(f32),
Double(f64),
Byte(u8),
Array(&'a [u8]),
Object(usize),
Null,
}
impl<'a> OutputData<'a> {
#[must_use]
pub fn as_str(&self) -> Option<&'a str> {
if let OutputData::String(s) = self {
Some(s)
} else {
None
}
}
#[must_use]
pub fn as_i64(&self) -> Option<i64> {
if let OutputData::SignedInteger(i) = self {
Some(*i)
} else {
None
}
}
#[must_use]
pub fn as_u64(&self) -> Option<u64> {
if let OutputData::UnsignedInteger(u) = self {
Some(*u)
} else {
None
}
}
#[must_use]
pub fn as_f32(&self) -> Option<f32> {
if let OutputData::Float(f) = self {
Some(*f)
} else {
None
}
}
#[must_use]
pub fn as_f64(&self) -> Option<f64> {
if let OutputData::Double(d) = self {
Some(*d)
} else {
None
}
}
#[must_use]
pub fn as_byte(&self) -> Option<u8> {
if let OutputData::Byte(b) = self {
Some(*b)
} else {
None
}
}
#[must_use]
pub fn as_array(&self) -> Option<&'a [u8]> {
if let OutputData::Array(arr) = self {
Some(arr)
} else {
None
}
}
#[must_use]
pub fn as_object(&self) -> Option<usize> {
if let OutputData::Object(idx) = self {
Some(*idx)
} else {
None
}
}
}
impl core::fmt::Display for OutputData<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
OutputData::String(s) => write!(f, "{s}"),
OutputData::SignedInteger(i) => write!(f, "{i}"),
OutputData::UnsignedInteger(u) => write!(f, "{u}"),
OutputData::Float(fp) => write!(f, "{fp}"),
OutputData::Double(d) => write!(f, "{d}"),
OutputData::Byte(b) => write!(f, "0x{b:02x}"),
OutputData::Array(arr) => write!(f, "[{arr:02x?}]"),
OutputData::Object(idx) => write!(f, "Object({idx})"),
OutputData::Null => write!(f, "Null"),
}
}
}