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
217
218
/// Module containing the [errors].
pub mod errors;
/// Module containing the [native] structs.
pub mod subject_native;

use std::{fs::read_to_string, path::Path};

use crate::errors::Error;
use crate::subject_native::SubjectNative;

/// Parses a Prelude native XML file into a `Native` stuct.
///
/// # Example
///
/// ```
/// use std::path::Path;
///
/// use prelude_xml_parser::parse_subject_native_file;
///
/// let file_path = Path::new("tests/assets/subject_native.xml");
/// let native = parse_subject_native_file(&file_path).unwrap();
///
/// assert!(native.patients.len() >= 1, "Vector length is less than 1");
/// ```
pub fn parse_subject_native_file(xml_path: &Path) -> Result<SubjectNative, Error> {
    if !xml_path.exists() {
        return Err(Error::FileNotFound(xml_path.to_path_buf()));
    }

    let xml_file = read_to_string(xml_path)?;
    let native = parse_subject_native_string(&xml_file)?;

    Ok(native)
}

/// Parse a string of Preliude native site XML into a `Native` struct.
///
/// # Example
///
/// ```
/// use chrono::{DateTime, Utc};
/// use prelude_xml_parser::parse_subject_native_string;
/// use prelude_xml_parser::subject_native::*;
///
/// let xml = r#"<export_from_vision_EDC date="30-May-2024 10:35 -0500" createdBy="Paul Sanders" role="Project Manager" numberSubjectsProcessed="4">
///     <patient patientId="ABC-001" uniqueId="1681574905819" whenCreated="2023-04-15 12:09:02 -0400" creator="Paul Sanders" siteName="Some Site" siteUniqueId="1681574834910" lastLanguage="English" numberOfForms="6">
///       <form name="day.0.form.name.demographics" lastModified="2023-04-15 12:09:15 -0400" whoLastModifiedName="Paul Sanders" whoLastModifiedRole="Project Manager" whenCreated="1681574905839" hasErrors="false" hasWarnings="false" locked="false" user="" dateTimeChanged="" formTitle="Demographics" formIndex="1" formGroup="Day 0" formState="In-Work">
///         <state value="form.state.in.work" signer="Paul Sanders - Project Manager" signerUniqueId="1681162687395" dateSigned="2023-04-15 12:09:02 -0400"/>
///         <category name="Demographics" type="normal" highestIndex="0">
///           <field name="breed" type="combo-box" dataType="string" errorCode="valid" whenCreated="2023-04-15 12:08:26 -0400" keepHistory="true">
///             <entry id="1">
///               <value by="Paul Sanders" byUniqueId="1681162687395" role="Project Manager" when="2023-04-15 12:09:02 -0400" xml:space="preserve">Labrador</value>
///             </entry>
///           </field>
///         </category>
///       </form>
///     </patient>
///     <patient patientId="DEF-002" uniqueId="1681574905820" whenCreated="2023-04-16 12:10:02 -0400" creator="Wade Watts" siteName="Another Site" siteUniqueId="1681574834911" lastLanguage="" numberOfForms="8">
///       <form name="day.0.form.name.demographics" lastModified="2023-04-16 12:10:15 -0400" whoLastModifiedName="Barney Rubble" whoLastModifiedRole="Technician" whenCreated="1681574905838" hasErrors="false" hasWarnings="false" locked="false" user="" dateTimeChanged="" formTitle="Demographics" formIndex="1" formGroup="Day 0" formState="In-Work">
///         <state value="form.state.in.work" signer="Paul Sanders - Project Manager" signerUniqueId="1681162687395" dateSigned="2023-04-16 12:10:02 -0400"/>
///         <category name="Demographics" type="normal" highestIndex="0">
///           <field name="breed" type="combo-box" dataType="string" errorCode="valid" whenCreated="2023-04-15 12:08:26 -0400" keepHistory="true">
///             <entry id="1">
///               <value by="Paul Sanders" byUniqueId="1681162687395" role="Project Manager" when="2023-04-15 12:09:02 -0400" xml:space="preserve">Labrador</value>
///             </entry>
///           </field>
///         </category>
///       </form>
///     </patient>
/// </export_from_vision_EDC>
/// "#;
///
/// let expected = SubjectNative {
///     patients: vec![
///         Patient {
///             patient_id: "ABC-001".to_string(),
///             unique_id: "1681574905819".to_string(),
///             when_created: DateTime::parse_from_rfc3339("2023-04-15T16:09:02Z")
///                 .unwrap()
///                 .with_timezone(&Utc),
///             creator: "Paul Sanders".to_string(),
///             site_name: "Some Site".to_string(),
///             site_unique_id: "1681574834910".to_string(),
///             last_language: Some("English".to_string()),
///             number_of_forms: 6,
///             forms: Some(vec![Form {
///                 name: "day.0.form.name.demographics".to_string(),
///                 last_modified: Some(DateTime::parse_from_rfc3339("2023-04-15T16:09:15Z")
///                     .unwrap()
///                     .with_timezone(&Utc)),
///                 who_last_modified_name: Some("Paul Sanders".to_string()),
///                 who_last_modified_role: Some("Project Manager".to_string()),
///                 when_created: 1681574905839,
///                 has_errors: false,
///                 has_warnings: false,
///                 locked: false,
///                 user: None,
///                 date_time_changed: None,
///                 form_title: "Demographics".to_string(),
///                 form_index: 1,
///                 form_group: "Day 0".to_string(),
///                 form_state: "In-Work".to_string(),
///                 states: Some(vec![State {
///                     value: "form.state.in.work".to_string(),
///                     signer: "Paul Sanders - Project Manager".to_string(),
///                     signer_unique_id: "1681162687395".to_string(),
///                     date_signed: Some(
///                         DateTime::parse_from_rfc3339("2023-04-15T16:09:02Z")
///                             .unwrap()
///                             .with_timezone(&Utc),
///                     ),
///                 }]),
///                 categories: Some(vec![Category {
///                     name: "Demographics".to_string(),
///                     category_type: "normal".to_string(),
///                     highest_index: 0,
///                     fields: vec![Field {
///                         name: "breed".to_string(),
///                         field_type: "combo-box".to_string(),
///                         data_type: "string".to_string(),
///                         error_code: "valid".to_string(),
///                         when_created: DateTime::parse_from_rfc3339("2023-04-15T16:08:26Z")
///                             .unwrap()
///                             .with_timezone(&Utc),
///                         keep_history: true,
///                         entries: Some(vec![Entry {
///                             id: "1".to_string(),
///                             value: Some(Value {
///                                 by: "Paul Sanders".to_string(),
///                                 by_unique_id: Some("1681162687395".to_string()),
///                                 role: "Project Manager".to_string(),
///                                 when: DateTime::parse_from_rfc3339("2023-04-15T16:09:02Z")
///                                     .unwrap()
///                                     .with_timezone(&Utc),
///                                 value: "Labrador".to_string(),
///                             }),
///                         }]),
///                     }],
///                 }]),
///             }]),
///         },
///         Patient {
///             patient_id: "DEF-002".to_string(),
///             unique_id: "1681574905820".to_string(),
///             when_created: DateTime::parse_from_rfc3339("2023-04-16T16:10:02Z")
///                 .unwrap()
///                 .with_timezone(&Utc),
///             creator: "Wade Watts".to_string(),
///             site_name: "Another Site".to_string(),
///             site_unique_id: "1681574834911".to_string(),
///             last_language: None,
///             number_of_forms: 8,
///             forms: Some(vec![Form {
///                 name: "day.0.form.name.demographics".to_string(),
///                 last_modified: Some(DateTime::parse_from_rfc3339("2023-04-16T16:10:15Z")
///                     .unwrap()
///                     .with_timezone(&Utc)),
///                 who_last_modified_name: Some("Barney Rubble".to_string()),
///                 who_last_modified_role: Some("Technician".to_string()),
///                 when_created: 1681574905838,
///                 has_errors: false,
///                 has_warnings: false,
///                 locked: false,
///                 user: None,
///                 date_time_changed: None,
///                 form_title: "Demographics".to_string(),
///                 form_index: 1,
///                 form_group: "Day 0".to_string(),
///                 form_state: "In-Work".to_string(),
///                 states: Some(vec![State {
///                     value: "form.state.in.work".to_string(),
///                     signer: "Paul Sanders - Project Manager".to_string(),
///                     signer_unique_id: "1681162687395".to_string(),
///                     date_signed: Some(
///                         DateTime::parse_from_rfc3339("2023-04-16T16:10:02Z")
///                             .unwrap()
///                             .with_timezone(&Utc),
///                     ),
///                 }]),
///                 categories: Some(vec![Category {
///                     name: "Demographics".to_string(),
///                     category_type: "normal".to_string(),
///                     highest_index: 0,
///                     fields: vec![Field {
///                         name: "breed".to_string(),
///                         field_type: "combo-box".to_string(),
///                         data_type: "string".to_string(),
///                         error_code: "valid".to_string(),
///                         when_created: DateTime::parse_from_rfc3339("2023-04-15T16:08:26Z")
///                             .unwrap()
///                             .with_timezone(&Utc),
///                         keep_history: true,
///                         entries: Some(vec![Entry {
///                             id: "1".to_string(),
///                             value: Some(Value {
///                                 by: "Paul Sanders".to_string(),
///                                 by_unique_id: Some("1681162687395".to_string()),
///                                 role: "Project Manager".to_string(),
///                                 when: DateTime::parse_from_rfc3339("2023-04-15T16:09:02Z")
///                                     .unwrap()
///                                     .with_timezone(&Utc),
///                                 value: "Labrador".to_string(),
///                             }),
///                         }]),
///                     }],
///                 }]),
///             }]),
///         },
///     ],
/// };
/// let result = parse_subject_native_string(xml).unwrap();
/// assert_eq!(result, expected);
/// ```
pub fn parse_subject_native_string(xml_str: &str) -> Result<SubjectNative, Error> {
    let native: SubjectNative = serde_xml_rs::from_str(xml_str)?;

    Ok(native)
}