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
use quickcheck::{Arbitrary, Gen};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use yaserde_derive::{YaDeserialize, YaSerialize};

use crate::GedcomxDate;

/// A concluded genealogical date.
#[skip_serializing_none]
#[derive(
    Debug, Serialize, Deserialize, YaSerialize, YaDeserialize, PartialEq, Clone, Default, Eq,
)]
#[yaserde(
    prefix = "gx",
    default_namespace = "gx",
    namespace = "gx: http://gedcomx.org/v1/"
)]
#[non_exhaustive]
pub struct Date {
    /// The original value of the date as supplied by the contributor.
    #[yaserde(prefix = "gx")]
    pub original: Option<String>,

    /// The standardized formal value of the date, formatted according to the
    /// GEDCOM X Date Format specification.
    #[yaserde(prefix = "gx")]
    pub formal: Option<GedcomxDate>,
}

impl Date {
    pub fn new<I: Into<String>>(original: Option<I>, formal: Option<GedcomxDate>) -> Self {
        Self {
            original: original.map(std::convert::Into::into),
            formal,
        }
    }
}

impl Arbitrary for Date {
    fn arbitrary(g: &mut Gen) -> Self {
        Self::new(
            Some(crate::arbitrary_trimmed(g)),
            Some(GedcomxDate::arbitrary(g)),
        )
    }
}

#[cfg(test)]
mod test {
    use pretty_assertions::assert_eq;
    use yaserde::ser::Config;

    use super::*;

    #[test]
    fn json_deserialize() {
        let json = r#"{
            "original" : "the original text",
            "formal" : "+0987-01-25T23:59Z"
          }"#;

        let date: Date = serde_json::from_str(json).unwrap();

        assert_eq!(
            date,
            Date {
                original: Some("the original text".to_string()),
                formal: Some("+0987-01-25T23:59Z".parse().unwrap())
            }
        );
    }

    #[test]
    fn json_deserialize_optional_fields() {
        let json = r#"{}"#;

        let date: Date = serde_json::from_str(json).unwrap();

        assert_eq!(
            date,
            Date {
                original: None,
                formal: None
            }
        );
    }

    #[test]
    fn json_serialize() {
        let date = Date {
            original: Some("the original text".to_string()),
            formal: Some("+0987-01-25T23:59Z".parse().unwrap()),
        };

        let json = serde_json::to_string(&date).unwrap();

        assert_eq!(
            json,
            r#"{"original":"the original text","formal":"+0987-01-25T23:59Z"}"#
        );
    }

    #[test]
    fn json_serialize_optional_fields() {
        let date = Date {
            original: None,
            formal: None,
        };

        let json = serde_json::to_string(&date).unwrap();

        assert_eq!(json, r#"{}"#);
    }

    #[test]
    fn xml_deserialize() {
        let xml = r#"
        <date>
            <original>the original text</original>
            <formal>+0987-01-25T23:59Z</formal>
        </date>"#;

        let date: Date = yaserde::de::from_str(xml).unwrap();

        assert_eq!(
            date,
            Date {
                original: Some("the original text".to_string()),
                formal: Some("+0987-01-25T23:59Z".parse().unwrap())
            }
        );
    }

    #[test]
    fn xml_deserialize_optional_fields() {
        let xml = r#"<date />"#;

        let date: Date = yaserde::de::from_str(xml).unwrap();

        assert_eq!(
            date,
            Date {
                original: None,
                formal: None
            }
        );
    }

    #[test]
    fn xml_serialize() {
        let date = Date {
            original: Some("the original text".to_string()),
            formal: Some("+0987-01-25T23:59Z".parse().unwrap()),
        };

        let config = Config {
            write_document_declaration: false,
            ..Default::default()
        };
        let xml = yaserde::ser::to_string_with_config(&date, &config).unwrap();

        assert_eq!(
            xml,
            r#"<Date xmlns="http://gedcomx.org/v1/"><original>the original text</original><formal>+0987-01-25T23:59Z</formal></Date>"#
        );
    }

    #[test]
    fn xml_serialize_optional_fields() {
        let date = Date {
            original: None,
            formal: None,
        };

        let config = Config {
            write_document_declaration: false,
            ..Default::default()
        };
        let xml = yaserde::ser::to_string_with_config(&date, &config).unwrap();

        assert_eq!(xml, r#"<Date xmlns="http://gedcomx.org/v1/" />"#);
    }

    #[quickcheck_macros::quickcheck]
    fn roundtrip_json(input: Date) -> bool {
        let json = serde_json::to_string(&input).unwrap();
        let from_json: Date = serde_json::from_str(&json).unwrap();
        assert_eq!(input, from_json);
        input == from_json
    }

    #[quickcheck_macros::quickcheck]
    fn roundtrip_xml(input: Date) -> bool {
        let xml = yaserde::ser::to_string(&input).unwrap();
        let from_xml: Date = yaserde::de::from_str(&xml).unwrap();
        input == from_xml
    }
}