hard-xml

Strong typed xml, based on xmlparser.

This is a fork of https://github.com/PoiScript/hard-xml as it has become unmaintained.

Crates.io Document

Quick Start

sh cargo add hard-xml

```rust use std::borrow::Cow; use hard_xml::{XmlRead, XmlWrite};

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

[xml(tag = "parent")]

struct Parent<'a> { #[xml(attr = "attr1")] attr1: Cow<'a, str>, #[xml(attr = "attr2")] attr2: Option>, #[xml(child = "child")] child: Vec>, }

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

[xml(tag = "child")]

struct Child<'a> { #[xml(text)] text: Cow<'a, str>, }

asserteq!( (Parent { attr1: "val".into(), attr2: None, child: vec![] }).tostring().unwrap(), r#""# );

asserteq!( Parent::fromstr(r#""#).unwrap(), Parent { attr1: "val".into(), attr2: Some("val".into()), child: vec![Child { text: "".into() }] } ); ```

Attributes

#[xml(tag = "")]

Specifies the xml tag of a struct or an enum variant.

```rust

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

[xml(tag = "parent")]

struct Parent {}

asserteq!( (Parent {}).tostring().unwrap(), r#""# );

asserteq!( Parent::fromstr(r#""#).unwrap(), Parent {} ); ```

```rust

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

[xml(tag = "tag1")]

struct Tag1 {}

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

[xml(tag = "tag2")]

struct Tag2 {}

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

enum Tag { #[xml(tag = "tag1")] Tag1(Tag1), #[xml(tag = "tag2")] Tag2(Tag2), }

asserteq!( (Tag::Tag1(Tag1 {})).tostring().unwrap(), r#""# );

asserteq!( Tag::fromstr(r#""#).unwrap(), Tag::Tag2(Tag2 {}) ); ```

#[xml(attr = "")]

Specifies that a struct field is attribute. Support Cow<str>, Option<Cow<str>>, T and Option<T> where T: FromStr + Display.

```rust use hard_xml::{XmlRead, XmlWrite};

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

[xml(tag = "parent")]

struct Parent { #[xml(attr = "attr")] attr: usize }

asserteq!( (Parent { attr: 42 }).tostring().unwrap(), r#""# );

asserteq!( Parent::fromstr(r#""#).unwrap(), Parent { attr: 48 } ); ```

#[xml(child = "")]

Specifies that a struct field is a child element. Support T, Option<T>, Vec<T> where T: XmlRead + XmlWrite.

```rust use hard_xml::{XmlRead, XmlWrite};

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

[xml(tag = "tag1")]

struct Tag1 {}

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

[xml(tag = "tag2")]

struct Tag2 {}

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

[xml(tag = "tag3")]

struct Tag3 {}

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

enum Tag12 { #[xml(tag = "tag1")] Tag1(Tag1), #[xml(tag = "tag2")] Tag2(Tag2), }

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

[xml(tag = "parent")]

struct Parent { #[xml(child = "tag3")] tag3: Vec, #[xml(child = "tag1", child = "tag2")] tag12: Option }

asserteq!( (Parent { tag3: vec![Tag3 {}], tag12: None }).tostring().unwrap(), r#""# );

asserteq!( Parent::fromstr(r#""#).unwrap(), Parent { tag3: vec![], tag12: Some(Tag12::Tag2(Tag2 {})) } ); ```

#[xml(text)]

Specifies that a struct field is text content. Support Cow<str>, Vec<Cow<str>>, Option<Cow<str>>, T, Vec<T>, Option<T> where T: FromStr + Display.

```rust use std::borrow::Cow; use hard_xml::{XmlRead, XmlWrite};

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

[xml(tag = "parent")]

struct Parent<'a> { #[xml(text)] content: Cow<'a, str>, }

asserteq!( (Parent { content: "content".into() }).tostring().unwrap(), r#"content"# );

asserteq!( Parent::fromstr(r#""#).unwrap(), Parent { content: "".into() } ); ```

#[xml(flatten_text = "")]

Specifies that a struct field is child text element. Support Cow<str>, Vec<Cow<str>>, Option<Cow<str>>, T, Vec<T>, Option<T> where T: FromStr + Display.

```rust use std::borrow::Cow; use hard_xml::{XmlRead, XmlWrite};

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

[xml(tag = "parent")]

struct Parent<'a> { #[xml(flatten_text = "child")] content: Cow<'a, str>, }

asserteq!( (Parent { content: "content".into() }).tostring().unwrap(), r#"content"# );

asserteq!( Parent::fromstr(r#""#).unwrap(), Parent { content: "".into() } ); ```

#[xml(cdata)]

Specifies a CDATA text. Should be used together with text or flatten_text.

#[xml(cdata)] only changes the behavior of writing, text field without #[xml(cdata)] can still works with cdata tag.

```rust use std::borrow::Cow; use hard_xml::{XmlRead, XmlWrite};

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

[xml(tag = "parent")]

struct Parent<'a> { #[xml(text, cdata)] content: Cow<'a, str>, }

asserteq!( (Parent { content: "content".into() }).tostring().unwrap(), r#""# ); ```

```rust use std::borrow::Cow; use hard_xml::{XmlRead, XmlWrite};

[derive(XmlWrite, XmlRead, PartialEq, Debug)]

[xml(tag = "parent")]

struct Parent<'a> { #[xml(flatten_text = "code", cdata)] content: Cow<'a, str>, }

asserteq!( (Parent { content: r#"hello("deities!");"#.into() }).tostring().unwrap(), r#""# ); ```

#[xml(default)]

Use Default::default() if the value is not present when reading.

```rust use std::borrow::Cow; use hard_xml::XmlRead;

[derive(XmlRead, PartialEq, Debug)]

[xml(tag = "root")]

struct Root { #[xml(default, attr = "attr")] attr: bool, }

asserteq!( Root::fromstr(r#""#).unwrap(), Root { attr: false } );

asserteq!( Root::fromstr(r#""#).unwrap(), Root { attr: true } ); ```

License

MIT

License: MIT