1#![recursion_limit = "256"]
2
3extern crate proc_macro;
4
5mod read;
6mod types;
7mod utils;
8mod write;
9
10use proc_macro::TokenStream;
11use quote::quote;
12use syn::{parse_macro_input, DeriveInput};
13use types::Element;
14
15#[proc_macro_derive(XmlRead, attributes(xml))]
16pub fn derive_xml_read(input: TokenStream) -> TokenStream {
17 let input = parse_macro_input!(input as DeriveInput);
18
19 let name = &input.ident;
20 let generics = &input.generics;
21
22 let params = &generics.params;
23
24 let where_clause = &generics.where_clause;
25
26 let input_lifetime = utils::gen_input_lifetime(&generics);
27
28 let mut params_with_input_lifetime = generics.params.clone();
29
30 params_with_input_lifetime.insert(0, input_lifetime.into());
31
32 let impl_read = read::impl_read(Element::parse(input.clone()));
33
34 let gen = quote! {
35 impl <#params_with_input_lifetime> strong_xml::XmlRead<'__input> for #name <#params>
36 #where_clause
37 {
38 fn from_reader(
39 mut reader: &mut strong_xml::XmlReader<'__input>
40 ) -> strong_xml::XmlResult<Self> {
41 use strong_xml::xmlparser::{ElementEnd, Token, Tokenizer};
42 use strong_xml::XmlError;
43 #impl_read
44 }
45 }
46 };
47
48 gen.into()
49}
50
51#[proc_macro_derive(XmlWrite, attributes(xml))]
52pub fn derive_xml_write(input: TokenStream) -> TokenStream {
53 let input = parse_macro_input!(input as DeriveInput);
54
55 let name = &input.ident;
56 let generics = &input.generics;
57
58 let params = &generics.params;
59
60 let where_clause = &generics.where_clause;
61
62 let impl_write = write::impl_write(Element::parse(input.clone()));
63
64 let gen = quote! {
65 impl <#params> strong_xml::XmlWrite for #name <#params>
66 #where_clause
67 {
68 fn to_writer<W: std::io::Write>(
69 &self,
70 mut writer: &mut strong_xml::XmlWriter<W>
71 ) -> strong_xml::XmlResult<()> {
72 #impl_write
73
74 Ok(())
75 }
76 }
77 };
78
79 gen.into()
80}