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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// 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 the insides of compound structures (structs and enum variants)

use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::*;

use crate::error_message::ParentRef;
use crate::field::{FieldBuilderPart, FieldDef, FieldIteratorPart, FieldTempInit};
use crate::scope::{mangle_member, FromEventsScope, IntoEventsScope};
use crate::state::{FromEventsSubmachine, IntoEventsSubmachine, State};
use crate::types::qname_ty;

/// A struct or enum variant's contents.
pub(crate) struct Compound {
    /// The fields of this compound.
    fields: Vec<FieldDef>,
}

impl Compound {
    /// Construct a compound from fields.
    pub(crate) fn from_fields(compound_fields: &Fields) -> Result<Self> {
        let mut fields = Vec::with_capacity(compound_fields.len());
        for (i, field) in compound_fields.iter().enumerate() {
            let index = match i.try_into() {
                Ok(v) => v,
                // we are converting to u32, are you crazy?!
                // (u32, because syn::Member::Index needs that.)
                Err(_) => {
                    return Err(Error::new_spanned(
                        field,
                        "okay, mate, that are way too many fields. get your life together.",
                    ))
                }
            };
            fields.push(FieldDef::from_field(field, index)?);
        }

        Ok(Self { fields })
    }

    /// Make and return a set of states which is used to construct the target
    /// type from XML events.
    ///
    /// The states are returned as partial state machine. See the return
    /// type's documentation for details.
    pub(crate) fn make_from_events_statemachine(
        &self,
        state_ty_ident: &Ident,
        output_name: &ParentRef,
        state_prefix: &str,
    ) -> Result<FromEventsSubmachine> {
        let scope = FromEventsScope::new();
        let FromEventsScope { ref attrs, .. } = scope;

        let default_state_ident = quote::format_ident!("{}Default", state_prefix);
        let builder_data_ident = quote::format_ident!("__data");
        let builder_data_ty: Type = TypePath {
            qself: None,
            path: quote::format_ident!("{}Data{}", state_ty_ident, state_prefix).into(),
        }
        .into();
        let mut states = Vec::new();

        let mut builder_data_def = TokenStream::default();
        let mut builder_data_init = TokenStream::default();
        let mut output_cons = TokenStream::default();

        for field in self.fields.iter() {
            let member = field.member();
            let builder_field_name = mangle_member(member);
            let part = field.make_builder_part(&scope, &output_name)?;

            match part {
                FieldBuilderPart::Init {
                    value: FieldTempInit { ty, init },
                } => {
                    builder_data_def.extend(quote! {
                        #builder_field_name: #ty,
                    });

                    builder_data_init.extend(quote! {
                        #builder_field_name: #init,
                    });

                    output_cons.extend(quote! {
                        #member: #builder_data_ident.#builder_field_name,
                    });
                }
            }
        }

        let unknown_attr_err = format!("Unknown attribute in {}.", output_name);
        let unknown_child_err = format!("Unknown child in {}.", output_name);

        let output_cons = match output_name {
            ParentRef::Named(ref path) => {
                quote! {
                    #path { #output_cons }
                }
            }
        };

        states.push(State::new_with_builder(
            default_state_ident.clone(),
            &builder_data_ident,
            &builder_data_ty,
        ).with_impl(quote! {
            match ev {
                // EndElement in Default state -> done parsing.
                ::xso::exports::rxml::Event::EndElement(_) => {
                    ::core::result::Result::Ok(::std::ops::ControlFlow::Continue(
                        #output_cons
                    ))
                }
                ::xso::exports::rxml::Event::StartElement(..) => {
                    ::core::result::Result::Err(::xso::error::Error::Other(#unknown_child_err))
                }
                ::xso::exports::rxml::Event::Text(..) => {
                    ::core::result::Result::Err(::xso::error::Error::Other("Unexpected text content".into()))
                }
                // we ignore these: a correct parser only generates
                // them at document start, and there we want to indeed
                // not worry about them being in front of the first
                // element.
                ::xso::exports::rxml::Event::XmlDeclaration(_, ::xso::exports::rxml::XmlVersion::V1_0) => ::core::result::Result::Ok(::std::ops::ControlFlow::Break(
                    Self::#default_state_ident { #builder_data_ident }
                ))
            }
        }));

        Ok(FromEventsSubmachine {
            defs: quote! {
                struct #builder_data_ty {
                    #builder_data_def
                }
            },
            states,
            init: quote! {
                let #builder_data_ident = #builder_data_ty {
                    #builder_data_init
                };
                if #attrs.len() > 0 {
                    return ::core::result::Result::Err(::xso::error::Error::Other(
                        #unknown_attr_err,
                    ).into());
                }
                ::core::result::Result::Ok(#state_ty_ident::#default_state_ident { #builder_data_ident })
            },
        })
    }

    /// Make and return a set of states which is used to destructure the
    /// target type into XML events.
    ///
    /// The states are returned as partial state machine. See the return
    /// type's documentation for details.
    ///
    /// **Important:** The returned submachine is not in functional state!
    /// It's `init` must be modified so that a variable called `name` of type
    /// `rxml::QName` is in scope.
    pub(crate) fn make_into_event_iter_statemachine(
        &self,
        input_name: &Path,
        state_prefix: &str,
    ) -> Result<IntoEventsSubmachine> {
        let scope = IntoEventsScope::new();
        let IntoEventsScope { ref attrs, .. } = scope;

        let start_element_state_ident = quote::format_ident!("{}StartElement", state_prefix);
        let end_element_state_ident = quote::format_ident!("{}EndElement", state_prefix);
        let name_ident = quote::format_ident!("name");
        let mut states = Vec::new();

        let mut init_body = TokenStream::default();
        let mut destructure = TokenStream::default();
        let mut start_init = TokenStream::default();

        states.push(
            State::new(start_element_state_ident.clone())
                .with_field(&name_ident, &qname_ty(Span::call_site())),
        );

        for field in self.fields.iter() {
            let member = field.member();
            let bound_name = mangle_member(member);
            let part = field.make_iterator_part(&scope, &bound_name)?;

            match part {
                FieldIteratorPart::Header { setter } => {
                    destructure.extend(quote! {
                        #member: #bound_name,
                    });
                    init_body.extend(setter);
                    start_init.extend(quote! {
                        #bound_name,
                    });
                    states[0].add_field(&bound_name, field.ty());
                }
            }
        }

        states[0].set_impl(quote! {
            {
                let mut #attrs = ::xso::exports::rxml::AttrMap::new();
                #init_body
                ::core::option::Option::Some(::xso::exports::rxml::Event::StartElement(
                    ::xso::exports::rxml::parser::EventMetrics::zero(),
                    #name_ident,
                    #attrs,
                ))
            }
        });

        states.push(
            State::new(end_element_state_ident.clone()).with_impl(quote! {
                ::core::option::Option::Some(::xso::exports::rxml::Event::EndElement(
                    ::xso::exports::rxml::parser::EventMetrics::zero(),
                ))
            }),
        );

        Ok(IntoEventsSubmachine {
            defs: TokenStream::default(),
            states,
            destructure: quote! {
                #input_name { #destructure }
            },
            init: quote! {
                Self::#start_element_state_ident { #name_ident, #start_init }
            },
        })
    }
}