1#![forbid(unsafe_code)]
8#![warn(missing_docs)]
9#![allow(rustdoc::private_intra_doc_links)]
10#![cfg_attr(docsrs, feature(doc_auto_cfg))]
11use proc_macro::TokenStream as RawTokenStream;
25use proc_macro2::{Span, TokenStream};
26use quote::quote;
27use syn::*;
28
29mod common;
30mod compound;
31mod enums;
32mod error_message;
33mod field;
34mod meta;
35mod scope;
36mod state;
37mod structs;
38mod types;
39
40use common::{AsXmlParts, FromXmlParts, ItemDef};
41
42fn parse_struct(item: Item) -> Result<(Visibility, Ident, Box<dyn ItemDef>)> {
47 match item {
48 Item::Struct(item) => {
49 let meta = meta::XmlCompoundMeta::parse_from_attributes(&item.attrs)?;
50 let def = structs::StructDef::new(&item.ident, meta, &item.fields)?;
51 Ok((item.vis, item.ident, Box::new(def)))
52 }
53 Item::Enum(item) => {
54 let meta = meta::XmlCompoundMeta::parse_from_attributes(&item.attrs)?;
55 let def = enums::EnumDef::new(&item.ident, meta, &item.variants)?;
56 Ok((item.vis, item.ident, Box::new(def)))
57 }
58 other => Err(Error::new_spanned(other, "cannot derive on this item")),
59 }
60}
61
62fn from_xml_impl(input: Item) -> Result<TokenStream> {
65 let (vis, ident, def) = parse_struct(input)?;
66
67 let name_ident = Ident::new("name", Span::call_site());
68 let attrs_ident = Ident::new("attrs", Span::call_site());
69
70 let FromXmlParts {
71 defs,
72 from_events_body,
73 builder_ty_ident,
74 } = def.make_from_events_builder(&vis, &name_ident, &attrs_ident)?;
75
76 #[cfg_attr(not(feature = "minidom"), allow(unused_mut))]
77 let mut result = quote! {
78 #defs
79
80 impl ::xso::FromXml for #ident {
81 type Builder = #builder_ty_ident;
82
83 fn from_events(
84 name: ::xso::exports::rxml::QName,
85 attrs: ::xso::exports::rxml::AttrMap,
86 ) -> ::core::result::Result<Self::Builder, ::xso::error::FromEventsError> {
87 #from_events_body
88 }
89 }
90 };
91
92 #[cfg(feature = "minidom")]
93 result.extend(quote! {
94 impl ::core::convert::TryFrom<::xso::exports::minidom::Element> for #ident {
95 type Error = ::xso::error::FromElementError;
96
97 fn try_from(other: ::xso::exports::minidom::Element) -> ::core::result::Result<Self, Self::Error> {
98 ::xso::try_from_element(other)
99 }
100 }
101 });
102
103 if def.debug() {
104 println!("{}", result);
105 }
106
107 Ok(result)
108}
109
110#[proc_macro_derive(FromXml, attributes(xml))]
114pub fn from_xml(input: RawTokenStream) -> RawTokenStream {
115 let item = syn::parse_macro_input!(input as Item);
118 match from_xml_impl(item) {
119 Ok(v) => v.into(),
120 Err(e) => e.into_compile_error().into(),
121 }
122}
123
124fn as_xml_impl(input: Item) -> Result<TokenStream> {
127 let (vis, ident, def) = parse_struct(input)?;
128
129 let AsXmlParts {
130 defs,
131 as_xml_iter_body,
132 item_iter_ty_lifetime,
133 item_iter_ty,
134 } = def.make_as_xml_iter(&vis)?;
135
136 #[cfg_attr(not(feature = "minidom"), allow(unused_mut))]
137 let mut result = quote! {
138 #defs
139
140 impl ::xso::AsXml for #ident {
141 type ItemIter<#item_iter_ty_lifetime> = #item_iter_ty;
142
143 fn as_xml_iter(&self) -> ::core::result::Result<Self::ItemIter<'_>, ::xso::error::Error> {
144 #as_xml_iter_body
145 }
146 }
147 };
148
149 #[cfg(all(feature = "minidom", feature = "panicking-into-impl"))]
150 result.extend(quote! {
151 impl ::core::convert::From<#ident> for ::xso::exports::minidom::Element {
152 fn from(other: #ident) -> Self {
153 ::xso::transform(&other).expect("seamless conversion into minidom::Element")
154 }
155 }
156
157 impl ::core::convert::From<&#ident> for ::xso::exports::minidom::Element {
158 fn from(other: &#ident) -> Self {
159 ::xso::transform(other).expect("seamless conversion into minidom::Element")
160 }
161 }
162 });
163
164 #[cfg(all(feature = "minidom", not(feature = "panicking-into-impl")))]
165 result.extend(quote! {
166 impl ::core::convert::TryFrom<#ident> for ::xso::exports::minidom::Element {
167 type Error = ::xso::error::Error;
168
169 fn try_from(other: #ident) -> ::core::result::Result<Self, Self::Error> {
170 ::xso::transform(&other)
171 }
172 }
173 impl ::core::convert::TryFrom<&#ident> for ::xso::exports::minidom::Element {
174 type Error = ::xso::error::Error;
175
176 fn try_from(other: &#ident) -> ::core::result::Result<Self, Self::Error> {
177 ::xso::transform(other)
178 }
179 }
180 });
181
182 if def.debug() {
183 println!("{}", result);
184 }
185
186 Ok(result)
187}
188
189#[proc_macro_derive(AsXml, attributes(xml))]
193pub fn as_xml(input: RawTokenStream) -> RawTokenStream {
194 let item = syn::parse_macro_input!(input as Item);
197 match as_xml_impl(item) {
198 Ok(v) => v.into(),
199 Err(e) => e.into_compile_error().into(),
200 }
201}