Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub fn parse_site_native_file(xml_path: &Path) -> Result<SiteNative, Error> {
/// name: "Demographics".to_string(),
/// category_type: "normal".to_string(),
/// highest_index: 0,
/// fields: vec![
/// fields: Some(vec![
/// Field {
/// name: "address".to_string(),
/// field_type: "text".to_string(),
Expand Down Expand Up @@ -245,13 +245,13 @@ pub fn parse_site_native_file(xml_path: &Path) -> Result<SiteNative, Error> {
/// },
/// ]),
/// },
/// ],
/// ]),
/// },
/// Category {
/// name: "Enrollment".to_string(),
/// category_type: "normal".to_string(),
/// highest_index: 0,
/// fields: vec![
/// fields: Some(vec![
/// Field {
/// name: "enrollment_closed_date".to_string(),
/// field_type: "popUpCalendar".to_string(),
Expand Down Expand Up @@ -305,7 +305,7 @@ pub fn parse_site_native_file(xml_path: &Path) -> Result<SiteNative, Error> {
/// keep_history: true,
/// entries: None,
/// },
/// ],
/// ]),
/// },
/// ]),
/// }]),
Expand Down Expand Up @@ -353,7 +353,7 @@ pub fn parse_site_native_file(xml_path: &Path) -> Result<SiteNative, Error> {
/// name: "Demographics".to_string(),
/// category_type: "normal".to_string(),
/// highest_index: 0,
/// fields: vec![Field {
/// fields: Some(vec![Field {
/// name: "address".to_string(),
/// field_type: "text".to_string(),
/// data_type: Some("string".to_string()),
Expand All @@ -375,7 +375,7 @@ pub fn parse_site_native_file(xml_path: &Path) -> Result<SiteNative, Error> {
/// }),
/// reason: None,
/// }]),
/// }],
/// }]),
/// }]),
/// }]),
/// },
Expand Down Expand Up @@ -493,7 +493,7 @@ pub fn parse_subject_native_file(xml_path: &Path) -> Result<SubjectNative, Error
/// name: "Demographics".to_string(),
/// category_type: "normal".to_string(),
/// highest_index: 0,
/// fields: vec![Field {
/// fields: Some(vec![Field {
/// name: "breed".to_string(),
/// field_type: "combo-box".to_string(),
/// data_type: Some("string".to_string()),
Expand All @@ -515,7 +515,7 @@ pub fn parse_subject_native_file(xml_path: &Path) -> Result<SubjectNative, Error
/// }),
/// reason: None,
/// }]),
/// }],
/// }]),
/// }]),
/// }]),
/// },
Expand Down Expand Up @@ -561,7 +561,7 @@ pub fn parse_subject_native_file(xml_path: &Path) -> Result<SubjectNative, Error
/// name: "Demographics".to_string(),
/// category_type: "normal".to_string(),
/// highest_index: 0,
/// fields: vec![Field {
/// fields: Some(vec![Field {
/// name: "breed".to_string(),
/// field_type: "combo-box".to_string(),
/// data_type: Some("string".to_string()),
Expand All @@ -583,13 +583,14 @@ pub fn parse_subject_native_file(xml_path: &Path) -> Result<SubjectNative, Error
/// }),
/// reason: None,
/// }]),
/// }],
/// }]),
/// }]),
/// }]),
/// },
/// ],
/// };
/// let result = parse_subject_native_string(xml).unwrap();
///
/// assert_eq!(result, expected);
/// ```
pub fn parse_subject_native_string(xml_str: &str) -> Result<SubjectNative, Error> {
Expand Down Expand Up @@ -696,7 +697,7 @@ pub fn parse_user_native_file(xml_path: &Path) -> Result<UserNative, Error> {
/// name: "demographics".to_string(),
/// category_type: "normal".to_string(),
/// highest_index: 0,
/// fields: vec![
/// fields: Some(vec![
/// Field {
/// name: "address".to_string(),
/// field_type: "text".to_string(),
Expand Down Expand Up @@ -731,13 +732,13 @@ pub fn parse_user_native_file(xml_path: &Path) -> Result<UserNative, Error> {
/// reason: None,
/// }]),
/// },
/// ],
/// ]),
/// },
/// Category {
/// name: "Administrative".to_string(),
/// category_type: "normal".to_string(),
/// highest_index: 0,
/// fields: vec![
/// fields: Some(vec![
/// Field {
/// name: "study_assignment".to_string(),
/// field_type: "text".to_string(),
Expand Down Expand Up @@ -771,14 +772,15 @@ pub fn parse_user_native_file(xml_path: &Path) -> Result<UserNative, Error> {
/// },
/// ]),
/// },
/// ],
/// ]),
/// },
/// ]),
/// }]),
/// }],
/// };
///
/// let result = parse_user_native_string(xml).unwrap();
///
/// assert_eq!(result, expected);
/// ```
pub fn parse_user_native_string(xml_str: &str) -> Result<UserNative, Error> {
Expand Down
28 changes: 14 additions & 14 deletions src/native/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct Value {
pub role: String,
pub when: DateTime<Utc>,

#[serde(rename = "$value")]
#[serde(alias = "$value")]
pub value: String,
}

Expand All @@ -45,7 +45,7 @@ pub struct Value {
pub role: String,
pub when: DateTime<Utc>,

#[serde(rename = "$value")]
#[serde(alias = "$value")]
pub value: String,
}

Expand Down Expand Up @@ -93,7 +93,7 @@ pub struct Reason {
pub role: String,
pub when: DateTime<Utc>,

#[serde(rename = "$value")]
#[serde(alias = "$value")]
pub value: String,
}

Expand All @@ -113,7 +113,7 @@ pub struct Reason {
pub role: String,
pub when: DateTime<Utc>,

#[serde(rename = "$value")]
#[serde(alias = "$value")]
pub value: String,
}

Expand Down Expand Up @@ -185,7 +185,7 @@ pub struct Field {
pub when_created: DateTime<Utc>,
pub keep_history: bool,

#[serde(rename = "entry")]
#[serde(alias = "entry")]
pub entries: Option<Vec<Entry>>,
}

Expand All @@ -209,7 +209,7 @@ pub struct Field {
pub when_created: DateTime<Utc>,
pub keep_history: bool,

#[serde(rename = "entry")]
#[serde(alias = "entry")]
pub entries: Option<Vec<Entry>>,
}

Expand Down Expand Up @@ -263,8 +263,8 @@ pub struct Category {

pub highest_index: usize,

#[serde(rename = "field", default)]
pub fields: Vec<Field>,
#[serde(alias = "field")]
pub fields: Option<Vec<Field>>,
}

#[cfg(feature = "python")]
Expand All @@ -279,8 +279,8 @@ pub struct Category {

pub highest_index: usize,

#[serde(rename = "field", default)]
pub fields: Vec<Field>,
#[serde(alias = "field")]
pub fields: Option<Vec<Field>>,
}

#[cfg(not(feature = "python"))]
Expand Down Expand Up @@ -390,10 +390,10 @@ pub struct Form {

pub form_state: String,

#[serde(rename = "state", default)]
#[serde(alias = "state")]
pub states: Option<Vec<State>>,

#[serde(rename = "category", default)]
#[serde(alias = "category")]
pub categories: Option<Vec<Category>>,
}

Expand Down Expand Up @@ -450,10 +450,10 @@ pub struct Form {

pub form_state: String,

#[serde(rename = "state", default)]
#[serde(alias = "state")]
pub states: Option<Vec<State>>,

#[serde(rename = "category", default)]
#[serde(alias = "category")]
pub categories: Option<Vec<Category>>,
}

Expand Down
26 changes: 18 additions & 8 deletions src/native/deserializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,24 @@ pub fn deserialize_empty_string_as_none_datetime<'de, D>(
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
if s.is_empty() {
Ok(None)
} else {
// Parse the datetime with a fixed offset, then convert it to UTC
let dt_with_offset = DateTime::parse_from_str(&s, "%Y-%m-%d %H:%M:%S %z")
.map_err(serde::de::Error::custom)?;
Ok(Some(dt_with_offset.with_timezone(&Utc)))
let s: Option<String> = Deserialize::deserialize(deserializer)?;
match s {
Some(v) => {
if v.is_empty() {
Ok(None)
} else {
// Parse the datetime with a fixed offset, then convert it to UTC

let dt_with_offset = if v.ends_with('Z') {
DateTime::parse_from_rfc3339(&v).map_err(serde::de::Error::custom)?
} else {
DateTime::parse_from_str(&v, "%Y-%m-%d %H:%M:%S %z")
.map_err(serde::de::Error::custom)?
};
Ok(Some(dt_with_offset.with_timezone(&Utc)))
}
}
None => Ok(None),
}
}

Expand Down
Loading