xso_proc/
common.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//! Definitions common to both enums and structs
8
9use proc_macro2::TokenStream;
10use syn::*;
11
12/// Parts necessary to construct a `::xso::FromXml` implementation.
13pub(crate) struct FromXmlParts {
14    /// Additional items necessary for the implementation.
15    pub(crate) defs: TokenStream,
16
17    /// The body of the `::xso::FromXml::from_xml` function.
18    pub(crate) from_events_body: TokenStream,
19
20    /// The name of the type which is the `::xso::FromXml::Builder`.
21    pub(crate) builder_ty_ident: Ident,
22}
23
24/// Parts necessary to construct a `::xso::AsXml` implementation.
25pub(crate) struct AsXmlParts {
26    /// Additional items necessary for the implementation.
27    pub(crate) defs: TokenStream,
28
29    /// The body of the `::xso::AsXml::as_xml_iter` function.
30    pub(crate) as_xml_iter_body: TokenStream,
31
32    /// The type which is the `::xso::AsXml::ItemIter`.
33    pub(crate) item_iter_ty: Type,
34
35    /// The lifetime name used in `item_iter_ty`.
36    pub(crate) item_iter_ty_lifetime: Lifetime,
37}
38
39/// Trait describing the definition of the XML (de-)serialisation for an item
40/// (enum or struct).
41pub(crate) trait ItemDef {
42    /// Construct the parts necessary for the caller to build an
43    /// `xso::FromXml` implementation for the item.
44    fn make_from_events_builder(
45        &self,
46        vis: &Visibility,
47        name_ident: &Ident,
48        attrs_ident: &Ident,
49    ) -> Result<FromXmlParts>;
50
51    /// Construct the parts necessary for the caller to build an `xso::AsXml`
52    /// implementation for the item.
53    fn make_as_xml_iter(&self, vis: &Visibility) -> Result<AsXmlParts>;
54
55    /// Return true iff the user requested debug output.
56    fn debug(&self) -> bool;
57}