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

use crate::{Attribution, Lang};

/// A note that was contributed from genealogical research.
///
/// Notes are not intended to contain genealogical conclusions. Notes are only
/// associated with a single genealogical resource.
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, YaSerialize, YaDeserialize, PartialEq, Clone, Default)]
#[yaserde(
    rename = "note",
    prefix = "gx",
    default_namespace = "gx",
    namespace = "gx: http://gedcomx.org/v1/"
)]
#[non_exhaustive]
pub struct Note {
    /// The locale identifier for the note.
    #[yaserde(attribute, prefix = "xml")]
    pub lang: Option<Lang>,

    /// A subject or title for the note.
    #[yaserde(prefix = "gx")]
    pub subject: Option<String>,

    /// The text of the note.
    #[yaserde(prefix = "gx")]
    pub text: String,

    /// The attribution of this note. If not provided, the attribution of the
    /// containing resource of the note is assumed.
    #[yaserde(prefix = "gx")]
    pub attribution: Option<Attribution>,
}

impl Note {
    pub fn new(
        lang: Option<Lang>,
        subject: Option<String>,
        text: String,
        attribution: Option<Attribution>,
    ) -> Self {
        Self {
            lang,
            subject,
            text,
            attribution,
        }
    }

    pub fn builder<I: Into<String>>(text: I) -> NoteBuilder {
        NoteBuilder::new(text)
    }
}

pub struct NoteBuilder(Note);

impl NoteBuilder {
    pub(crate) fn new<I: Into<String>>(text: I) -> Self {
        Self(Note {
            text: text.into(),
            ..Note::default()
        })
    }

    pub fn lang<I: Into<Lang>>(&mut self, lang: I) -> &mut Self {
        self.0.lang = Some(lang.into());
        self
    }

    pub fn subject<I: Into<String>>(&mut self, subject: I) -> &mut Self {
        self.0.subject = Some(subject.into());
        self
    }

    pub fn attribution(&mut self, attribution: Attribution) -> &mut Self {
        self.0.attribution = Some(attribution);
        self
    }

    pub fn build(&self) -> Note {
        Note::new(
            self.0.lang.clone(),
            self.0.subject.clone(),
            self.0.text.clone(),
            self.0.attribution.clone(),
        )
    }
}

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

    #[test]
    fn builder() {
        let expected = Note {
            lang: Some("en".into()),
            subject: Some("subject".to_string()),
            text: "text".to_string(),
            attribution: Some(Attribution::default()),
        };

        let actual = Note::builder("text")
            .lang("en")
            .subject("subject")
            .attribution(Attribution::default())
            .build();

        assert_eq!(actual, expected)
    }

    #[test]
    fn json_deserialize() {
        let data = TestData::new();

        let json = r#"{
            "lang" : "en",
            "subject" : "TestSubject",
            "text" : "This is a note",
            "attribution" : {
                "contributor" : {
                "resource" : "A-1"
                },
                "modified" : 1394175600000
            }        
        }"#;

        let note: Note = serde_json::from_str(json).unwrap();
        assert_eq!(
            note,
            Note {
                lang: Some("en".into()),
                subject: Some("TestSubject".to_string()),
                text: "This is a note".to_string(),
                attribution: Some(data.attribution),
            }
        )
    }

    #[test]
    fn json_deserialize_optional_fields() {
        let json = r#"{
            "text" : "This is a note"      
        }"#;

        let note: Note = serde_json::from_str(json).unwrap();
        assert_eq!(note, Note::builder("This is a note").build())
    }

    #[test]
    fn json_serialize() {
        let data = TestData::new();

        let note = Note {
            lang: Some("en".into()),
            subject: Some("TestSubject".to_string()),
            text: "This is a note".to_string(),
            attribution: Some(data.attribution),
        };

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

        assert_eq!(
            json,
            r#"{"lang":"en","subject":"TestSubject","text":"This is a note","attribution":{"contributor":{"resource":"A-1"},"modified":1394175600000}}"#
        );
    }

    #[test]
    fn json_serialize_optional_fields() {
        let note = Note::builder("This is a note").build();

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

        assert_eq!(json, r#"{"text":"This is a note"}"#);
    }

    #[test]
    fn xml_serialize() {
        let note = Note::builder("...text of the note...")
            .lang("en")
            .subject("...subject or title...")
            .attribution(Attribution::default())
            .build();

        let mut config = yaserde::ser::Config::default();
        config.write_document_declaration = false;
        let xml = yaserde::ser::to_string_with_config(&note, &config).unwrap();
        let expected = r##"<note xmlns="http://gedcomx.org/v1/" xml:lang="en"><subject>...subject or title...</subject><text>...text of the note...</text><attribution /></note>"##;

        assert_eq!(xml, expected)
    }

    #[test]
    fn xml_deserialize() {
        let xml = r##"<note xml:lang="en">
        <subject>...subject or title...</subject>
        <text>...text of the note...</text>
        <attribution>
        </attribution>    
        </note>"##;

        let note: Note = yaserde::de::from_str(&xml).unwrap();
        let expected = Note::builder("...text of the note...")
            .lang("en")
            .subject("...subject or title...")
            .attribution(Attribution::default())
            .build();
        assert_eq!(note, expected)
    }
}