diff --git a/Cargo.toml b/Cargo.toml index ae81050..1dbb542 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ authors = ["LIAUD Corentin "] categories = ["data-structures"] description = "Easy and highly-configurable XML builder/writer" -edition = "2021" +edition = "2024" keywords = ["xml"] license = "MIT" name = "xml-builder" diff --git a/src/builder.rs b/src/builder.rs index a840701..3c272d5 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,4 +1,4 @@ -use crate::{XMLVersion, XML}; +use crate::{XML, XMLVersion}; /// Builder structure used to generate a custom XML structure. pub struct XMLBuilder { @@ -10,7 +10,7 @@ pub struct XMLBuilder { /// The encoding to set for the document. /// /// Defaults to `UTF-8`. - encoding: String, + encoding: Option, /// XML standalone attribute. /// @@ -44,7 +44,7 @@ impl Default for XMLBuilder { fn default() -> Self { Self { version: XMLVersion::XML1_0, - encoding: "UTF-8".into(), + encoding: None, standalone: None, indent: true, sort_attributes: false, @@ -77,7 +77,7 @@ impl XMLBuilder { /// /// `encoding` - A String representing the encoding to use for the document. pub fn encoding(mut self, encoding: String) -> Self { - self.encoding = encoding; + self.encoding = Some(encoding); self } diff --git a/src/xml.rs b/src/xml.rs index b180413..d0162d2 100644 --- a/src/xml.rs +++ b/src/xml.rs @@ -10,10 +10,12 @@ pub struct XML { /// Defaults to `XML1.0`. version: XMLVersion, - /// The encoding to set for the document. + /// XML encoding attribute. /// - /// Defaults to `UTF-8`. - encoding: String, + /// The optional encoding to set for the document. + /// + /// Defaults to `None`. + encoding: Option, /// XML standalone attribute. /// @@ -49,7 +51,7 @@ pub struct XML { impl XML { pub(crate) fn new( version: XMLVersion, - encoding: String, + encoding: Option, standalone: Option, indent: bool, sort_attributes: bool, @@ -81,21 +83,28 @@ impl XML { /// /// Consumes the XML object. pub fn generate(self, mut writer: W) -> Result<()> { - let standalone_attribute = match self.standalone { - Some(_) => r#" standalone="yes""#.to_string(), - None => String::default(), - }; - let suffix = match self.break_lines { - true => "\n", - false => "", - }; - write!( writer, - r#"{}"#, - self.version, self.encoding, standalone_attribute, suffix + r#""#, + self.version, + encoding = if let Some(encoding) = self.encoding { + format!(" encoding=\"{encoding}\"") + } else { + String::default() + }, + standalone = if let Some(standalone) = self.standalone + && standalone + { + " standalone=\"yes\"".to_string() + } else { + String::default() + } )?; + if self.break_lines { + writeln!(writer)?; + } + // And then XML elements if present... if let Some(elem) = &self.root { elem.render( diff --git a/src/xmlelement.rs b/src/xmlelement.rs index 1cc9f5a..b393682 100644 --- a/src/xmlelement.rs +++ b/src/xmlelement.rs @@ -1,6 +1,6 @@ use std::io::Write; -use crate::{escape_str, Result, XMLElementContent, XMLError}; +use crate::{Result, XMLElementContent, XMLError, escape_str}; /// Structure representing an XML element field. #[derive(Clone)] @@ -73,7 +73,7 @@ impl XMLElement { XMLElementContent::Text(_) => { return Err(XMLError::InsertError( "Cannot insert child inside an element with text".into(), - )) + )); } }; @@ -95,7 +95,7 @@ impl XMLElement { _ => { return Err(XMLError::InsertError( "Cannot insert text in a non-empty element".into(), - )) + )); } }; diff --git a/tests/tests.rs b/tests/tests.rs index 9c494e1..0428886 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -22,7 +22,7 @@ fn test_xml_version() { let mut writer: Vec = Vec::new(); xml.generate(&mut writer).unwrap(); - let expected = "\n"; + let expected = "\n"; let res = std::str::from_utf8(&writer).unwrap(); assert_eq!(res, expected, "Both values does not match..."); @@ -57,7 +57,7 @@ fn test_indent() { let mut writer: Vec = Vec::new(); xml.generate(&mut writer).unwrap(); - let expected = " + let expected = " @@ -80,7 +80,7 @@ fn test_line_breaks() { let mut writer: Vec = Vec::new(); xml.generate(&mut writer).unwrap(); - let expected = "\t"; + let expected = "\t"; let res = std::str::from_utf8(&writer).unwrap(); assert_eq!(res, expected, "Both values does not match..."); @@ -100,8 +100,7 @@ fn test_expand_empty_tags() { let mut writer: Vec = Vec::new(); xml.generate(&mut writer).unwrap(); - let expected = - "\n\n\t\n\n"; + let expected = "\n\n\t\n\n"; let res = std::str::from_utf8(&writer).unwrap(); assert_eq!(res, expected, "Both values does not match..."); @@ -114,7 +113,7 @@ fn test_xml_version_1_0() { let mut writer: Vec = Vec::new(); xml.generate(&mut writer).unwrap(); - let expected = "\n"; + let expected = "\n"; let res = std::str::from_utf8(&writer).unwrap(); assert_eq!(res, expected, "Both values does not match..."); @@ -127,7 +126,7 @@ fn test_xml_version_1_1() { let mut writer: Vec = Vec::new(); xml.generate(&mut writer).unwrap(); - let expected = "\n"; + let expected = "\n"; let res = std::str::from_utf8(&writer).unwrap(); assert_eq!(res, expected, "Both values does not match...");