xso_proc/
lib.rs

1// Copyright (c) 2024 Jonas Schäfer <jonas@zombofant.net>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#![forbid(unsafe_code)]
8#![warn(missing_docs)]
9#![allow(rustdoc::private_intra_doc_links)]
10#![cfg_attr(docsrs, feature(doc_auto_cfg))]
11/*!
12# Macros for parsing XML into Rust structs, and vice versa
13
14**If you are a user of `xso_proc` or `xso`, please
15return to `xso` for more information**. The documentation of
16`xso_proc` is geared toward developers of `…_macros` and `…_core`.
17
18**You have been warned.**
19*/
20
21// Wondering about RawTokenStream vs. TokenStream?
22// syn mostly works with proc_macro2, while the proc macros themselves use
23// proc_macro.
24use 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
42/// Convert an [`syn::Item`] into the parts relevant for us.
43///
44/// If the item is of an unsupported variant, an appropriate error is
45/// returned.
46fn 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
62/// Generate a `xso::FromXml` implementation for the given item, or fail with
63/// a proper compiler error.
64fn 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                ctx: &::xso::Context<'_>,
87            ) -> ::core::result::Result<Self::Builder, ::xso::error::FromEventsError> {
88                #from_events_body
89            }
90        }
91    };
92
93    #[cfg(feature = "minidom")]
94    result.extend(quote! {
95        impl ::core::convert::TryFrom<::xso::exports::minidom::Element> for #ident {
96            type Error = ::xso::error::FromElementError;
97
98            fn try_from(other: ::xso::exports::minidom::Element) -> ::core::result::Result<Self, Self::Error> {
99                ::xso::try_from_element(other)
100            }
101        }
102    });
103
104    if def.debug() {
105        println!("{}", result);
106    }
107
108    Ok(result)
109}
110
111/// Macro to derive a `xso::FromXml` implementation on a type.
112///
113/// The user-facing documentation for this macro lives in the `xso` crate.
114#[proc_macro_derive(FromXml, attributes(xml))]
115pub fn from_xml(input: RawTokenStream) -> RawTokenStream {
116    // Shim wrapper around `from_xml_impl` which converts any errors into
117    // actual compiler errors within the resulting token stream.
118    let item = syn::parse_macro_input!(input as Item);
119    match from_xml_impl(item) {
120        Ok(v) => v.into(),
121        Err(e) => e.into_compile_error().into(),
122    }
123}
124
125/// Generate a `xso::AsXml` implementation for the given item, or fail with
126/// a proper compiler error.
127fn as_xml_impl(input: Item) -> Result<TokenStream> {
128    let (vis, ident, def) = parse_struct(input)?;
129
130    let AsXmlParts {
131        defs,
132        as_xml_iter_body,
133        item_iter_ty_lifetime,
134        item_iter_ty,
135    } = def.make_as_xml_iter(&vis)?;
136
137    #[cfg_attr(not(feature = "minidom"), allow(unused_mut))]
138    let mut result = quote! {
139        #defs
140
141        impl ::xso::AsXml for #ident {
142            type ItemIter<#item_iter_ty_lifetime> = #item_iter_ty;
143
144            fn as_xml_iter(&self) -> ::core::result::Result<Self::ItemIter<'_>, ::xso::error::Error> {
145                #as_xml_iter_body
146            }
147        }
148    };
149
150    #[cfg(all(feature = "minidom", feature = "panicking-into-impl"))]
151    result.extend(quote! {
152        impl ::core::convert::From<#ident> for ::xso::exports::minidom::Element {
153            fn from(other: #ident) -> Self {
154                ::xso::transform(&other).expect("seamless conversion into minidom::Element")
155            }
156        }
157
158        impl ::core::convert::From<&#ident> for ::xso::exports::minidom::Element {
159            fn from(other: &#ident) -> Self {
160                ::xso::transform(other).expect("seamless conversion into minidom::Element")
161            }
162        }
163    });
164
165    #[cfg(all(feature = "minidom", not(feature = "panicking-into-impl")))]
166    result.extend(quote! {
167        impl ::core::convert::TryFrom<#ident> for ::xso::exports::minidom::Element {
168            type Error = ::xso::error::Error;
169
170            fn try_from(other: #ident) -> ::core::result::Result<Self, Self::Error> {
171                ::xso::transform(&other)
172            }
173        }
174        impl ::core::convert::TryFrom<&#ident> for ::xso::exports::minidom::Element {
175            type Error = ::xso::error::Error;
176
177            fn try_from(other: &#ident) -> ::core::result::Result<Self, Self::Error> {
178                ::xso::transform(other)
179            }
180        }
181    });
182
183    if def.debug() {
184        println!("{}", result);
185    }
186
187    Ok(result)
188}
189
190/// Macro to derive a `xso::AsXml` implementation on a type.
191///
192/// The user-facing documentation for this macro lives in the `xso` crate.
193#[proc_macro_derive(AsXml, attributes(xml))]
194pub fn as_xml(input: RawTokenStream) -> RawTokenStream {
195    // Shim wrapper around `as_xml_impl` which converts any errors into
196    // actual compiler errors within the resulting token stream.
197    let item = syn::parse_macro_input!(input as Item);
198    match as_xml_impl(item) {
199        Ok(v) => v.into(),
200        Err(e) => e.into_compile_error().into(),
201    }
202}