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
use crate::{Data, Enum, Error, Expected, Found, FoundData, Number, Struct, Value};
use alloc::boxed::Box;
use alloc::vec::Vec;

/// A convenience wrapper for constructing [crate::Found] and returning an error.
pub trait Unexpected {
    /// Consumes the type and returns an error.
    fn unexpected(self, expected: Expected) -> Error;
}

impl Unexpected for Number {
    fn unexpected(self, expected: Expected) -> Error {
        let found = Found::Number(self);
        Error::unexpected(found, expected)
    }
}

impl Value<'_> {
    fn into_found(self) -> Found {
        match self {
            Value::Unit => Found::Unit,
            Value::Bool(v) => Found::Bool(v),
            Value::Number(v) => Found::Number(v),
            Value::Char(v) => Found::Char(v),
            Value::String(v) => Found::String(v.into_owned()),
            Value::Bytes(v) => Found::Bytes(v.into_owned()),
            Value::Seq(v) => {
                let mut vec = Vec::with_capacity(v.len());
                for value in v {
                    vec.push(value.into_found());
                }
                Found::Seq(vec)
            }
            Value::Map(v) => {
                let mut vec = Vec::with_capacity(v.len());
                for (key, value) in v {
                    vec.push((key.into_found(), value.into_found()));
                }
                Found::Map(vec)
            }
            Value::Option(v) => Found::Option(v.map(|x| Box::new(x.into_found()))),
            Value::Struct(v) => Found::Struct {
                name: v.name.into_owned(),
                data: Box::new(v.data.into_found()),
            },
            Value::Enum(v) => Found::Enum {
                name: v.name.into_owned(),
                variant: v.variant.into_owned(),
                data: Box::new(v.data.into_found()),
            },
            Value::Tuple(v) => {
                let mut vec = Vec::with_capacity(v.len());
                for value in v {
                    vec.push(value.into_found());
                }
                Found::Tuple(vec)
            }
        }
    }
}

impl Data<'_> {
    fn into_found(self) -> FoundData {
        match self {
            Data::Unit => FoundData::Unit,
            Data::NewType { value } => FoundData::NewType(value.into_found()),
            Data::Tuple { values } => {
                FoundData::Tuple(values.into_iter().map(Value::into_found).collect())
            }
            Data::Struct { fields } => FoundData::Struct(
                fields
                    .into_iter()
                    .map(|(k, v)| (k.into_owned(), v.into_found()))
                    .collect(),
            ),
        }
    }
}

impl Unexpected for Value<'_> {
    fn unexpected(self, expected: Expected) -> Error {
        let found = self.into_found();
        Error::unexpected(found, expected)
    }
}

impl Unexpected for Box<Struct<'_>> {
    fn unexpected(self, expected: Expected) -> Error {
        let found = Found::Struct {
            name: self.name.into_owned(),
            data: Box::new(self.data.into_found()),
        };
        Error::unexpected(found, expected)
    }
}

impl Unexpected for Box<Enum<'_>> {
    fn unexpected(self, expected: Expected) -> Error {
        let found = Found::Enum {
            name: self.name.into_owned(),
            variant: self.variant.into_owned(),
            data: Box::new(self.data.into_found()),
        };
        Error::unexpected(found, expected)
    }
}