musli_core/
expecting.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! Internal helpers for generating nice expectation messages.

use core::fmt::{self, Display};

use crate::de::SizeHint;

pub trait Expecting {
    /// Generated the actual message of what we expected.
    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;

    /// Return a type that can be formatted from `self`.
    #[doc(hidden)]
    fn format(&self) -> &dyn Expecting
    where
        Self: Sized,
    {
        self
    }
}

impl Expecting for str {
    #[inline]
    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.fmt(f)
    }
}

impl fmt::Display for &dyn Expecting {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.expecting(f)
    }
}

struct FormatFn<T>(T);

impl<T> fmt::Display for FormatFn<T>
where
    T: Fn(&mut fmt::Formatter<'_>) -> fmt::Result,
{
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        (self.0)(f)
    }
}

#[inline]
fn format_fn<T>(function: T) -> FormatFn<T>
where
    T: Fn(&mut fmt::Formatter<'_>) -> fmt::Result,
{
    FormatFn(function)
}

/// Format an invalid type message.
pub(crate) fn unsupported_type<'a>(
    actual: &'a dyn fmt::Display,
    expected: &'a dyn Expecting,
) -> impl fmt::Display + 'a {
    format_fn(move |f| {
        write! {
            f,
            "Got unsupported type `{actual}`, but expected {expected}"
        }
    })
}

/// Format a bad visitor type message.
pub(crate) fn bad_visitor_type<'a>(
    actual: &'a dyn fmt::Display,
    expected: &'a dyn Expecting,
) -> impl fmt::Display + 'a {
    format_fn(move |f| {
        write! {
            f,
            "Bad reference type {actual}, expected {expected}",
        }
    })
}

macro_rules! expect_with {
    ($($vis:vis $ident:ident($string:literal, $ty:ty);)*) => {
        $(
            $vis struct $ident($vis $ty);

            impl fmt::Display for $ident {
                fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                    write!(f, $string, self.0)
                }
            }
        )*
    }
}

macro_rules! expect {
    ($($vis:vis $ident:ident($string:literal);)*) => {
        $(
            $vis struct $ident;

            impl fmt::Display for $ident {
                fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                    write!(f, $string)
                }
            }
        )*
    }
}

expect_with! {
    pub(crate) SequenceWith("sequence with {0}", SizeHint);
    pub(crate) MapWith("map with {0}", SizeHint);
    pub(crate) BytesWith("bytes with {0}", SizeHint);
    pub(crate) StringWith("string with {0}", SizeHint);
}

expect! {
    pub(crate) Any("a dynamic value");
    pub(crate) Empty("empty");
    pub(crate) Option("option");
    pub(crate) Pack("pack");
    pub(crate) Bool("boolean");
    pub(crate) Char("character");
    pub(crate) Number("arbitrary precision number");
    pub(crate) Unsigned8("8-bit unsigned integer");
    pub(crate) Unsigned16("16-bit unsigned integer");
    pub(crate) Unsigned32("32-bit unsigned integer");
    pub(crate) Unsigned64("64-bit unsigned integer");
    pub(crate) Unsigned128("128-bit unsigned integer");
    pub(crate) Signed8("8-bit signed integer");
    pub(crate) Signed16("16-bit signed integer");
    pub(crate) Signed32("32-bit signed integer");
    pub(crate) Signed64("64-bit signed integer");
    pub(crate) Signed128("128-bit signed integer");
    pub(crate) Float32("32-bit float");
    pub(crate) Float64("64-bit float");
    pub(crate) Isize("isize");
    pub(crate) Usize("usize");
    pub(crate) String("string");
    pub(crate) CollectString("collected string");
    pub(crate) Bytes("bytes");
    pub(crate) Array("array");
    pub(crate) Map("map");
    pub(crate) MapEntries("map entries");
    pub(crate) UnsizedMap("unsized map");
    pub(crate) MapVariant("map variant");
    pub(crate) UnsizedSequence("unsized sequence");
    pub(crate) SequenceVariant("sequence variant");
    pub(crate) Variant("variant");
    pub(crate) AnyValue("a value");
}