Skip to content

Commit

Permalink
Allow adding the 'text' attribute to Option<String>
Browse files Browse the repository at this point in the history
  • Loading branch information
ephraimkunz committed Apr 30, 2021
1 parent fb19a35 commit 4b27b04
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
20 changes: 20 additions & 0 deletions yaserde/tests/deserializer.rs
Expand Up @@ -440,6 +440,26 @@ fn de_text_content_with_attributes() {
);
}

#[test]
fn de_text_attribute_on_optional_string() {
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(rename = "base")]
pub struct XmlStruct {
#[yaserde(text)]
text: Option<String>,
}

let model = XmlStruct {
text: Some("Testing text".to_string()),
};
let content = r#"<base>Testing text</base>"#;
convert_and_validate!(content, XmlStruct, model);

let model = XmlStruct { text: None };
let content = r#"<base></base>"#;
convert_and_validate!(content, XmlStruct, model);
}

#[test]
fn de_enum() {
init();
Expand Down
2 changes: 1 addition & 1 deletion yaserde/tests/flatten.rs
Expand Up @@ -54,7 +54,7 @@ fn basic_flatten() {
fn default() -> Self {
DateKind::Working
}
};
}

let model = DateTime {
date: Date {
Expand Down
22 changes: 22 additions & 0 deletions yaserde/tests/serializer.rs
Expand Up @@ -324,6 +324,28 @@ fn ser_text_content_with_attributes() {
serialize_and_validate!(model, content);
}

#[test]
fn ser_text_attribute_on_optional_string() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(rename = "base")]
pub struct XmlStruct {
#[yaserde(text)]
text: Option<String>,
}

let model = XmlStruct {
text: Some("Testing text".to_string()),
};

let content = r#"<base>Testing text</base>"#;
serialize_and_validate!(model, content);

let model = XmlStruct { text: None };

let content = r#"<base></base>"#;
serialize_and_validate!(model, content);
}

#[test]
fn ser_keyword() {
#[derive(YaSerialize, PartialEq, Debug)]
Expand Down
8 changes: 7 additions & 1 deletion yaserde_derive/src/de/expand_struct.rs
Expand Up @@ -303,7 +303,13 @@ pub fn parse(

match field.get_type() {
Field::FieldString => set_text(&quote! { text_content.to_owned() }),
Field::FieldStruct { .. } | Field::FieldOption { .. } | Field::FieldVec { .. } => None,
Field::FieldOption { data_type } => match *data_type {
Field::FieldString => set_text(
&quote! { if text_content.is_empty() { None } else { Some(text_content.to_owned()) }},
),
_ => None,
},
Field::FieldStruct { .. } | Field::FieldVec { .. } => None,
simple_type => {
let type_token: TokenStream = simple_type.into();
set_text(&quote! { #type_token::from_str(text_content).unwrap() })
Expand Down
4 changes: 2 additions & 2 deletions yaserde_derive/src/lib.rs
Expand Up @@ -14,7 +14,7 @@ pub fn derive_deserialize(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
match de::expand_derive_deserialize(&ast) {
Ok(expanded) => expanded.into(),
Err(msg) => panic!(msg),
Err(msg) => panic!("{}", msg),
}
}

Expand All @@ -23,6 +23,6 @@ pub fn derive_serialize(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
match ser::expand_derive_serialize(&ast) {
Ok(expanded) => expanded.into(),
Err(msg) => panic!(msg),
Err(msg) => panic!("{}", msg),
}
}
15 changes: 11 additions & 4 deletions yaserde_derive/src/ser/expand_struct.rs
Expand Up @@ -145,10 +145,17 @@ pub fn serialize(
.map(|field| {
let label = field.label();
if field.is_text_content() {
return Some(quote!(
let data_event = ::xml::writer::XmlEvent::characters(&self.#label);
writer.write(data_event).map_err(|e| e.to_string())?;
));
return match field.get_type() {
Field::FieldOption { .. } => Some(quote!(
let s = self.#label.as_deref().unwrap_or_default();
let data_event = ::xml::writer::XmlEvent::characters(s);
writer.write(data_event).map_err(|e| e.to_string())?;
)),
_ => Some(quote!(
let data_event = ::xml::writer::XmlEvent::characters(&self.#label);
writer.write(data_event).map_err(|e| e.to_string())?;
)),
};
}

let label_name = field.renamed_label(root_attributes);
Expand Down

0 comments on commit 4b27b04

Please sign in to comment.