diff --git a/README.md b/README.md index 401663f..4120e54 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ This library will support XML de/ser-ializing with all specific features. - [x] **namespace**: defines the namespace of the field - [x] **rename**: be able to rename a field - [x] **root**: rename the based element. Used only at the XML root. +- [x] **skip_serializing**: Exclude this field from the serialized output - [x] **skip_serializing_if**: Skip the serialisation for this field if the condition is true - [x] **text**: this field match to the text content diff --git a/yaserde/tests/skip.rs b/yaserde/tests/skip.rs new file mode 100644 index 0000000..db40266 --- /dev/null +++ b/yaserde/tests/skip.rs @@ -0,0 +1,27 @@ +#[macro_use] +extern crate yaserde; +#[macro_use] +extern crate yaserde_derive; + +fn init() { + let _ = env_logger::builder().is_test(true).try_init(); +} + +#[test] +fn skip_serializing() { + init(); + + #[derive(YaSerialize, PartialEq, Debug)] + #[yaserde(rename = "base")] + pub struct XmlStruct { + #[yaserde(skip_serializing)] + skipped_serializing: String, + } + + let model = XmlStruct { + skipped_serializing: "skipped serializing".to_string(), + }; + + let content = ""; + serialize_and_validate!(model, content); +} diff --git a/yaserde_derive/src/common/attribute.rs b/yaserde_derive/src/common/attribute.rs index 88afe95..be4361f 100644 --- a/yaserde_derive/src/common/attribute.rs +++ b/yaserde_derive/src/common/attribute.rs @@ -12,6 +12,7 @@ pub struct YaSerdeAttribute { pub namespaces: BTreeMap, String>, pub prefix: Option, pub rename: Option, + pub skip_serializing: bool, pub skip_serializing_if: Option, pub text: bool, } @@ -39,6 +40,7 @@ impl YaSerdeAttribute { let mut namespaces = BTreeMap::new(); let mut prefix = None; let mut rename = None; + let mut skip_serializing = false; let mut skip_serializing_if = None; let mut text = false; @@ -80,6 +82,9 @@ impl YaSerdeAttribute { "rename" => { rename = get_value(&mut attr_iter); } + "skip_serializing" => { + skip_serializing = true; + } "skip_serializing_if" => { skip_serializing_if = get_value(&mut attr_iter); } @@ -102,6 +107,7 @@ impl YaSerdeAttribute { namespaces, prefix, rename, + skip_serializing, skip_serializing_if, text, } @@ -176,6 +182,7 @@ fn parse_empty_attributes() { namespaces: BTreeMap::new(), prefix: None, rename: None, + skip_serializing: false, skip_serializing_if: None, text: false, }, @@ -225,6 +232,7 @@ fn parse_attributes() { namespaces: BTreeMap::new(), prefix: None, rename: None, + skip_serializing: false, skip_serializing_if: None, text: false, }, @@ -274,6 +282,7 @@ fn only_parse_yaserde_attributes() { namespaces: BTreeMap::new(), prefix: None, rename: None, + skip_serializing: false, skip_serializing_if: None, text: false, }, @@ -329,6 +338,7 @@ fn parse_attributes_with_values() { namespaces, prefix: None, rename: None, + skip_serializing: false, skip_serializing_if: None, text: false, }, diff --git a/yaserde_derive/src/common/field.rs b/yaserde_derive/src/common/field.rs index e44a638..0aa9d83 100644 --- a/yaserde_derive/src/common/field.rs +++ b/yaserde_derive/src/common/field.rs @@ -40,6 +40,10 @@ impl YaSerdeField { self.syn_field.ident.clone() } + pub fn is_skip_serializing(&self) -> bool { + self.attributes.skip_serializing + } + pub fn get_value_label(&self) -> Option { self .syn_field diff --git a/yaserde_derive/src/ser/element.rs b/yaserde_derive/src/ser/element.rs index 1934551..0f2fb0c 100644 --- a/yaserde_derive/src/ser/element.rs +++ b/yaserde_derive/src/ser/element.rs @@ -47,18 +47,20 @@ pub fn condition_generator(label: &Option, field: &YaSerdeField) -> Token .get_default_function() .map(|default_function| quote!(self.#label != #default_function())); + let skip_serializing = field.is_skip_serializing(); + field .get_skip_serializing_if_function() .map(|skip_if_function| { if let Some(prev_conditions) = &default_condition { - quote!(if !self.#skip_if_function(&self.#label) && #prev_conditions) + quote!(if !#skip_serializing && !self.#skip_if_function(&self.#label) && #prev_conditions) } else { - quote!(if !self.#skip_if_function(&self.#label)) + quote!(if !#skip_serializing && !self.#skip_if_function(&self.#label)) } }) .unwrap_or_else(|| { default_condition - .map(|condition| quote!(if #condition)) - .unwrap_or_default() + .map(|condition| quote!(if !#skip_serializing && #condition)) + .unwrap_or(quote!(if !#skip_serializing)) }) }