Skip to content

Commit

Permalink
fix: now use std::io::Write for bounds instead of std::fmt::Write
Browse files Browse the repository at this point in the history
  • Loading branch information
LIAUD Corentin committed Aug 27, 2024
1 parent f1ece3e commit da23656
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "xml-builder"
version = "0.5.2"
authors = ["LIAUD Corentin <[email protected]>"]
authors = ["LIAUD Corentin <[email protected]>"]
categories = ["data-structures"]
description = "Easy and highly-configurable XML builder/writer"
edition = "2021"
keywords = ["xml"]
categories = ["data-structures"]
readme = "README.md"
license = "MIT"
edition = "2021"
repository = "https://github.com/cocool97/xml-builder"
name = "xml-builder"
readme = "README.md"
repository = "https://github.com/cocool97/xml-builder"
version = "0.5.3"
37 changes: 17 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![dependency status](https://deps.rs/repo/github/cocool97/xml-builder/status.svg)](https://deps.rs/repo/github/cocool97/xml-builder)
[![codecov](https://codecov.io/gh/cocool97/xml-builder/branch/master/graph/badge.svg?token=2PMZ6D9E5M)](https://codecov.io/gh/cocool97/xml-builder)

This crate allows you to easily create an XML file in a short time by building a highly-configurable object tree.
This crate allows you to easily create an XML file in a short time by building a highly-configurable object tree.

## Main advantages

Expand Down Expand Up @@ -40,28 +40,25 @@ xml-builder = "*"
```rust
use xml_builder::{XMLBuilder, XMLElement, XMLVersion};

fn main() {
let mut xml = XMLBuilder::new()
.version(XMLVersion::XML1_1)
.encoding("UTF-8".into())
.build();
let mut xml = XMLBuilder::new()
.version(XMLVersion::XML1_1)
.encoding("UTF-8".into())
.build();

let mut house = XMLElement::new("house");
house.add_attribute("rooms", "2");
let mut house = XMLElement::new("house");
house.add_attribute("rooms", "2");

for i in 1..=2 {
let mut room = XMLElement::new("room");
room.add_attribute("number", &i.to_string());
room.add_text(format!("This is room number {}", i)).unwrap();

house.add_child(room).unwrap();
}
for i in 1..=2 {
let mut room = XMLElement::new("room");
room.add_attribute("number", &i.to_string());
room.add_text(format!("This is room number {}", i)).unwrap();
house.add_child(room).unwrap();
}

xml.set_root_element(house);
xml.set_root_element(house);

let mut writer: Vec<u8> = Vec::new();
xml.generate(&mut writer).unwrap();
}
let mut writer: Vec<u8> = Vec::new();
xml.generate(&mut writer).unwrap();
```

This following XML content will then be displayed:
Expand All @@ -72,4 +69,4 @@ This following XML content will then be displayed:
<room number="1" price="42">This is room number 1</room>
<room number="2" price="84">This is room number 2</room>
</house>
```
```
4 changes: 1 addition & 3 deletions src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ impl XML {
writeln!(
writer,
r#"<?xml version="{}" encoding="{}"{}?>"#,
self.version.to_string(),
self.encoding,
standalone_attribute
self.version, self.encoding, standalone_attribute
)?;

// And then XML elements if present...
Expand Down
9 changes: 4 additions & 5 deletions src/xmlversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ pub enum XMLVersion {
XML1_1,
}

impl ToString for XMLVersion {
/// Converts the XMLVersion enum value into a string usable in the XML document
fn to_string(&self) -> String {
impl std::fmt::Display for XMLVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
XMLVersion::XML1_0 => "1.0".into(),
XMLVersion::XML1_1 => "1.1".into(),
XMLVersion::XML1_0 => write!(f, "1.0"),
XMLVersion::XML1_1 => write!(f, "1.1"),
}
}
}

0 comments on commit da23656

Please sign in to comment.