qfall_math/rational/q/
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::Q;
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", Q);
25deserialize!("value", Value, Q);
26
27#[cfg(test)]
28mod test_serialize {
29    use crate::rational::Q;
30    use std::str::FromStr;
31
32    /// Tests whether the serialization of a positive [`Q`] works.
33    #[test]
34    fn serialize_output_positive() {
35        let q = Q::from((17, 3));
36        let cmp_str = "{\"value\":\"17/3\"}";
37
38        assert_eq!(cmp_str, serde_json::to_string(&q).unwrap());
39    }
40
41    /// Tests whether the serialization of a negative [`Q`] works.
42    #[test]
43    fn serialize_output_negative() {
44        let q = Q::from((-17, 3));
45        let cmp_str = "{\"value\":\"-17/3\"}";
46
47        assert_eq!(cmp_str, serde_json::to_string(&q).unwrap());
48    }
49
50    /// Tests whether the serialization of a positive large [`Q`] works.
51    #[test]
52    fn serialize_output_positive_large() {
53        let val_str = format!("{}/2", u64::MAX);
54        let q = Q::from_str(&val_str).unwrap();
55        let cmp_str = format!("{{\"value\":\"{val_str}\"}}");
56
57        assert_eq!(cmp_str, serde_json::to_string(&q).unwrap());
58    }
59
60    /// Tests whether the serialization of a negative large [`Q`] works.
61    #[test]
62    fn serialize_output_negative_large() {
63        let val_str = format!("-{}/2", u64::MAX);
64        let q = Q::from_str(&val_str).unwrap();
65        let cmp_str = format!("{{\"value\":\"{val_str}\"}}");
66
67        assert_eq!(cmp_str, serde_json::to_string(&q).unwrap());
68    }
69}
70
71#[cfg(test)]
72mod test_deserialize {
73    use crate::rational::Q;
74    use std::str::FromStr;
75
76    /// Tests whether the deserialization of a positive [`Q`] works.
77    #[test]
78    fn deserialize_positive() {
79        let q_string = "{\"value\":\"17/3\"}";
80        assert_eq!(
81            Q::from((17, 3)),
82            serde_json::from_str::<Q>(q_string).unwrap()
83        );
84    }
85
86    /// Tests whether the deserialization of a negative [`Q`] works.
87    #[test]
88    fn deserialize_negative() {
89        let q_string = "{\"value\":\"-17/3\"}";
90        assert_eq!(
91            Q::from((-17, 3)),
92            serde_json::from_str::<Q>(q_string).unwrap()
93        );
94    }
95
96    /// Tests whether the deserialization of a positive large [`Q`] works.
97    #[test]
98    fn deserialize_positive_large() {
99        let val_str = format!("{}/2", u64::MAX);
100        let z_string = format!("{{\"value\":\"{val_str}\"}}");
101
102        assert_eq!(
103            Q::from_str(&val_str).unwrap(),
104            serde_json::from_str::<Q>(&z_string).unwrap()
105        )
106    }
107
108    /// Tests whether the deserialization of a negative large [`Q`] works.
109    #[test]
110    fn deserialize_negative_large() {
111        let val_str = format!("-{}/2", u64::MAX);
112        let z_string = format!("{{\"value\":\"{val_str}\"}}");
113
114        assert_eq!(
115            Q::from_str(&val_str).unwrap(),
116            serde_json::from_str::<Q>(&z_string).unwrap()
117        )
118    }
119
120    /// Tests whether no fields 'value' provided yield an error
121    #[test]
122    fn no_field_value() {
123        let a: Result<Q, serde_json::Error> = serde_json::from_str("{{\"tree\":\"{17}\"}}");
124        assert!(a.is_err());
125
126        let b: Result<Q, serde_json::Error> = serde_json::from_str("{{}}");
127        assert!(b.is_err());
128    }
129
130    /// Tests whether too many fields yield an error
131    #[test]
132    fn too_many_fields() {
133        let a: Result<Q, serde_json::Error> =
134            serde_json::from_str("{{\"tree\":\"{17/3}\", \"value\":\"{17}\"}}");
135        assert!(a.is_err());
136
137        let b: Result<Q, serde_json::Error> =
138            serde_json::from_str("{{\"value\":\"{}\", \"value\":\"{17}\"}}");
139        assert!(b.is_err());
140    }
141}