Skip to main content

java_mapping_serde/
de.rs

1//! Deserialization based on visitors.
2
3use core::{convert::Infallible, fmt::Display};
4
5mod r#impl;
6mod visit;
7
8pub use visit::*;
9
10/// Error type used by a deserializer.
11pub trait Error: core::error::Error + Sized {
12    /// A general error message during deserialization.
13    fn custom<T>(msg: T) -> Self
14    where
15        T: Display;
16
17    /// Receives a type different from what it was expecting when visiting through a deserialization.
18    fn invalid_type(unexp: impl Display, exp: impl Display) -> Self {
19        Self::custom(format_args!("invalid type: {unexp}, expected {exp}"))
20    }
21
22    /// One or more fields are missing in the provided arguments during deserializer.
23    fn missing_field(field: impl Display) -> Self {
24        Self::custom(format_args!("missing field for deserializer: {field}"))
25    }
26}
27
28/// A type that can be deserialized from a [`Deserializer`].
29pub trait Deserialize<'de>: Sized {
30    /// Whether the implementation returns `Some` or `None` conditionally.
31    const IS_CONDITIONAL: bool = true;
32
33    /// Deserializes elements from the given deserializer.
34    fn deserialize<D>(deserializer: D) -> Result<Option<Self>, D::Error>
35    where
36        D: Deserializer<'de>;
37}
38
39/// Deserializer of a mapping file.
40pub trait Deserializer<'de> {
41    /// The error type.
42    type Error: Error;
43
44    /// Whether inner classes should be flattened.
45    const FLAT_CLASSES: bool;
46
47    /// Returns the source namespace of this mapping.
48    fn src_namespace(&self) -> &str;
49
50    /// Returns the destination namespaces of this mapping.
51    fn dst_namespaces(&self) -> impl Iterator<Item = &str>;
52
53    /// Seeks for the next entry and passes it into the given `visitor`.
54    fn deserialize_any<V>(&mut self, visitor: V) -> Result<Option<V::Value>, Self::Error>
55    where
56        V: Visitor<'de>;
57
58    /// Hints the deserializer to deserialize a class.
59    #[inline]
60    fn deserialize_class<V>(&mut self, visitor: V) -> Result<Option<V::Value>, Self::Error>
61    where
62        V: Visitor<'de>,
63    {
64        self.deserialize_any(visitor)
65    }
66
67    /// Hints the deserializer to deserialize a field.
68    #[inline]
69    fn deserialize_field<V>(&mut self, visitor: V) -> Result<Option<V::Value>, Self::Error>
70    where
71        V: Visitor<'de>,
72    {
73        self.deserialize_any(visitor)
74    }
75
76    /// Hints the deserializer to deserialize a method.
77    #[inline]
78    fn deserialize_method<V>(&mut self, visitor: V) -> Result<Option<V::Value>, Self::Error>
79    where
80        V: Visitor<'de>,
81    {
82        self.deserialize_any(visitor)
83    }
84
85    /// Hints the deserializer to deserialize a method argument.
86    #[inline]
87    fn deserialize_method_arg<V>(&mut self, visitor: V) -> Result<Option<V::Value>, Self::Error>
88    where
89        V: Visitor<'de>,
90    {
91        self.deserialize_any(visitor)
92    }
93
94    /// Hints the deserializer to deserialize a method variable.
95    #[inline]
96    fn deserialize_method_var<V>(&mut self, visitor: V) -> Result<Option<V::Value>, Self::Error>
97    where
98        V: Visitor<'de>,
99    {
100        self.deserialize_any(visitor)
101    }
102
103    /// Hints the count of remaining top-level entries of this deserializer.
104    ///
105    /// See [`Iterator::size_hint`] for more information.
106    #[inline]
107    fn size_hint(&self) -> (usize, Option<usize>) {
108        (0, None)
109    }
110}
111
112impl<'de, T> Deserializer<'de> for &mut T
113where
114    T: Deserializer<'de>,
115{
116    type Error = T::Error;
117
118    const FLAT_CLASSES: bool = T::FLAT_CLASSES;
119
120    #[inline]
121    fn deserialize_any<V>(&mut self, visitor: V) -> Result<Option<V::Value>, Self::Error>
122    where
123        V: Visitor<'de>,
124    {
125        T::deserialize_any(self, visitor)
126    }
127
128    #[inline]
129    fn deserialize_class<V>(&mut self, visitor: V) -> Result<Option<V::Value>, Self::Error>
130    where
131        V: Visitor<'de>,
132    {
133        T::deserialize_class(self, visitor)
134    }
135
136    #[inline]
137    fn deserialize_field<V>(&mut self, visitor: V) -> Result<Option<V::Value>, Self::Error>
138    where
139        V: Visitor<'de>,
140    {
141        T::deserialize_field(self, visitor)
142    }
143
144    #[inline]
145    fn deserialize_method<V>(&mut self, visitor: V) -> Result<Option<V::Value>, Self::Error>
146    where
147        V: Visitor<'de>,
148    {
149        T::deserialize_method(self, visitor)
150    }
151
152    #[inline]
153    fn deserialize_method_arg<V>(&mut self, visitor: V) -> Result<Option<V::Value>, Self::Error>
154    where
155        V: Visitor<'de>,
156    {
157        T::deserialize_method_arg(self, visitor)
158    }
159
160    #[inline]
161    fn deserialize_method_var<V>(&mut self, visitor: V) -> Result<Option<V::Value>, Self::Error>
162    where
163        V: Visitor<'de>,
164    {
165        T::deserialize_method_var(self, visitor)
166    }
167
168    #[inline]
169    fn size_hint(&self) -> (usize, Option<usize>) {
170        T::size_hint(self)
171    }
172
173    #[inline]
174    fn src_namespace(&self) -> &str {
175        T::src_namespace(self)
176    }
177
178    #[inline]
179    fn dst_namespaces(&self) -> impl Iterator<Item = &str> {
180        T::dst_namespaces(self)
181    }
182}
183
184impl Error for Infallible {
185    fn custom<T>(_msg: T) -> Self
186    where
187        T: Display,
188    {
189        unreachable!()
190    }
191}