1use minicbor::{Decode, Decoder};
2
3#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
12pub enum NullAndStringOr<'buffer, T> {
13 Ok(T),
14 Err(&'buffer str),
15}
16
17impl<'buffer, Context, T: Decode<'buffer, Context>> Decode<'buffer, Context>
18 for NullAndStringOr<'buffer, T>
19{
20 fn decode(
21 d: &mut Decoder<'buffer>,
22 context: &mut Context,
23 ) -> Result<Self, minicbor::decode::Error> {
24 let mut p = d.probe();
25 let is_error = if let Some(length) = p.array()? {
26 if length == 2 {
27 p.datatype()? == minicbor::data::Type::Null
28 } else {
29 false
30 }
31 } else {
32 false
33 };
34 if is_error {
35 d.array()?;
36 d.skip()?;
37 Ok(Self::Err(d.str()?))
38 } else {
39 Ok(Self::Ok(T::decode(d, context)?))
40 }
41 }
42}
43
44#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
46pub struct TryFromInt;
47
48impl core::fmt::Display for TryFromInt {
49 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
50 "invalid value".fmt(f)
51 }
52}
53
54#[cfg(feature = "std")]
55impl std::error::Error for TryFromInt {}