musli_value/
type_hint.rs

1use core::fmt;
2
3#[cfg(feature = "alloc")]
4use musli::de::SizeHint;
5
6/// A type hint.
7#[derive(Debug, Clone, Copy)]
8#[non_exhaustive]
9pub(crate) enum TypeHint {
10    /// A unit type or an empty value.
11    Unit,
12    /// A boolean type.
13    Bool,
14    /// A character type.
15    Char,
16    /// The type as a number.
17    Number(NumberHint),
18    /// A byte array.
19    #[cfg(feature = "alloc")]
20    Bytes(SizeHint),
21    /// A string with the given length.
22    #[cfg(feature = "alloc")]
23    String(SizeHint),
24    /// A sequence with a length hint.
25    #[cfg(feature = "alloc")]
26    Sequence(SizeHint),
27    /// A map with a length hint.
28    #[cfg(feature = "alloc")]
29    Map(SizeHint),
30    /// A variant.
31    #[cfg(feature = "alloc")]
32    Variant,
33    /// An optional value.
34    #[cfg(feature = "alloc")]
35    Option,
36}
37
38impl fmt::Display for TypeHint {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            TypeHint::Unit => write!(f, "unit"),
42            TypeHint::Bool => write!(f, "bool"),
43            TypeHint::Char => write!(f, "char"),
44            TypeHint::Number(number) => number.fmt(f),
45            #[cfg(feature = "alloc")]
46            TypeHint::Bytes(size) => write!(f, "bytes with {size}"),
47            #[cfg(feature = "alloc")]
48            TypeHint::String(size) => write!(f, "string with {size}"),
49            #[cfg(feature = "alloc")]
50            TypeHint::Sequence(size) => write!(f, "sequence with {size}"),
51            #[cfg(feature = "alloc")]
52            TypeHint::Map(size) => write!(f, "map with {size}"),
53            #[cfg(feature = "alloc")]
54            TypeHint::Variant => write!(f, "variant"),
55            #[cfg(feature = "alloc")]
56            TypeHint::Option => write!(f, "option"),
57        }
58    }
59}
60
61/// A number hint.
62#[derive(Debug, Clone, Copy)]
63#[non_exhaustive]
64pub(crate) enum NumberHint {
65    /// An unsigned 8-bit integer.
66    U8,
67    /// An unsigned 16-bit integer.
68    U16,
69    /// An unsigned 32-bit integer.
70    U32,
71    /// An unsigned 64-bit integer.
72    U64,
73    /// An unsigned 128-bit integer.
74    U128,
75    /// A signed 8-bit integer.
76    I8,
77    /// A signed 16-bit integer.
78    I16,
79    /// A signed 32-bit integer.
80    I32,
81    /// A signed 64-bit integer.
82    I64,
83    /// A signed 128-bit integer.
84    I128,
85    /// A [usize]-typed value.
86    Usize,
87    /// A [isize]-typed value.
88    Isize,
89    /// A 32-bit float.
90    F32,
91    /// A 64-bit float.
92    F64,
93}
94
95impl fmt::Display for NumberHint {
96    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97        match self {
98            NumberHint::U8 => write!(f, "u8"),
99            NumberHint::U16 => write!(f, "u16"),
100            NumberHint::U32 => write!(f, "u32"),
101            NumberHint::U64 => write!(f, "u64"),
102            NumberHint::U128 => write!(f, "u128"),
103            NumberHint::I8 => write!(f, "i8"),
104            NumberHint::I16 => write!(f, "i16"),
105            NumberHint::I32 => write!(f, "i32"),
106            NumberHint::I64 => write!(f, "i64"),
107            NumberHint::I128 => write!(f, "i128"),
108            NumberHint::Usize => write!(f, "usize"),
109            NumberHint::Isize => write!(f, "isize"),
110            NumberHint::F32 => write!(f, "f32"),
111            NumberHint::F64 => write!(f, "f64"),
112        }
113    }
114}