rateslib/scheduling/
serde.rs1use crate::json::JSON;
2use crate::scheduling::{Cal, Calendar, Convention, NamedCal, StubInference, UnionCal};
3
4impl JSON for Cal {}
5impl JSON for UnionCal {}
6impl JSON for NamedCal {}
7impl JSON for Calendar {}
8impl JSON for StubInference {}
9impl JSON for Convention {}
10
11#[cfg(test)]
12mod tests {
13    use super::*;
14    use crate::scheduling::ndt;
15
16    #[test]
17    fn test_cal_json() {
18        let hols = vec![ndt(2015, 9, 8), ndt(2015, 9, 10)];
19        let hcal = Cal::new(hols, vec![5, 6]);
20        let js = hcal.to_json().unwrap();
21        let hcal2 = Cal::from_json(&js).unwrap();
22        assert_eq!(hcal, hcal2);
23    }
24
25    #[test]
26    fn test_union_cal_json() {
27        let hols = vec![ndt(2015, 9, 8), ndt(2015, 9, 10)];
28        let settle = vec![ndt(2015, 9, 11)];
29        let hcal = Cal::new(hols, vec![5, 6]);
30        let scal = Cal::new(settle, vec![5, 6]);
31        let ucal = UnionCal::new(vec![hcal], vec![scal].into());
32        let js = ucal.to_json().unwrap();
33        let ucal2 = UnionCal::from_json(&js).unwrap();
34        assert_eq!(ucal, ucal2);
35    }
36
37    #[test]
38    fn test_named_cal_json() {
39        let ncal = NamedCal::try_new("tgt,ldn|fed").unwrap();
40        let js = ncal.to_json().unwrap();
41        let ncal2 = NamedCal::from_json(&js).unwrap();
42        assert_eq!(ncal, ncal2);
43    }
44
45    #[test]
46    fn test_cal_type_json() {
47        let cal = Calendar::NamedCal(NamedCal::try_new("tgt,ldn|fed").unwrap());
48        let js = cal.to_json().unwrap();
49        let cal2 = Calendar::from_json(&js).unwrap();
50        assert_eq!(cal, cal2);
51    }
52
53    #[test]
54    fn test_stub_inf_json() {
55        let si = StubInference::LongBack;
56        let js = si.to_json().unwrap();
57        let si2 = StubInference::from_json(&js).unwrap();
58        assert_eq!(si, si2);
59    }
60}