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/*!
11# Macros for parsing XML into Rust structs, and vice versa
12
13**If you are a user of `xso_proc` or `xso`, please
14return to `xso` for more information**. The documentation of
15`xso_proc` is geared toward developers of `…_macros` and `…_core`.
16
17**You have been warned.**
18*/
19
20// Wondering about RawTokenStream vs. TokenStream?
21// syn mostly works with proc_macro2, while the proc macros themselves use
22// proc_macro.
23use proc_macro::TokenStream as RawTokenStream;
24use proc_macro2::{Span, TokenStream};
25use quote::quote;
26use syn::*;
27
28mod compound;
29mod error_message;
30mod field;
31mod meta;
32mod scope;
33mod state;
34mod structs;
35mod types;
36
37/// Convert an [`syn::Item`] into the parts relevant for us.
38///
39/// If the item is of an unsupported variant, an appropriate error is
40/// returned.
41fn parse_struct(item: Item) -> Result<(Visibility, Ident, structs::StructDef)> {
42 match item {
43 Item::Struct(item) => {
44 let meta = meta::XmlCompoundMeta::parse_from_attributes(&item.attrs)?;
45 let def = structs::StructDef::new(&item.ident, meta, &item.fields)?;
46 Ok((item.vis, item.ident, def))
47 }
48 other => Err(Error::new_spanned(other, "cannot derive on this item")),
49 }
50}
51
52/// Generate a `xso::FromXml` implementation for the given item, or fail with
53/// a proper compiler error.
54fn from_xml_impl(input: Item) -> Result<TokenStream> {
55 let (vis, ident, def) = parse_struct(input)?;
56
57 let name_ident = Ident::new("name", Span::call_site());
58 let attrs_ident = Ident::new("attrs", Span::call_site());
59
60 let structs::FromXmlParts {
61 defs,
62 from_events_body,
63 builder_ty_ident,
64 } = def.make_from_events_builder(&vis, &name_ident, &attrs_ident)?;
65
66 #[cfg_attr(not(feature = "minidom"), allow(unused_mut))]
67 let mut result = quote! {
68 #defs
69
70 impl ::xso::FromXml for #ident {
71 type Builder = #builder_ty_ident;
72
73 fn from_events(
74 name: ::xso::exports::rxml::QName,
75 attrs: ::xso::exports::rxml::AttrMap,
76 ) -> ::core::result::Result<Self::Builder, ::xso::error::FromEventsError> {
77 #from_events_body
78 }
79 }
80 };
81
82 #[cfg(feature = "minidom")]
83 result.extend(quote! {
84 impl ::std::convert::TryFrom<::xso::exports::minidom::Element> for #ident {
85 type Error = ::xso::error::FromElementError;
86
87 fn try_from(other: ::xso::exports::minidom::Element) -> ::core::result::Result<Self, Self::Error> {
88 ::xso::try_from_element(other)
89 }
90 }
91 });
92
93 if def.debug() {
94 println!("{}", result);
95 }
96
97 Ok(result)
98}
99
100/// Macro to derive a `xso::FromXml` implementation on a type.
101///
102/// The user-facing documentation for this macro lives in the `xso` crate.
103#[proc_macro_derive(FromXml, attributes(xml))]
104pub fn from_xml(input: RawTokenStream) -> RawTokenStream {
105 // Shim wrapper around `from_xml_impl` which converts any errors into
106 // actual compiler errors within the resulting token stream.
107 let item = syn::parse_macro_input!(input as Item);
108 match from_xml_impl(item) {
109 Ok(v) => v.into(),
110 Err(e) => e.into_compile_error().into(),
111 }
112}
113
114/// Generate a `xso::IntoXml` implementation for the given item, or fail with
115/// a proper compiler error.
116fn into_xml_impl(input: Item) -> Result<TokenStream> {
117 let (vis, ident, def) = parse_struct(input)?;
118
119 let structs::IntoXmlParts {
120 defs,
121 into_event_iter_body,
122 event_iter_ty_ident,
123 } = def.make_into_event_iter(&vis)?;
124
125 #[cfg_attr(not(feature = "minidom"), allow(unused_mut))]
126 let mut result = quote! {
127 #defs
128
129 impl ::xso::IntoXml for #ident {
130 type EventIter = #event_iter_ty_ident;
131
132 fn into_event_iter(self) -> ::core::result::Result<Self::EventIter, ::xso::error::Error> {
133 #into_event_iter_body
134 }
135 }
136 };
137
138 #[cfg(all(feature = "minidom", feature = "panicking-into-impl"))]
139 result.extend(quote! {
140 impl ::std::convert::From<#ident> for ::xso::exports::minidom::Element {
141 fn from(other: #ident) -> Self {
142 ::xso::transform(other).expect("seamless conversion into minidom::Element")
143 }
144 }
145 });
146
147 #[cfg(all(feature = "minidom", not(feature = "panicking-into-impl")))]
148 result.extend(quote! {
149 impl ::std::convert::TryFrom<#ident> for ::xso::exports::minidom::Element {
150 type Error = ::xso::error::Error;
151
152 fn try_from(other: #ident) -> ::core::result::Result<Self, Self::Error> {
153 ::xso::transform(other)
154 }
155 }
156 });
157
158 if def.debug() {
159 println!("{}", result);
160 }
161
162 Ok(result)
163}
164
165/// Macro to derive a `xso::IntoXml` implementation on a type.
166///
167/// The user-facing documentation for this macro lives in the `xso` crate.
168#[proc_macro_derive(IntoXml, attributes(xml))]
169pub fn into_xml(input: RawTokenStream) -> RawTokenStream {
170 // Shim wrapper around `into_xml_impl` which converts any errors into
171 // actual compiler errors within the resulting token stream.
172 let item = syn::parse_macro_input!(input as Item);
173 match into_xml_impl(item) {
174 Ok(v) => v.into(),
175 Err(e) => e.into_compile_error().into(),
176 }
177}