serde_smile/value/
big_integer.rs

1use serde::de::{self, MapAccess, Visitor};
2use serde::ser::SerializeStruct;
3use serde::{Deserialize, Deserializer, Serialize, Serializer};
4use serde_bytes::{ByteBuf, Bytes};
5use std::fmt;
6
7/// A parsed Smile `BigInteger` value.
8///
9/// This is a "magic" type which corresponds to the `BigInteger` type defined in Smile. It is intended to be used only
10/// for serialization and deserialization, and it intentionally does *not* implement any kind of traditional big integer
11/// math API.
12///
13/// It should only be used with the `serde-smile` serializers and deserializers; it will produce a nonsensical encoding
14/// when used with other `serde` libraries.
15#[derive(Clone, PartialEq, Eq, Debug)]
16pub struct BigInteger(Vec<u8>);
17
18impl BigInteger {
19    pub(crate) const STRUCT_NAME: &'static str = "\0SmileBigInteger";
20    pub(crate) const FIELD_NAME: &'static str = "\0SmileBigIntegerField";
21
22    /// Creates a `BigInteger` from its representation as a byte buffer in two's complement big-endian.
23    #[inline]
24    pub fn from_be_bytes(buf: Vec<u8>) -> Self {
25        BigInteger(buf)
26    }
27
28    /// Returns a slice containing the two's complement big-endian representation of the `BigInteger`.
29    #[inline]
30    pub fn as_be_bytes(&self) -> &[u8] {
31        &self.0
32    }
33
34    /// Consumes the `BigInteger`, returning a byte buffer containing its two's complement big-endian representation.
35    #[inline]
36    pub fn into_be_bytes(self) -> Vec<u8> {
37        self.0
38    }
39}
40
41impl Serialize for BigInteger {
42    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
43    where
44        S: Serializer,
45    {
46        let mut s = serializer.serialize_struct(Self::STRUCT_NAME, 1)?;
47        s.serialize_field(Self::FIELD_NAME, &Bytes::new(&self.0))?;
48        s.end()
49    }
50}
51
52impl<'de> Deserialize<'de> for BigInteger {
53    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
54    where
55        D: Deserializer<'de>,
56    {
57        deserializer.deserialize_struct(Self::STRUCT_NAME, &[Self::FIELD_NAME], BigIntegerVisitor)
58    }
59}
60
61pub(crate) struct BigIntegerVisitor;
62
63impl BigIntegerVisitor {
64    pub(crate) fn finish_map<'de, A>(self, mut map: A) -> Result<BigInteger, A::Error>
65    where
66        A: MapAccess<'de>,
67    {
68        map.next_value::<ByteBuf>()
69            .map(|b| BigInteger(b.into_vec()))
70    }
71}
72
73impl<'de> Visitor<'de> for BigIntegerVisitor {
74    type Value = BigInteger;
75
76    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
77        formatter.write_str("a big integer")
78    }
79
80    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
81    where
82        A: MapAccess<'de>,
83    {
84        let value = map.next_key::<BigIntegerKey>()?;
85        if value.is_none() {
86            return Err(de::Error::custom("big integer key not found"));
87        }
88        self.finish_map(map)
89    }
90}
91
92struct BigIntegerKey;
93
94impl<'de> Deserialize<'de> for BigIntegerKey {
95    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
96    where
97        D: Deserializer<'de>,
98    {
99        struct KeyVisitor;
100
101        impl<'de> Visitor<'de> for KeyVisitor {
102            type Value = BigIntegerKey;
103
104            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
105                formatter.write_str("a valid big integer field")
106            }
107
108            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
109            where
110                E: de::Error,
111            {
112                if v == BigInteger::FIELD_NAME {
113                    Ok(BigIntegerKey)
114                } else {
115                    Err(de::Error::custom("expected field with custom name"))
116                }
117            }
118        }
119
120        deserializer.deserialize_identifier(KeyVisitor)
121    }
122}