Skip to content

Commit

Permalink
Add quick_xml1
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Jan 11, 2023
1 parent 617c2aa commit 99942d8
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions quick_xml1/.gitignore
@@ -0,0 +1 @@
/target/
32 changes: 32 additions & 0 deletions quick_xml1/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions quick_xml1/Cargo.toml
@@ -0,0 +1,12 @@
[package]
name = "quick_xml1"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
quick-xml = "0.27.1"

[dev-dependencies]
anyhow = "1.0.68"
39 changes: 39 additions & 0 deletions quick_xml1/src/main.rs
@@ -0,0 +1,39 @@
fn main() {
println!("Hello, world!");
}

#[cfg(test)]
mod tests {
use std::io::Cursor;

use quick_xml::events::{BytesDecl, BytesEnd, BytesStart, BytesText, Event};
use quick_xml::writer::Writer;

#[test]
fn test_quick_xml() -> anyhow::Result<()> {
let mut writer = Writer::new(Cursor::new(Vec::new()));
writer.write_event(Event::Decl(BytesDecl::new("1.0", Some("UTF-8"), None)))?;
let mut elm = BytesStart::new("urlset");
elm.push_attribute(("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"));
writer.write_event(Event::Start(elm))?;

writer.write_event(Event::Start(BytesStart::new("url")))?;
writer.write_event(Event::Start(BytesStart::new("loc")))?;
writer.write_event(Event::Text(BytesText::new("http://www.example.com/")))?;
writer.write_event(Event::End(BytesEnd::new("loc")))?;
writer.write_event(Event::End(BytesEnd::new("url")))?;

writer.write_event(Event::End(BytesEnd::new("urlset")))?;
let result = writer.into_inner().into_inner();
let expected = concat!(
r#"<?xml version="1.0" encoding="UTF-8"?>"#,
r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
r#"<url>"#,
r#"<loc>http://www.example.com/</loc>"#,
r#"</url>"#,
r#"</urlset>"#
);
assert_eq!(result, expected.as_bytes());
Ok(())
}
}

0 comments on commit 99942d8

Please sign in to comment.