use minicbor::{Decode, Decoder};
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum NullAndStringOr<'buffer, T> {
Ok(T),
Err(&'buffer str),
}
impl<'buffer, Context, T: Decode<'buffer, Context>> Decode<'buffer, Context>
for NullAndStringOr<'buffer, T>
{
fn decode(
d: &mut Decoder<'buffer>,
context: &mut Context,
) -> Result<Self, minicbor::decode::Error> {
let mut p = d.probe();
let is_error = if let Some(length) = p.array()? {
if length == 2 {
p.datatype()? == minicbor::data::Type::Null
} else {
false
}
} else {
false
};
if is_error {
d.array()?;
d.skip()?;
Ok(Self::Err(d.str()?))
} else {
Ok(Self::Ok(T::decode(d, context)?))
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct TryFromInt;
impl core::fmt::Display for TryFromInt {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
"invalid value".fmt(f)
}
}
#[cfg(feature = "std")]
impl std::error::Error for TryFromInt {}