https://github.com/poiscript/strong-xml
Strong typed xml, based on xmlparser.
https://github.com/poiscript/strong-xml
Last synced: 10 months ago
JSON representation
Strong typed xml, based on xmlparser.
- Host: GitHub
- URL: https://github.com/poiscript/strong-xml
- Owner: PoiScript
- License: mit
- Created: 2020-02-20T09:56:20.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-05-16T21:10:24.000Z (over 3 years ago)
- Last Synced: 2025-02-27T16:19:20.527Z (11 months ago)
- Language: Rust
- Homepage:
- Size: 172 KB
- Stars: 33
- Watchers: 4
- Forks: 12
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# strong-xml
Strong typed xml, based on xmlparser.
[](https://github.com/PoiScript/strong-xml/actions?query=workflow%3ATest)
[](https://crates.io/crates/strong-xml)
[](https://docs.rs/strong-xml)
### Quick Start
```toml
strong-xml = "0.6"
```
```rust
use std::borrow::Cow;
use strong_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>,
}
assert_eq!(
(Parent { attr1: "val".into(), attr2: None, child: vec![] }).to_string().unwrap(),
r#""#
);
assert_eq!(
Parent::from_str(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 {}
assert_eq!(
(Parent {}).to_string().unwrap(),
r#""#
);
assert_eq!(
Parent::from_str(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),
}
assert_eq!(
(Tag::Tag1(Tag1 {})).to_string().unwrap(),
r#""#
);
assert_eq!(
Tag::from_str(r#""#).unwrap(),
Tag::Tag2(Tag2 {})
);
```
#### `#[xml(attr = "")]`
Specifies that a struct field is attribute. Support
`Cow`, `Option>`, `T` and `Option`
where `T: FromStr + Display`.
```rust
use strong_xml::{XmlRead, XmlWrite};
#[derive(XmlWrite, XmlRead, PartialEq, Debug)]
#[xml(tag = "parent")]
struct Parent {
#[xml(attr = "attr")]
attr: usize
}
assert_eq!(
(Parent { attr: 42 }).to_string().unwrap(),
r#""#
);
assert_eq!(
Parent::from_str(r#""#).unwrap(),
Parent { attr: 48 }
);
```
#### `#[xml(child = "")]`
Specifies that a struct field is a child element. Support
`T`, `Option`, `Vec` where `T: XmlRead + XmlWrite`.
```rust
use strong_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
}
assert_eq!(
(Parent { tag3: vec![Tag3 {}], tag12: None }).to_string().unwrap(),
r#""#
);
assert_eq!(
Parent::from_str(r#""#).unwrap(),
Parent { tag3: vec![], tag12: Some(Tag12::Tag2(Tag2 {})) }
);
```
#### `#[xml(text)]`
Specifies that a struct field is text content.
Support `Cow`, `Vec>`, `Option>`,
`T`, `Vec`, `Option` where `T: FromStr + Display`.
```rust
use std::borrow::Cow;
use strong_xml::{XmlRead, XmlWrite};
#[derive(XmlWrite, XmlRead, PartialEq, Debug)]
#[xml(tag = "parent")]
struct Parent<'a> {
#[xml(text)]
content: Cow<'a, str>,
}
assert_eq!(
(Parent { content: "content".into() }).to_string().unwrap(),
r#"content"#
);
assert_eq!(
Parent::from_str(r#""#).unwrap(),
Parent { content: "".into() }
);
```
#### `#[xml(flatten_text = "")]`
Specifies that a struct field is child text element.
Support `Cow`, `Vec>`, `Option>`,
`T`, `Vec`, `Option` where `T: FromStr + Display`.
```rust
use std::borrow::Cow;
use strong_xml::{XmlRead, XmlWrite};
#[derive(XmlWrite, XmlRead, PartialEq, Debug)]
#[xml(tag = "parent")]
struct Parent<'a> {
#[xml(flatten_text = "child")]
content: Cow<'a, str>,
}
assert_eq!(
(Parent { content: "content".into() }).to_string().unwrap(),
r#"content"#
);
assert_eq!(
Parent::from_str(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 strong_xml::{XmlRead, XmlWrite};
#[derive(XmlWrite, XmlRead, PartialEq, Debug)]
#[xml(tag = "parent")]
struct Parent<'a> {
#[xml(text, cdata)]
content: Cow<'a, str>,
}
assert_eq!(
(Parent { content: "content".into() }).to_string().unwrap(),
r#""#
);
```
```rust
use std::borrow::Cow;
use strong_xml::{XmlRead, XmlWrite};
#[derive(XmlWrite, XmlRead, PartialEq, Debug)]
#[xml(tag = "parent")]
struct Parent<'a> {
#[xml(flatten_text = "code", cdata)]
content: Cow<'a, str>,
}
assert_eq!(
(Parent { content: r#"hello("deities!");"#.into() }).to_string().unwrap(),
r#""#
);
```
#### `#[xml(default)]`
Use `Default::default()` if the value is not present when reading.
```rust
use std::borrow::Cow;
use strong_xml::XmlRead;
#[derive(XmlRead, PartialEq, Debug)]
#[xml(tag = "root")]
struct Root {
#[xml(default, attr = "attr")]
attr: bool,
}
assert_eq!(
Root::from_str(r#""#).unwrap(),
Root { attr: false }
);
assert_eq!(
Root::from_str(r#""#).unwrap(),
Root { attr: true }
);
```
### License
MIT
License: MIT