adapters 0.0.0

A high-performance, native Rust schema validation, serialization, deserialization, and data transformation library.
Documentation
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Deserialization — converting generic [`Value`] instances into concrete Rust types.
//!
//! This module defines the [`Deserialize`] trait, which handles safe type conversion
//! and value-level range checks during deserialization from standard and nested formats.

use crate::error::{DeserializationError, Error};
use crate::value::Value;
use std::collections::BTreeMap;

/// Conversion of an unstructured or structured [`Value`] into a typed Rust representation.
///
/// Implementations of this trait are provided for all primitive types, standard options,
/// vectors, and tree structures. Users can derive this trait automatically for custom structs
/// using `#[derive(Schema)]`.
pub trait Deserialize: Sized {
    /// Deserializes `Self` from the provided dynamic [`Value`].
    ///
    /// # Errors
    ///
    /// Returns a [`DeserializationError`] if the data types mismatch, or if numeric constraints
    /// are violated (e.g. range overflow).
    fn deserialize(value: Value) -> Result<Self, Error>;
}

impl Deserialize for bool {
    fn deserialize(value: Value) -> Result<Self, Error> {
        match value {
            Value::Bool(b) => Ok(b),
            other => Err(DeserializationError::new(format!(
                "expected bool, got {}",
                other.type_name()
            ))
            .into()),
        }
    }
}

impl Deserialize for String {
    fn deserialize(value: Value) -> Result<Self, Error> {
        match value {
            Value::String(s) => Ok(s),
            other => Err(DeserializationError::new(format!(
                "expected string, got {}",
                other.type_name()
            ))
            .into()),
        }
    }
}

impl Deserialize for char {
    fn deserialize(value: Value) -> Result<Self, Error> {
        match value {
            Value::String(s) => {
                let mut chars = s.chars();
                if let (Some(c), None) = (chars.next(), chars.next()) {
                    Ok(c)
                } else {
                    Err(
                        DeserializationError::new("expected a single character string for char")
                            .into(),
                    )
                }
            }
            other => Err(DeserializationError::new(format!(
                "expected string for char, got {}",
                other.type_name()
            ))
            .into()),
        }
    }
}

impl Deserialize for f64 {
    fn deserialize(value: Value) -> Result<Self, Error> {
        match value {
            Value::Float(f) => Ok(f),
            Value::Int(n) => Ok(n as f64),
            other => Err(DeserializationError::new(format!(
                "expected float, got {}",
                other.type_name()
            ))
            .into()),
        }
    }
}

impl Deserialize for f32 {
    fn deserialize(value: Value) -> Result<Self, Error> {
        f64::deserialize(value).map(|f| f as f32)
    }
}

macro_rules! deserialize_int {
    ($($t:ty, $min:expr, $max:expr),*) => {
        $(impl Deserialize for $t {
            fn deserialize(value: Value) -> Result<Self, Error> {
                let n = match &value {
                    Value::Int(n) => *n,
                    Value::Float(f) if f.fract() == 0.0 => *f as i64,
                    other => return Err(DeserializationError::new(
                        format!("expected integer, got {}", other.type_name())
                    ).into()),
                };
                if n < ($min as i64) || n > ($max as i64) {
                    return Err(DeserializationError::new(
                        format!("value {n} out of range for {}", stringify!($t))
                    ).into());
                }
                Ok(n as $t)
            }
        })*
    };
}

deserialize_int!(
    i8,
    i8::MIN,
    i8::MAX,
    i16,
    i16::MIN,
    i16::MAX,
    i32,
    i32::MIN,
    i32::MAX,
    i64,
    i64::MIN,
    i64::MAX
);

macro_rules! deserialize_uint {
    ($($t:ty, $max:expr),*) => {
        $(impl Deserialize for $t {
            fn deserialize(value: Value) -> Result<Self, Error> {
                let n = match &value {
                    Value::Int(n) => *n,
                    Value::Float(f) if f.fract() == 0.0 => *f as i64,
                    other => return Err(DeserializationError::new(
                        format!("expected integer, got {}", other.type_name())
                    ).into()),
                };
                if n < 0 || n > ($max as i64) {
                    return Err(DeserializationError::new(
                        format!("value {n} out of range for {}", stringify!($t))
                    ).into());
                }
                Ok(n as $t)
            }
        })*
    };
}

