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
// SPDX-License-Identifier: CC0-1.0

use core::fmt;
use core::marker::PhantomData;
use core::str::{self, FromStr};

use serde::de;

/// A serde visitor that works for `T`s implementing `FromStr`.
pub struct FromStrVisitor<T> {
    expectation: &'static str,
    _pd: PhantomData<T>,
}

impl<T> FromStrVisitor<T> {
    pub fn new(expectation: &'static str) -> Self {
        FromStrVisitor { expectation, _pd: PhantomData }
    }
}

impl<'de, T> de::Visitor<'de> for FromStrVisitor<T>
where
    T: FromStr,
    <T as FromStr>::Err: fmt::Display,
{
    type Value = T;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str(self.expectation)
    }

    fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
        FromStr::from_str(v).map_err(E::custom)
    }
}

pub struct BytesVisitor<F> {
    expectation: &'static str,
    parse_fn: F,
}

impl<F, T, Err> BytesVisitor<F>
where
    F: FnOnce(&[u8]) -> Result<T, Err>,
    Err: fmt::Display,
{
    pub fn new(expectation: &'static str, parse_fn: F) -> Self {
        BytesVisitor { expectation, parse_fn }
    }
}

impl<'de, F, T, Err> de::Visitor<'de> for BytesVisitor<F>
where
    F: FnOnce(&[u8]) -> Result<T, Err>,
    Err: fmt::Display,
{
    type Value = T;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str(self.expectation)
    }

    fn visit_bytes<E: de::Error>(self, v: &[u8]) -> Result<Self::Value, E> {
        (self.parse_fn)(v).map_err(E::custom)
    }
}

macro_rules! impl_tuple_visitor {
    ($thing:ident, $len:expr) => {
        pub(crate) struct $thing<F> {
            expectation: &'static str,
            parse_fn: F,
        }

        impl<F, T, E> $thing<F>
        where
            F: FnOnce(&[u8]) -> Result<T, E>,
            E: fmt::Display,
        {
            pub fn new(expectation: &'static str, parse_fn: F) -> Self {
                $thing { expectation, parse_fn }
            }
        }

        impl<'de, F, T, E> de::Visitor<'de> for $thing<F>
        where
            F: FnOnce(&[u8]) -> Result<T, E>,
            E: fmt::Display,
        {
            type Value = T;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str(self.expectation)
            }

            fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error>
            where
                V: de::SeqAccess<'de>,
            {
                let mut bytes = [0u8; $len];

                for (i, byte) in bytes.iter_mut().enumerate() {
                    if let Some(value) = seq.next_element()? {
                        *byte = value;
                    } else {
                        return Err(de::Error::invalid_length(i, &self));
                    }
                }
                (self.parse_fn)(&bytes).map_err(de::Error::custom)
            }
        }
    };
}

impl_tuple_visitor!(Tuple32Visitor, 32);
impl_tuple_visitor!(Tuple33Visitor, 33);