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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["LIAUD Corentin <corentinliaud26@gmail.com>"]
categories = ["data-structures"]
description = "Easy and highly-configurable XML builder/writer"
edition = "2021"
edition = "2024"
keywords = ["xml"]
license = "MIT"
name = "xml-builder"
Expand Down
8 changes: 4 additions & 4 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{XMLVersion, XML};
use crate::{XML, XMLVersion};

/// Builder structure used to generate a custom XML structure.
pub struct XMLBuilder {
Expand All @@ -10,7 +10,7 @@ pub struct XMLBuilder {
/// The encoding to set for the document.
///
/// Defaults to `UTF-8`.
encoding: String,
encoding: Option<String>,

/// XML standalone attribute.
///
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
39 changes: 24 additions & 15 deletions src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/// XML standalone attribute.
///
Expand Down Expand Up @@ -49,7 +51,7 @@ pub struct XML {
impl XML {
pub(crate) fn new(
version: XMLVersion,
encoding: String,
encoding: Option<String>,
standalone: Option<bool>,
indent: bool,
sort_attributes: bool,
Expand Down Expand Up @@ -81,21 +83,28 @@ impl XML {
///
/// Consumes the XML object.
pub fn generate<W: Write>(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#"<?xml version="{}" encoding="{}"{}?>{}"#,
self.version, self.encoding, standalone_attribute, suffix
r#"<?xml version="{}"{encoding}{standalone}?>"#,
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(
Expand Down
6 changes: 3 additions & 3 deletions src/xmlelement.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -73,7 +73,7 @@ impl XMLElement {
XMLElementContent::Text(_) => {
return Err(XMLError::InsertError(
"Cannot insert child inside an element with text".into(),
))
));
}
};

Expand All @@ -95,7 +95,7 @@ impl XMLElement {
_ => {
return Err(XMLError::InsertError(
"Cannot insert text in a non-empty element".into(),
))
));
}
};

Expand Down
13 changes: 6 additions & 7 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn test_xml_version() {
let mut writer: Vec<u8> = Vec::new();
xml.generate(&mut writer).unwrap();

let expected = "<?xml version=\"1.1\" encoding=\"UTF-8\"?>\n";
let expected = "<?xml version=\"1.1\"?>\n";
let res = std::str::from_utf8(&writer).unwrap();

assert_eq!(res, expected, "Both values does not match...");
Expand Down Expand Up @@ -57,7 +57,7 @@ fn test_indent() {
let mut writer: Vec<u8> = Vec::new();
xml.generate(&mut writer).unwrap();

let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
let expected = "<?xml version=\"1.0\"?>
<root>
<indentation />
<indentation />
Expand All @@ -80,7 +80,7 @@ fn test_line_breaks() {
let mut writer: Vec<u8> = Vec::new();
xml.generate(&mut writer).unwrap();

let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>\t<element /></root>";
let expected = "<?xml version=\"1.0\"?><root>\t<element /></root>";
let res = std::str::from_utf8(&writer).unwrap();

assert_eq!(res, expected, "Both values does not match...");
Expand All @@ -100,8 +100,7 @@ fn test_expand_empty_tags() {
let mut writer: Vec<u8> = Vec::new();
xml.generate(&mut writer).unwrap();

let expected =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n\t<element></element>\n</root>\n";
let expected = "<?xml version=\"1.0\"?>\n<root>\n\t<element></element>\n</root>\n";
let res = std::str::from_utf8(&writer).unwrap();

assert_eq!(res, expected, "Both values does not match...");
Expand All @@ -114,7 +113,7 @@ fn test_xml_version_1_0() {
let mut writer: Vec<u8> = Vec::new();
xml.generate(&mut writer).unwrap();

let expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
let expected = "<?xml version=\"1.0\"?>\n";
let res = std::str::from_utf8(&writer).unwrap();

assert_eq!(res, expected, "Both values does not match...");
Expand All @@ -127,7 +126,7 @@ fn test_xml_version_1_1() {
let mut writer: Vec<u8> = Vec::new();
xml.generate(&mut writer).unwrap();

let expected = "<?xml version=\"1.1\" encoding=\"UTF-8\"?>\n";
let expected = "<?xml version=\"1.1\"?>\n";
let res = std::str::from_utf8(&writer).unwrap();

assert_eq!(res, expected, "Both values does not match...");
Expand Down
Loading