deserialize_uint!(u8, u8::MAX, u16, u16::MAX, u32, u32::MAX, usize, usize::MAX);

impl Deserialize for u64 {
    fn deserialize(value: Value) -> Result<Self, Error> {
        match value {
            Value::Int(n) if n >= 0 => Ok(n as u64),
            Value::Int(n) => {
                Err(DeserializationError::new(format!("value {n} out of range for u64")).into())
            }
            Value::Float(f) if f.fract() == 0.0 && f >= 0.0 => Ok(f as u64),
            other => Err(DeserializationError::new(format!(
                "expected integer, got {}",
                other.type_name()
            ))
            .into()),
        }
    }
}

impl Deserialize for i128 {
    fn deserialize(value: Value) -> Result<Self, Error> {
        match value {
            Value::Int(n) => Ok(n as i128),
            Value::Float(f) if f.fract() == 0.0 => Ok(f as i128),
            other => Err(DeserializationError::new(format!(
                "expected integer, got {}",
                other.type_name()
            ))
            .into()),
        }
    }
}

impl Deserialize for u128 {
    fn deserialize(value: Value) -> Result<Self, Error> {
        match value {
            Value::Int(n) if n >= 0 => Ok(n as u128),
            Value::Int(n) => {
                Err(DeserializationError::new(format!("value {n} out of range for u128")).into())
            }
            Value::Float(f) if f.fract() == 0.0 && f >= 0.0 => Ok(f as u128),
            other => Err(DeserializationError::new(format!(
                "expected integer, got {}",
                other.type_name()
            ))
            .into()),
        }
    }
}

impl Deserialize for std::net::Ipv4Addr {
    fn deserialize(value: Value) -> Result<Self, Error> {
        match value {
            Value::String(s) => s.parse::<std::net::Ipv4Addr>().map_err(|e| {
                DeserializationError::new(format!("invalid IPv4 address: {}", e)).into()
            }),
            other => Err(DeserializationError::new(format!(
                "expected string for IPv4 address, got {}",
                other.type_name()
            ))
            .into()),
        }
    }
}

impl Deserialize for std::net::Ipv6Addr {
    fn deserialize(value: Value) -> Result<Self, Error> {
        match value {
            Value::String(s) => s.parse::<std::net::Ipv6Addr>().map_err(|e| {
                DeserializationError::new(format!("invalid IPv6 address: {}", e)).into()
            }),
            other => Err(DeserializationError::new(format!(
                "expected string for IPv6 address, got {}",
                other.type_name()
            ))
            .into()),
        }
    }
}

impl Deserialize for std::net::IpAddr {
    fn deserialize(value: Value) -> Result<Self, Error> {
        match value {
            Value::String(s) => s.parse::<std::net::IpAddr>().map_err(|e| {
                DeserializationError::new(format!("invalid IP address: {}", e)).into()
            }),
            other => Err(DeserializationError::new(format!(
                "expected string for IP address, got {}",
                other.type_name()
            ))
            .into()),
        }
    }
}

impl<T: Deserialize> Deserialize for Option<T> {
    fn deserialize(value: Value) -> Result<Self, Error> {
        if value.is_null() {
            Ok(None)
        } else {
            T::deserialize(value).map(Some)
        }
    }
}

impl<T: Deserialize> Deserialize for Vec<T> {
    fn deserialize(value: Value) -> Result<Self, Error> {
        match value {
            Value::Array(arr) => arr.into_iter().map(T::deserialize).collect(),
            other => Err(DeserializationError::new(format!(
                "expected array, got {}",
                other.type_name()
            ))
            .into()),
        }
    }
}

impl<T: Deserialize> Deserialize for BTreeMap<String, T> {
    fn deserialize(value: Value) -> Result<Self, Error> {
        match value {
            Value::Object(map) => map
                .into_iter()
                .map(|(k, v)| T::deserialize(v).map(|t| (k, t)))
                .collect(),
            other => Err(DeserializationError::new(format!(
                "expected object, got {}",
                other.type_name()
            ))
            .into()),
        }
    }
}

