1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) 2024 Jonas Schäfer <jonas@zombofant.net>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

//! Handling of structs

use proc_macro2::TokenStream;
use quote::quote;
use syn::*;

use crate::compound::Compound;
use crate::meta::{NameRef, NamespaceRef, XmlCompoundMeta};

/// Parts necessary to construct a `::xso::FromXml` implementation.
pub(crate) struct FromXmlParts {
    /// Additional items necessary for the implementation.
    pub(crate) defs: TokenStream,

    /// The body of the `::xso::FromXml::from_xml` function.
    pub(crate) from_events_body: TokenStream,

    /// The name of the type which is the `::xso::FromXml::Builder`.
    pub(crate) builder_ty_ident: Ident,
}

/// Parts necessary to construct a `::xso::IntoXml` implementation.
pub(crate) struct IntoXmlParts {
    /// Additional items necessary for the implementation.
    pub(crate) defs: TokenStream,

    /// The body of the `::xso::IntoXml::into_event_iter` function.
    pub(crate) into_event_iter_body: TokenStream,

    /// The name of the type which is the `::xso::IntoXml::EventIter`.
    pub(crate) event_iter_ty_ident: Ident,
}

/// Definition of a struct and how to parse it.
pub(crate) struct StructDef {
    /// The XML namespace of the element to map the struct to.
    namespace: NamespaceRef,

    /// The XML name of the element to map the struct to.
    name: NameRef,

    /// The field(s) of this struct.
    inner: Compound,

    /// Name of the target type.
    target_ty_ident: Ident,

    /// Name of the builder type.
    builder_ty_ident: Ident,

    /// Name of the iterator type.
    event_iter_ty_ident: Ident,
}

impl StructDef {
    /// Create a new struct from its name, meta, and fields.
    pub(crate) fn new(ident: &Ident, meta: XmlCompoundMeta, fields: &Fields) -> Result<Self> {
        let Some(namespace) = meta.namespace else {
            return Err(Error::new(meta.span, "`namespace` is required on structs"));
        };

        let Some(name) = meta.name else {
            return Err(Error::new(meta.span, "`name` is required on structs"));
        };

        Ok(Self {
            namespace,
            name,
            inner: Compound::from_fields(fields)?,
            target_ty_ident: ident.clone(),
            builder_ty_ident: quote::format_ident!("{}FromXmlBuilder", ident),
            event_iter_ty_ident: quote::format_ident!("{}IntoXmlIterator", ident),
        })
    }

    pub(crate) fn make_from_events_builder(
        &self,
        vis: &Visibility,
        name_ident: &Ident,
        attrs_ident: &Ident,
    ) -> Result<FromXmlParts> {
        let xml_namespace = &self.namespace;
        let xml_name = &self.name;

        let target_ty_ident = &self.target_ty_ident;
        let builder_ty_ident = &self.builder_ty_ident;
        let state_ty_ident = quote::format_ident!("{}State", builder_ty_ident);

        let defs = self
            .inner
            .make_from_events_statemachine(
                &state_ty_ident,
                &Path::from(target_ty_ident.clone()).into(),
                "Struct",
            )?
            .with_augmented_init(|init| {
                quote! {
                    if name.0 != #xml_namespace || name.1 != #xml_name {
                        ::core::result::Result::Err(::xso::error::FromEventsError::Mismatch {
                            name,
                            attrs,
                        })
                    } else {
                        #init
                    }
                }
            })
            .compile()
            .render(
                vis,
                &builder_ty_ident,
                &state_ty_ident,
                &TypePath {
                    qself: None,
                    path: target_ty_ident.clone().into(),
                }
                .into(),
            )?;

        Ok(FromXmlParts {
            defs,
            from_events_body: quote! {
                #builder_ty_ident::new(#name_ident, #attrs_ident)
            },
            builder_ty_ident: builder_ty_ident.clone(),
        })
    }

    pub(crate) fn make_into_event_iter(&self, vis: &Visibility) -> Result<IntoXmlParts> {
        let xml_namespace = &self.namespace;
        let xml_name = &self.name;

        let target_ty_ident = &self.target_ty_ident;
        let event_iter_ty_ident = &self.event_iter_ty_ident;
        let state_ty_ident = quote::format_ident!("{}State", event_iter_ty_ident);

        let defs = self
            .inner
            .make_into_event_iter_statemachine(&target_ty_ident.clone().into(), "Struct")?
            .with_augmented_init(|init| {
                quote! {
                    let name = (
                        ::xso::exports::rxml::Namespace::from(#xml_namespace),
                        #xml_name.into(),
                    );
                    #init
                }
            })
            .compile()
            .render(
                vis,
                &TypePath {
                    qself: None,
                    path: target_ty_ident.clone().into(),
                }
                .into(),
                &state_ty_ident,
                &event_iter_ty_ident,
            )?;

        Ok(IntoXmlParts {
            defs,
            into_event_iter_body: quote! {
                #event_iter_ty_ident::new(self)
            },
            event_iter_ty_ident: event_iter_ty_ident.clone(),
        })
    }
}