Skip to main content

xso_proc/field/
child.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//! This module concerns the processing of typed child elements.
8//!
9//! In particular, it provides both `#[xml(extract)]` and `#[xml(child)]`
10//! implementations in a single type.
11
12use proc_macro2::{Span, TokenStream};
13use quote::{quote, quote_spanned};
14use syn::{spanned::Spanned, *};
15
16use crate::common::GenericsInfo;
17use crate::compound::Compound;
18use crate::error_message::{self, ParentRef};
19use crate::meta::{AmountConstraint, Flag, NameRef, NamespaceRef};
20use crate::scope::{AsItemsScope, FromEventsScope};
21use crate::types::{
22    as_xml_iter_fn, as_xml_trait, default_fn, extend_fn, from_events_fn, from_xml_builder_ty,
23    from_xml_trait, into_iterator_into_iter_fn, into_iterator_item_ty, into_iterator_iter_ty,
24    item_iter_ty, option_as_xml_ty, option_ty, ref_ty,
25};
26
27use super::{Field, FieldBuilderPart, FieldIteratorPart, FieldTempInit, NestedMatcher};
28
29/// The field maps to a child
30pub(super) struct ChildField {
31    /// Flag indicating whether the value should be defaulted if the
32    /// child is absent.
33    pub(super) default_: Flag,
34
35    /// Number of child elements allowed.
36    pub(super) amount: AmountConstraint,
37
38    /// If set, the child element is not parsed as a field implementing
39    /// `FromXml` / `AsXml`, but instead its contents are extracted.
40    pub(super) extract: Option<ExtractDef>,
41}
42
43impl Field for ChildField {
44    fn make_builder_part(
45        &self,
46        scope: &FromEventsScope,
47        generics: &mut GenericsInfo,
48        container_name: &ParentRef,
49        member: &Member,
50        ty: &Type,
51    ) -> Result<FieldBuilderPart> {
52        let (element_ty, is_container) = match self.amount {
53            AmountConstraint::FixedSingle(_) => (ty.clone(), false),
54            AmountConstraint::Any(_) => (into_iterator_item_ty(ty.clone()), true),
55        };
56
57        let (extra_defs, matcher, fetch, builder) = match self.extract {
58            Some(ref extract) => extract.make_from_xml_builder_parts(
59                scope,
60                generics,
61                container_name,
62                member,
63                is_container,
64                ty,
65            )?,
66            None => {
67                let FromEventsScope {
68                    ref substate_result,
69                    ..
70                } = scope;
71
72                generics.add_bound_if_generic(&element_ty, from_xml_trait(Span::call_site()));
73                let from_events = from_events_fn(element_ty.clone());
74
75                let span = element_ty.span();
76                let matcher = quote_spanned! { span=> #from_events(name, attrs, ctx) };
77                let builder = from_xml_builder_ty(element_ty.clone());
78
79                (
80                    TokenStream::default(),
81                    matcher,
82                    quote! { #substate_result },
83                    builder,
84                )
85            }
86        };
87
88        let field_access = scope.access_field(member);
89        match self.amount {
90            AmountConstraint::FixedSingle(_) => {
91                let missing_msg = error_message::on_missing_child(container_name, member);
92                let duplicate_msg = error_message::on_duplicate_child(container_name, member);
93
94                let on_absent = match self.default_ {
95                    Flag::Absent => quote! {
96                        return ::core::result::Result::Err(::xso::error::Error::Other(#missing_msg).into())
97                    },
98                    Flag::Present(_) => {
99                        let default_ = default_fn(element_ty.clone());
100                        quote! {
101                            #default_()
102                        }
103                    }
104                };
105
106                Ok(FieldBuilderPart::Nested {
107                    extra_defs,
108                    value: FieldTempInit {
109                        init: quote! { ::core::option::Option::None },
110                        ty: option_ty(ty.clone()),
111                    },
112                    matcher: NestedMatcher::Selective(quote! {
113                        match #matcher {
114                            ::core::result::Result::Ok(v) => if #field_access.is_some() {
115                                ::core::result::Result::Err(::xso::error::FromEventsError::Invalid(::xso::error::Error::Other(#duplicate_msg)))
116                            } else {
117                                ::core::result::Result::Ok(v)
118                            },
119                            ::core::result::Result::Err(e) => ::core::result::Result::Err(e),
120                        }
121                    }),
122                    builder,
123                    collect: quote! {
124                        #field_access = ::core::option::Option::Some(#fetch);
125                    },
126                    finalize: quote! {
127                        match #field_access {
128                            ::core::option::Option::Some(value) => value,
129                            ::core::option::Option::None => #on_absent,
130                        }
131                    },
132                })
133            }
134            AmountConstraint::Any(_) => {
135                let ty_extend = extend_fn(ty.clone(), element_ty.clone());
136                let ty_default = default_fn(ty.clone());
137                Ok(FieldBuilderPart::Nested {
138                    extra_defs,
139                    value: FieldTempInit {
140                        init: quote! { #ty_default() },
141                        ty: ty.clone(),
142                    },
143                    matcher: NestedMatcher::Selective(matcher),
144                    builder,
145                    collect: quote! {
146                        #ty_extend(&mut #field_access, [#fetch]);
147                    },
148                    finalize: quote! { #field_access },
149                })
150            }
151        }
152    }
153
154    fn make_iterator_part(
155        &self,
156        scope: &AsItemsScope,
157        generics: &mut GenericsInfo,
158        container_name: &ParentRef,
159        bound_name: &Ident,
160        member: &Member,
161        ty: &Type,
162    ) -> Result<FieldIteratorPart> {
163        let AsItemsScope { ref lifetime, .. } = scope;
164
165        let (item_ty, is_container) = match self.amount {
166            AmountConstraint::FixedSingle(_) => (ty.clone(), false),
167            AmountConstraint::Any(_) => {
168                // This should give us the type of element stored in the
169                // collection.
170                (into_iterator_item_ty(ty.clone()), true)
171            }
172        };
173
174        let (extra_defs, init, iter_ty) = match self.extract {
175            Some(ref extract) => extract.make_as_item_iter_parts(
176                scope,
177                generics,
178                ty,
179                container_name,
180                bound_name,
181                member,
182                is_container,
183            )?,
184            None => {
185                generics.add_bound_if_generic(&item_ty, as_xml_trait(Span::call_site()));
186                let as_xml_iter = as_xml_iter_fn(item_ty.clone());
187                let item_iter = item_iter_ty(item_ty.clone(), lifetime.clone());
188
189                let span = item_ty.span();
190                (
191                    TokenStream::default(),
192                    quote_spanned! { span=> #as_xml_iter(#bound_name)? },
193                    item_iter,
194                )
195            }
196        };
197
198        match self.amount {
199            AmountConstraint::FixedSingle(_) => Ok(FieldIteratorPart::Content {
200                extra_defs,
201                value: FieldTempInit { init, ty: iter_ty },
202                generator: quote! {
203                    #bound_name.next().transpose()
204                },
205            }),
206            AmountConstraint::Any(_) => {
207                // This is the collection type we actually work
208                // with -- as_xml_iter uses references after all.
209                let ty = ref_ty(ty.clone(), lifetime.clone());
210
211                // But the iterator for iterating over the elements
212                // inside the collection must use the ref type.
213                let element_iter = into_iterator_iter_ty(ty.clone());
214
215                // And likewise the into_iter impl.
216                let into_iter = into_iterator_into_iter_fn(ty.clone());
217
218                let state_ty = Type::Tuple(TypeTuple {
219                    paren_token: token::Paren::default(),
220                    elems: [element_iter, option_ty(iter_ty)].into_iter().collect(),
221                });
222
223                Ok(FieldIteratorPart::Content {
224                    extra_defs,
225                    value: FieldTempInit {
226                        init: quote! {
227                            (#into_iter(#bound_name), ::core::option::Option::None)
228                        },
229                        ty: state_ty,
230                    },
231                    generator: quote! {
232                        loop {
233                            if let ::core::option::Option::Some(current) = #bound_name.1.as_mut() {
234                                if let ::core::option::Option::Some(item) = current.next() {
235                                    break ::core::option::Option::Some(item).transpose();
236                                }
237                            }
238                            if let ::core::option::Option::Some(item) = #bound_name.0.next() {
239                                #bound_name.1 = ::core::option::Option::Some({
240                                    let #bound_name = item;
241                                    #init
242                                });
243                            } else {
244                                break ::core::result::Result::Ok(::core::option::Option::None)
245                            }
246                        }
247                    },
248                })
249            }
250        }
251    }
252}
253
254/// Definition of what to extract from a child element.
255pub(super) struct ExtractDef {
256    /// The XML namespace of the child to extract data from.
257    pub(super) xml_namespace: NamespaceRef,
258
259    /// The XML name of the child to extract data from.
260    pub(super) xml_name: NameRef,
261
262    /// Compound which contains the arguments of the `extract(..)` meta
263    /// (except the `from`), transformed into a struct with unnamed
264    /// fields.
265    ///
266    /// This is used to generate the parsing/serialisation code, by
267    /// essentially "declaring" a shim struct, as if it were a real Rust
268    /// struct, and using the result of the parsing process directly for
269    /// the field on which the `extract(..)` option was used, instead of
270    /// putting it into a Rust struct.
271    pub(super) parts: Compound,
272}
273
274impl ExtractDef {
275    /// Construct
276    /// [`FieldBuilderPart::Nested::extra_defs`],
277    /// [`FieldBuilderPart::Nested::matcher`],
278    /// an expression which pulls the extraction result from
279    /// `substate_result`,
280    /// and the [`FieldBuilderPart::Nested::builder`] type.
281    fn make_from_xml_builder_parts(
282        &self,
283        scope: &FromEventsScope,
284        generics: &GenericsInfo,
285        container_name: &ParentRef,
286        member: &Member,
287        collecting_into_container: bool,
288        output_ty: &Type,
289    ) -> Result<(TokenStream, TokenStream, TokenStream, Type)> {
290        let FromEventsScope {
291            ref substate_result,
292            ..
293        } = scope;
294
295        let xml_namespace = &self.xml_namespace;
296        let xml_name = &self.xml_name;
297
298        let from_xml_builder_ty_ident = scope.make_member_type_name(member, "FromXmlBuilder");
299        let state_ty_ident = quote::format_ident!("{}State", from_xml_builder_ty_ident,);
300
301        let inner_ty = self.parts.to_single_or_tuple_ty();
302        let mut generics = generics.scoped_for(&inner_ty)?;
303        let extra_defs = self.parts.make_from_events_statemachine(
304            &mut generics,
305            &state_ty_ident,
306            &container_name.child(member.clone()),
307            "",
308        )?.with_augmented_init(|init| quote! {
309            if name.0 == #xml_namespace && name.1 == #xml_name {
310                #init
311            } else {
312                ::core::result::Result::Err(::xso::error::FromEventsError::Mismatch { name, attrs })
313            }
314        }).compile().render(
315            &Visibility::Inherited,
316            &generics,
317            &from_xml_builder_ty_ident,
318            &state_ty_ident,
319            &self.parts.to_tuple_ty().into(),
320            None,
321        )?;
322        let from_xml_builder_ty = generics.ty_with_arguments(from_xml_builder_ty_ident.clone());
323
324        let matcher = quote! { #state_ty_ident::new(name, attrs, ctx).map(|x| #from_xml_builder_ty_ident(::core::option::Option::Some(x))) };
325
326        let fetch = if self.parts.field_count() == 1 {
327            // If we extract only a single field, we automatically unwrap the
328            // tuple, because that behaviour is more obvious to users.
329            quote! { #substate_result.0 }
330        } else {
331            // If we extract more than one field, we pass the value down as
332            // the tuple that it is.
333            quote! { #substate_result }
334        };
335
336        let fetch = if collecting_into_container {
337            // This is for symmetry with the AsXml implementation part. Please
338            // see there for why we cannot do option magic in the collection
339            // case.
340            fetch
341        } else {
342            // This little ".into()" here goes a long way. It relies on one of
343            // the most underrated trait implementations in the standard
344            // library: `impl From<T> for Option<T>`, which creates a
345            // `Some(_)` from a `T`. Why is it so great? Because there is also
346            // `impl From<Option<T>> for Option<T>` (obviously), which is just
347            // a move. So even without knowing the exact type of the substate
348            // result and the field, we can make an "downcast" to `Option<T>`
349            // if the field is of type `Option<T>`, and it does the right
350            // thing no matter whether the extracted field is of type
351            // `Option<T>` or `T`.
352            //
353            // And then, type inference does the rest: There is ambiguity
354            // there, of course, if we call `.into()` on a value of type
355            // `Option<T>`: Should Rust wrap it into another layer of
356            // `Option`, or should it just move the value? The answer lies in
357            // the type constraint imposed by the place the value is *used*,
358            // which is strictly bound by the field's type (so there is, in
359            // fact, no ambiguity). So this works all kinds of magic.
360            quote_spanned! {
361                output_ty.span()=>
362                    <#output_ty as ::core::convert::From::<#inner_ty>>::from(#fetch)
363            }
364        };
365
366        Ok((extra_defs, matcher, fetch, from_xml_builder_ty))
367    }
368
369    /// Construct
370    /// [`FieldIteratorPart::Content::extra_defs`],
371    /// the [`FieldIteratorPart::Content::value`] init,
372    /// and the iterator type.
373    fn make_as_item_iter_parts(
374        &self,
375        scope: &AsItemsScope,
376        generics: &GenericsInfo,
377        input_ty: &Type,
378        container_name: &ParentRef,
379        bound_name: &Ident,
380        member: &Member,
381        iterating_container: bool,
382    ) -> Result<(TokenStream, TokenStream, Type)> {
383        let AsItemsScope { ref lifetime, .. } = scope;
384
385        let xml_namespace = &self.xml_namespace;
386        let xml_name = &self.xml_name;
387
388        let item_iter_ty_ident = scope.make_member_type_name(member, "AsXmlIterator");
389        let state_ty_ident = quote::format_ident!("{}State", item_iter_ty_ident,);
390        let tuple_ty = self.parts.to_ref_tuple_ty(lifetime);
391
392        let (repack, inner_ty) = match self.parts.single_ty() {
393            Some(single_ty) => (
394                quote! { #bound_name, },
395                ref_ty(single_ty.clone(), lifetime.clone()),
396            ),
397            None => {
398                let mut repack_tuple = TokenStream::default();
399                // The cast here is sound, because the constructor of Compound
400                // already asserts that there are less than 2^32 fields (with
401                // what I think is a great error message, go check it out).
402                for i in 0..(tuple_ty.elems.len() as u32) {
403                    let index = Index {
404                        index: i,
405                        span: Span::call_site(),
406                    };
407                    repack_tuple.extend(quote! {
408                        &#bound_name.#index,
409                    })
410                }
411                let ref_tuple_ty = ref_ty(self.parts.to_tuple_ty().into(), lifetime.clone());
412                (repack_tuple, ref_tuple_ty)
413            }
414        };
415
416        let mut generics = generics.scoped_for(&ref_ty(inner_ty.clone(), lifetime.clone()))?;
417
418        let item_iter_ty = generics.ty_with_arguments(item_iter_ty_ident.clone());
419        let extra_defs = self
420            .parts
421            .make_as_item_iter_statemachine(
422                &mut generics,
423                &container_name.child(member.clone()),
424                &state_ty_ident,
425                "",
426                lifetime,
427            )?
428            .with_augmented_init(|init| {
429                quote! {
430                    let name = (
431                        ::xso::exports::rxml::Namespace::from(#xml_namespace),
432                        ::xso::exports::alloc::borrow::Cow::Borrowed(#xml_name),
433                    );
434                    #init
435                }
436            })
437            .compile()
438            .render(
439                &Visibility::Inherited,
440                &generics,
441                &tuple_ty.into(),
442                &state_ty_ident,
443                lifetime,
444                &item_iter_ty_ident,
445            )?;
446
447        let (make_iter, item_iter_ty) = if iterating_container {
448            // When we are iterating a container, the container's iterator's
449            // item type may either be `&(A, B, ...)` or `(&A, &B, ...)`.
450            // Unfortunately, to be able to handle both, we need to omit the
451            // magic Option cast, because we cannot specify the type of the
452            // argument of the `.map(...)` closure and rust is not able to
453            // infer that type because the repacking is too opaque.
454            //
455            // However, this is not much of a loss, because it doesn't really
456            // make sense to have the option cast there, anyway: optional
457            // elements in a container would be weird.
458            (
459                quote! {
460                    #item_iter_ty_ident::new((#repack))?
461                },
462                item_iter_ty,
463            )
464        } else {
465            // Again we exploit the extreme usefulness of the
466            // `impl From<T> for Option<T>`. We already wrote extensively
467            // about that in [`make_from_xml_builder_parts`] implementation
468            // corresponding to this code above, and we will not repeat it
469            // here.
470
471            // These sections with quote_spanned are used to improve error
472            // messages on type mismatches. Without these, the rustc errors
473            // will point at `#[derive(AsXml)]` only, instead of directly
474            // pointing at the sources of those types.
475            let cast = quote_spanned! { input_ty.span()=>
476                ::core::option::Option::from(#bound_name)
477            };
478            let type_assert = quote_spanned! { inner_ty.span()=>
479                ::core::option::Option<#inner_ty>
480            };
481            (
482                quote! {
483                    ::xso::asxml::OptionAsXml::new({ let x: #type_assert = #cast; x.map(|#bound_name: #inner_ty| {
484                        #item_iter_ty_ident::new((#repack))
485                    })}.transpose()?)
486                },
487                option_as_xml_ty(item_iter_ty),
488            )
489        };
490
491        Ok((extra_defs, make_iter, item_iter_ty))
492    }
493}