impl Deserialize for Value {
    fn deserialize(value: Value) -> Result<Self, Error> {
        Ok(value)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_bool() {
        assert!(bool::deserialize(Value::Bool(true)).unwrap());
    }

    #[test]
    fn test_bool_wrong_type() {
        assert!(bool::deserialize(Value::Int(1)).is_err());
    }

    #[test]
    fn test_i64() {
        assert_eq!(i64::deserialize(Value::Int(42)).unwrap(), 42);
    }

    #[test]
    fn test_u8_in_range() {
        assert_eq!(u8::deserialize(Value::Int(255)).unwrap(), 255u8);
    }

    #[test]
    fn test_u8_out_of_range() {
        assert!(u8::deserialize(Value::Int(256)).is_err());
    }

    #[test]
    fn test_u8_negative_fails() {
        assert!(u8::deserialize(Value::Int(-1)).is_err());
    }

    #[test]
    fn test_i8_out_of_range() {
        assert!(i8::deserialize(Value::Int(200)).is_err());
    }

    #[test]
    fn test_f64() {
        assert_eq!(f64::deserialize(Value::Float(1.23)).unwrap(), 1.23);
    }

    #[test]
    fn test_f64_from_int() {
        assert_eq!(f64::deserialize(Value::Int(5)).unwrap(), 5.0);
    }

    #[test]
    fn test_string() {
        assert_eq!(
            String::deserialize(Value::String("hi".into())).unwrap(),
            "hi"
        );
    }

    #[test]
    fn test_option_none() {
        let v: Option<i32> = Option::deserialize(Value::Null).unwrap();
        assert_eq!(v, None);
    }

    #[test]
    fn test_option_some() {
        let v: Option<i32> = Option::deserialize(Value::Int(7)).unwrap();
        assert_eq!(v, Some(7));
    }

    #[test]
    fn test_vec() {
        let v = Vec::<i32>::deserialize(Value::Array(vec![Value::Int(1), Value::Int(2)])).unwrap();
        assert_eq!(v, vec![1, 2]);
    }

    #[test]
    fn test_btreemap() {
        let mut m = std::collections::BTreeMap::new();
        m.insert("n".to_string(), Value::Int(9));
        let result = BTreeMap::<String, i32>::deserialize(Value::Object(m)).unwrap();
        assert_eq!(result["n"], 9);
    }

    #[test]
    fn test_u64_negative_fails() {
        assert!(u64::deserialize(Value::Int(-1)).is_err());
    }

    #[test]
    fn test_new_types_deserialization() {
        // char tests
        assert_eq!(char::deserialize(Value::String("x".into())).unwrap(), 'x');
        assert!(char::deserialize(Value::String("xy".into())).is_err());
        assert!(char::deserialize(Value::Int(42)).is_err());

        // i128 & u128 tests
        assert_eq!(
            i128::deserialize(Value::Int(123456789)).unwrap(),
            123456789i128
        );
        assert_eq!(
            u128::deserialize(Value::Int(987654321)).unwrap(),
            987654321u128
        );
        assert!(u128::deserialize(Value::Int(-5)).is_err());

        // IpAddress tests
        let ip4 = std::net::Ipv4Addr::new(127, 0, 0, 1);
        let ip6 = std::net::Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);

        assert_eq!(
            std::net::Ipv4Addr::deserialize(Value::String("127.0.0.1".into())).unwrap(),
            ip4
        );
        assert!(std::net::Ipv4Addr::deserialize(Value::String("not-an-ip".into())).is_err());

        assert_eq!(
            std::net::Ipv6Addr::deserialize(Value::String("::1".into())).unwrap(),
            ip6
        );
        assert!(std::net::Ipv6Addr::deserialize(Value::String("not-an-ip".into())).is_err());

        assert_eq!(
            std::net::IpAddr::deserialize(Value::String("127.0.0.1".into())).unwrap(),
            std::net::IpAddr::V4(ip4)
        );
        assert_eq!(
            std::net::IpAddr::deserialize(Value::String("::1".into())).unwrap(),
            std::net::IpAddr::V6(ip6)
        );
    }
}