qfall_math/integer/z/
serialize.rs

1// Copyright © 2023 Marvin Beckmann
2//
3// This file is part of qFALL-math.
4//
5// qFALL-math is free software: you can redistribute it and/or modify it under
6// the terms of the Mozilla Public License Version 2.0 as published by the
7// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
8
9//! This module contains implementations of functions
10//! important for serialization such as the [`Serialize`] and [`Deserialize`] trait.
11//!
12//! The explicit functions contain the documentation.
13
14use super::Z;
15use crate::macros::serialize::{deserialize, serialize};
16use core::fmt;
17use serde::{
18    Deserialize, Serialize,
19    de::{Error, MapAccess, Unexpected, Visitor},
20    ser::SerializeStruct,
21};
22use std::str::FromStr;
23
24serialize!("value", Z);
25deserialize!("value", Value, Z);
26
27#[cfg(test)]
28mod test_serialize {
29    use crate::integer::Z;
30    use std::str::FromStr;
31
32    /// Tests whether the serialization of a positive [`Z`] works.
33    #[test]
34    fn serialize_output_positive() {
35        let z = Z::from(17);
36        let cmp_str = "{\"value\":\"17\"}";
37
38        assert_eq!(cmp_str, serde_json::to_string(&z).unwrap());
39    }
40
41    /// Tests whether the serialization of a negative [`Z`] works.
42    #[test]
43    fn serialize_output_negative() {
44        let z = Z::from(-17);
45        let cmp_str = "{\"value\":\"-17\"}";
46
47        assert_eq!(cmp_str, serde_json::to_string(&z).unwrap());
48    }
49
50    /// Tests whether the serialization of a positive large [`Z`] works.
51    #[test]
52    fn serialize_output_positive_large() {
53        let val_str = u64::MAX.to_string();
54        let z = Z::from_str(&val_str).unwrap();
55        let cmp_str = format!("{{\"value\":\"{val_str}\"}}");
56
57        assert_eq!(cmp_str, serde_json::to_string(&z).unwrap());
58    }
59
60    /// Tests whether the serialization of a negative large [`Z`] works.
61    #[test]
62    fn serialize_output_negative_large() {
63        let val_str = format!("-{}", u64::MAX);
64        let z = Z::from_str(&val_str).unwrap();
65        let cmp_str = format!("{{\"value\":\"{val_str}\"}}");
66
67        assert_eq!(cmp_str, serde_json::to_string(&z).unwrap());
68    }
69}
70
71#[cfg(test)]
72mod test_deserialize {
73    use crate::integer::Z;
74    use std::str::FromStr;
75
76    /// Tests whether the deserialization of a positive [`Z`] works.
77    #[test]
78    fn deserialize_positive() {
79        let z_string = "{\"value\":\"17\"}";
80        assert_eq!(Z::from(17), serde_json::from_str::<Z>(z_string).unwrap());
81    }
82
83    /// Tests whether the deserialization of a negative [`Z`] works.
84    #[test]
85    fn deserialize_negative() {
86        let z_string = "{\"value\":\"-17\"}";
87        assert_eq!(Z::from(-17), serde_json::from_str::<Z>(z_string).unwrap());
88    }
89
90    /// Tests whether the deserialization of a positive large [`Z`] works.
91    #[test]
92    fn deserialize_positive_large() {
93        let val_str = u64::MAX.to_string();
94        let z_string = format!("{{\"value\":\"{val_str}\"}}");
95
96        assert_eq!(
97            Z::from_str(&val_str).unwrap(),
98            serde_json::from_str::<Z>(&z_string).unwrap()
99        )
100    }
101
102    /// Tests whether the deserialization of a negative large [`Z`] works.
103    #[test]
104    fn deserialize_negative_large() {
105        let val_str = format!("-{}", u64::MAX);
106        let z_string = format!("{{\"value\":\"{val_str}\"}}");
107
108        assert_eq!(
109            Z::from_str(&val_str).unwrap(),
110            serde_json::from_str::<Z>(&z_string).unwrap()
111        )
112    }
113
114    /// Tests whether no fields 'value' provided yield an error
115    #[test]
116    fn no_field_value() {
117        let a: Result<Z, serde_json::Error> = serde_json::from_str("{{\"tree\":\"{17}\"}}");
118        assert!(a.is_err());
119
120        let b: Result<Z, serde_json::Error> = serde_json::from_str("{{}}");
121        assert!(b.is_err());
122    }
123
124    /// Tests whether too many fields yield an error
125    #[test]
126    fn too_many_fields() {
127        let a: Result<Z, serde_json::Error> =
128            serde_json::from_str("{{\"tree\":\"{17}\", \"value\":\"{17}\"}}");
129        assert!(a.is_err());
130
131        let b: Result<Z, serde_json::Error> =
132            serde_json::from_str("{{\"value\":\"{}\", \"value\":\"{17}\"}}");
133        assert!(b.is_err());
134    }
135}