4 个版本 (2 个破坏性版本)

0.3.0 2021年4月20日
0.2.1 2019年3月21日
0.2.0 2019年3月21日
0.1.0 2019年3月16日

#9 in #xml-element

Download history 478/week @ 2024-03-13 559/week @ 2024-03-20 357/week @ 2024-03-27 345/week @ 2024-04-03 297/week @ 2024-04-10 304/week @ 2024-04-17 332/week @ 2024-04-24 226/week @ 2024-05-01 287/week @ 2024-05-08 397/week @ 2024-05-15 361/week @ 2024-05-22 361/week @ 2024-05-29 486/week @ 2024-06-05 388/week @ 2024-06-12 401/week @ 2024-06-19 325/week @ 2024-06-26

1,645 个月下载量
用于 2 crates

MIT 许可证

23KB
417

simple_xml_serialize_macro

使用此 proc_macro crate 与 simple_xml_serialize 一起使用,允许用 #[xml_element("...")] 注释结构体,以生成到 XMLElement 的结构体 From 实现。单个字段用 sxs_type_attrsxs_type_empty_attrsxs_type_textsxs_type_elementsxs_type_multi_element 注释。未注释的字段将被忽略。

extern crate simple_xml_serialize;
extern crate simple_xml_serialize_macro;
use simple_xml_serialize::XMLElement;
use simple_xml_serialize_macro::xml_element;


#[xml_element("custom_name_here")]
struct MyPoint {
    #[sxs_type_attr(rename="lat")]
    latitude: f32,
    #[sxs_type_attr]
    lon: f32,
    #[sxs_type_attr]
    active: bool,
    #[sxs_type_element(rename="Identifier")]
    name: MyName,
}

#[xml_element("Name")]
struct MyName {
    #[sxs_type_text]
    val: String,
}

fn main() {
    let my_point = MyPoint {
        latitude: 43.38,
        lon: 60.11,
        active: true,
        name: MyName{val: "p1".to_string()},
    };
    let my_point_xml = XMLElement::from(my_point); // can also take refs `&my_point`
    let expected = r#"<custom_name_here lat="43.38" lon="60.11" active="true"><Identifier>p1</Identifier></custom_name_here>"#;
    assert_eq!(expected, my_point_xml.to_string());

    let expected = r#"<custom_name_here lat="43.38" lon="60.11" active="true">
  <Identifier>
    p1
  </Identifier>
</custom_name_here>"#;
    assert_eq!(expected, my_point_xml.to_string_pretty("\n","  "));
}

功能

还有一个功能 process_options 允许相同的代码在 Option 类型后面工作。由于生成代码有点棘手,我怀疑它可能太容易破坏,所以它后面有一个功能门。通过在您的 Cargo.toml 中添加 features = ["process_options"] 启用它。

use simple_xml_serialize::XMLElement;
use simple_xml_serialize_macro::xml_element;

#[xml_element("Employee")]
struct Person1 {
    #[sxs_type_attr(rename="Name")]
    name: String,
    #[sxs_type_attr]
    age: Option<u8>,
}

let person1 = Person1{name: "Robert".to_string(), age: None};
let expected = r#"<Employee Name="Robert"/>"#;
assert_eq!(XMLElement::from(&person1).to_string(), expected);


let person1 = Person1{name: "Robert".to_string(), age: Some(52)};
let expected = r#"<Employee Name="Robert" age="52"/>"#;
assert_eq!(XMLElement::from(&person1).to_string(), expected);

依赖项

~2MB
~46K SLoC