xso_proc/field/
mod.rs

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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// 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/.

//! Compound (struct or enum variant) field types

use proc_macro2::{Span, TokenStream};
use syn::{spanned::Spanned, *};

use rxml_validation::NcName;

use crate::compound::Compound;
use crate::error_message::ParentRef;
use crate::meta::{AmountConstraint, Flag, NameRef, NamespaceRef, QNameRef, XmlFieldMeta};
use crate::scope::{AsItemsScope, FromEventsScope};

mod attribute;
mod child;
#[cfg(feature = "minidom")]
mod element;
mod flag;
mod text;

use self::attribute::AttributeField;
use self::child::{ChildField, ExtractDef};
#[cfg(feature = "minidom")]
use self::element::ElementField;
use self::flag::FlagField;
use self::text::TextField;

/// Code slices necessary for declaring and initializing a temporary variable
/// for parsing purposes.
pub(crate) struct FieldTempInit {
    /// The type of the temporary variable.
    pub(crate) ty: Type,

    /// The initializer for the temporary variable.
    pub(crate) init: TokenStream,
}

/// Configure how a nested field builder selects child elements.
pub(crate) enum NestedMatcher {
    /// Matches a specific child element fallabily.
    Selective(
        /// Expression which evaluates to `Result<T, FromEventsError>`,
        /// consuming `name: rxml::QName` and `attrs: rxml::AttrMap`.
        ///
        /// `T` must be the type specified in the
        /// [`FieldBuilderPart::Nested::builder`]  field.
        TokenStream,
    ),

    #[cfg_attr(not(feature = "minidom"), allow(dead_code))]
    /// Matches any child element not matched by another matcher.
    ///
    /// Only a single field may use this variant, otherwise an error is
    /// raised during execution of the proc macro.
    Fallback(
        /// Expression which evaluates to `T` (or `return`s an error),
        /// consuming `name: rxml::QName` and `attrs: rxml::AttrMap`.
        ///
        /// `T` must be the type specified in the
        /// [`FieldBuilderPart::Nested::builder`]  field.
        TokenStream,
    ),
}

/// Describe how a struct or enum variant's member is parsed from XML data.
///
/// This struct is returned from [`FieldDef::make_builder_part`] and
/// contains code snippets and instructions for
/// [`Compound::make_from_events_statemachine`][`crate::compound::Compound::make_from_events_statemachine`]
/// to parse the field's data from XML.
pub(crate) enum FieldBuilderPart {
    /// Parse a field from the item's element's start event.
    Init {
        /// Expression and type which extracts the field's data from the
        /// element's start event.
        value: FieldTempInit,
    },

    /// Parse a field from text events.
    Text {
        /// Expression and type which initializes a buffer to use during
        /// parsing.
        value: FieldTempInit,

        /// Statement which takes text and accumulates it into the temporary
        /// value declared via `value`.
        collect: TokenStream,

        /// Expression which evaluates to the field's type, consuming the
        /// temporary value.
        finalize: TokenStream,
    },

    /// Parse a field from child element events.
    Nested {
        /// Additional definition items which need to be inserted at module
        /// level for the rest of the implementation to work.
        extra_defs: TokenStream,

        /// Expression and type which initializes a buffer to use during
        /// parsing.
        value: FieldTempInit,

        /// Configure child matching behaviour for this field. See
        /// [`NestedMatcher`] for options.
        matcher: NestedMatcher,

        /// Type implementing `xso::FromEventsBuilder` which parses the child
        /// element.
        ///
        /// This type is returned by the expressions in
        /// [`matcher`][`Self::Nested::matcher`].
        builder: Type,

        /// Expression which consumes the value stored in the identifier
        /// [`crate::common::FromEventsScope::substate_result`][`FromEventsScope::substate_result`]
        /// and somehow collects it into the field declared with
        /// [`value`][`Self::Nested::value`].
        collect: TokenStream,

        /// Expression which consumes the data from the field declared with
        /// [`value`][`Self::Nested::value`] and converts it into the field's
        /// type.
        finalize: TokenStream,
    },
}

/// Describe how a struct or enum variant's member is converted to XML data.
///
/// This struct is returned from [`FieldDef::make_iterator_part`] and
/// contains code snippets and instructions for
/// [`Compound::make_into_events_statemachine`][`crate::compound::Compound::make_into_events_statemachine`]
/// to convert the field's data into XML.
pub(crate) enum FieldIteratorPart {
    /// The field is emitted as part of StartElement.
    Header {
        /// An expression which consumes the field's value and returns a
        /// `Item`.
        generator: TokenStream,
    },

    /// The field is emitted as text item.
    Text {
        /// An expression which consumes the field's value and returns a
        /// String, which is then emitted as text data.
        generator: TokenStream,
    },

    /// The field is emitted as series of items which form a child element.
    Content {
        /// Additional definition items which need to be inserted at module
        /// level for the rest of the implementation to work.
        extra_defs: TokenStream,

        /// Expression and type which initializes the nested iterator.
        ///
        /// Note that this is evaluated at construction time of the iterator.
        /// Fields of this variant do not get access to their original data,
        /// unless they carry it in the contents of this `value`.
        value: FieldTempInit,

        /// An expression which uses the value (mutably) and evaluates to
        /// a Result<Option<Item>, Error>. Once the state returns None, the
        /// processing will advance to the next state.
        generator: TokenStream,
    },
}

trait Field {
    /// Construct the builder pieces for this field.
    ///
    /// `container_name` must be a reference to the compound's type, so that
    /// it can be used for error messages.
    ///
    /// `member` and `ty` refer to the field itself.
    fn make_builder_part(
        &self,
        scope: &FromEventsScope,
        container_name: &ParentRef,
        member: &Member,
        ty: &Type,
    ) -> Result<FieldBuilderPart>;

    /// Construct the iterator pieces for this field.
    ///
    /// `bound_name` must be the name to which the field's value is bound in
    /// the iterator code.
    ///
    /// `member` and `ty` refer to the field itself.
    ///
    /// `bound_name` is the name under which the field's value is accessible
    /// in the various parts of the code.
    fn make_iterator_part(
        &self,
        scope: &AsItemsScope,
        container_name: &ParentRef,
        bound_name: &Ident,
        member: &Member,
        ty: &Type,
    ) -> Result<FieldIteratorPart>;

    /// Return true if and only if this field captures text content.
    fn captures_text(&self) -> bool {
        false
    }
}

fn default_name(span: Span, name: Option<NameRef>, field_ident: Option<&Ident>) -> Result<NameRef> {
    match name {
        Some(v) => Ok(v),
        None => match field_ident {
            None => Err(Error::new(
                span,
                "name must be explicitly specified with the `name` key on unnamed fields",
            )),
            Some(field_ident) => match NcName::try_from(field_ident.to_string()) {
                Ok(value) => Ok(NameRef::Literal {
                    span: field_ident.span(),
                    value,
                }),
                Err(e) => Err(Error::new(
                    field_ident.span(),
                    format!("invalid XML name: {}", e),
                )),
            },
        },
    }
}

/// Construct a new field implementation from the meta attributes.
///
/// `field_ident` is, for some field types, used to infer an XML name if
/// it is not specified explicitly.
///
/// `field_ty` is needed for type inference on extracted fields.
///
/// `container_namespace` is used in some cases to insert a default
/// namespace.
fn new_field(
    meta: XmlFieldMeta,
    field_ident: Option<&Ident>,
    field_ty: &Type,
    container_namespace: &NamespaceRef,
) -> Result<Box<dyn Field>> {
    match meta {
        XmlFieldMeta::Attribute {
            span,
            qname: QNameRef { namespace, name },
            default_,
            type_,
        } => {
            let xml_name = default_name(span, name, field_ident)?;

            // This would've been taken via `XmlFieldMeta::take_type` if
            // this field was within an extract where a `type_` is legal
            // to have.
            if let Some(type_) = type_ {
                return Err(Error::new_spanned(
                    type_,
                    "specifying `type_` on fields inside structs and enum variants is redundant and not allowed."
                ));
            }

            Ok(Box::new(AttributeField {
                xml_name,
                xml_namespace: namespace,
                default_,
            }))
        }

        XmlFieldMeta::Text {
            span: _,
            codec,
            type_,
        } => {
            // This would've been taken via `XmlFieldMeta::take_type` if
            // this field was within an extract where a `type_` is legal
            // to have.
            if let Some(type_) = type_ {
                return Err(Error::new_spanned(
                    type_,
                    "specifying `type_` on fields inside structs and enum variants is redundant and not allowed."
                ));
            }

            Ok(Box::new(TextField { codec }))
        }

        XmlFieldMeta::Child {
            span: _,
            default_,
            amount,
        } => {
            if let Some(AmountConstraint::Any(ref amount_span)) = amount {
                if let Flag::Present(ref flag_span) = default_ {
                    let mut err =
                        Error::new(*flag_span, "`default` has no meaning for child collections");
                    err.combine(Error::new(
                        *amount_span,
                        "the field is treated as a collection because of this `n` value",
                    ));
                    return Err(err);
                }
            }

            Ok(Box::new(ChildField {
                default_,
                amount: amount.unwrap_or(AmountConstraint::FixedSingle(Span::call_site())),
                extract: None,
            }))
        }

        XmlFieldMeta::Extract {
            span,
            default_,
            qname: QNameRef { namespace, name },
            amount,
            fields,
            on_unknown_attribute,
            on_unknown_child,
        } => {
            let xml_namespace = namespace.unwrap_or_else(|| container_namespace.clone());
            let xml_name = default_name(span, name, field_ident)?;

            let amount = amount.unwrap_or(AmountConstraint::FixedSingle(Span::call_site()));
            match amount {
                AmountConstraint::Any(ref amount) => {
                    if let Flag::Present(default_) = default_ {
                        let mut err = Error::new(
                            default_,
                            "default cannot be set when collecting into a collection",
                        );
                        err.combine(Error::new(
                            *amount,
                            "`n` was set to a non-1 value here, which enables connection logic",
                        ));
                        return Err(err);
                    }
                }
                AmountConstraint::FixedSingle(_) => (),
            }

            let mut field_defs = Vec::new();
            let allow_inference =
                matches!(amount, AmountConstraint::FixedSingle(_)) && fields.len() == 1;
            for (i, mut field) in fields.into_iter().enumerate() {
                let field_ty = match field.take_type() {
                    Some(v) => v,
                    None => {
                        if allow_inference {
                            field_ty.clone()
                        } else {
                            return Err(Error::new(
                            field.span(),
                            "extracted field must specify a type explicitly when extracting into a collection or when extracting more than one field."
                        ));
                        }
                    }
                };

                field_defs.push(FieldDef::from_extract(
                    field,
                    i as u32,
                    &field_ty,
                    &xml_namespace,
                ));
            }
            let parts =
                Compound::from_field_defs(field_defs, on_unknown_attribute, on_unknown_child)?;

            Ok(Box::new(ChildField {
                default_,
                amount,
                extract: Some(ExtractDef {
                    xml_namespace,
                    xml_name,
                    parts,
                }),
            }))
        }

        #[cfg(feature = "minidom")]
        XmlFieldMeta::Element { span, amount } => {
            match amount {
                Some(AmountConstraint::Any(_)) => (),
                Some(AmountConstraint::FixedSingle(span)) => {
                    return Err(Error::new(
                        span,
                        "only `n = ..` is supported for #[xml(element)]` currently",
                    ))
                }
                None => return Err(Error::new(span, "`n` must be set to `..` currently")),
            }

            Ok(Box::new(ElementField))
        }

        #[cfg(not(feature = "minidom"))]
        XmlFieldMeta::Element { span, amount } => {
            let _ = amount;
            Err(Error::new(
                span,
                "#[xml(element)] requires xso to be built with the \"minidom\" feature.",
            ))
        }

        XmlFieldMeta::Flag {
            span,
            qname: QNameRef { namespace, name },
        } => {
            let xml_namespace = namespace.unwrap_or_else(|| container_namespace.clone());
            let xml_name = default_name(span, name, field_ident)?;
            Ok(Box::new(FlagField {
                xml_namespace,
                xml_name,
            }))
        }
    }
}

/// Definition of a single field in a compound.
///
/// See [`Compound`][`crate::compound::Compound`] for more information on
/// compounds in general.
pub(crate) struct FieldDef {
    /// A span which refers to the field's definition.
    span: Span,

    /// The member identifying the field.
    member: Member,

    /// The type of the field.
    ty: Type,

    /// The way the field is mapped to XML.
    inner: Box<dyn Field>,
}

impl FieldDef {
    /// Create a new field definition from its declaration.
    ///
    /// The `index` must be the zero-based index of the field even for named
    /// fields.
    pub(crate) fn from_field(
        field: &syn::Field,
        index: u32,
        container_namespace: &NamespaceRef,
    ) -> Result<Self> {
        let (member, ident) = match field.ident.as_ref() {
            Some(v) => (Member::Named(v.clone()), Some(v)),
            None => (
                Member::Unnamed(Index {
                    index,
                    // We use the type's span here, because `field.span()`
                    // will visually point at the `#[xml(..)]` meta, which is
                    // not helpful when glancing at error messages referring
                    // to the field itself.
                    span: field.ty.span(),
                }),
                None,
            ),
        };
        // This will either be the field's identifier's span (for named
        // fields) or the field's type (for unnamed fields), which should give
        // the user a good visual feedback about which field an error message
        // is.
        let field_span = member.span();
        let meta = XmlFieldMeta::parse_from_attributes(&field.attrs, &field_span)?;
        let ty = field.ty.clone();

        Ok(Self {
            span: field_span,
            inner: new_field(meta, ident, &ty, container_namespace)?,
            member,
            ty,
        })
    }

    /// Create a new field definition from its declaration.
    ///
    /// The `index` must be the zero-based index of the field even for named
    /// fields.
    pub(crate) fn from_extract(
        meta: XmlFieldMeta,
        index: u32,
        ty: &Type,
        container_namespace: &NamespaceRef,
    ) -> Result<Self> {
        let span = meta.span();
        Ok(Self {
            span,
            member: Member::Unnamed(Index { index, span }),
            ty: ty.clone(),
            inner: new_field(meta, None, ty, container_namespace)?,
        })
    }

    /// Access the [`syn::Member`] identifying this field in the original
    /// type.
    pub(crate) fn member(&self) -> &Member {
        &self.member
    }

    /// Access the field's type.
    pub(crate) fn ty(&self) -> &Type {
        &self.ty
    }

    /// Construct the builder pieces for this field.
    ///
    /// `container_name` must be a reference to the compound's type, so that
    /// it can be used for error messages.
    pub(crate) fn make_builder_part(
        &self,
        scope: &FromEventsScope,
        container_name: &ParentRef,
    ) -> Result<FieldBuilderPart> {
        self.inner
            .make_builder_part(scope, container_name, &self.member, &self.ty)
    }

    /// Construct the iterator pieces for this field.
    ///
    /// `bound_name` must be the name to which the field's value is bound in
    /// the iterator code.
    pub(crate) fn make_iterator_part(
        &self,
        scope: &AsItemsScope,
        container_name: &ParentRef,
        bound_name: &Ident,
    ) -> Result<FieldIteratorPart> {
        self.inner
            .make_iterator_part(scope, container_name, bound_name, &self.member, &self.ty)
    }

    /// Return true if this field's parsing consumes text data.
    pub(crate) fn is_text_field(&self) -> bool {
        self.inner.captures_text()
    }

    /// Return a span which points at the field's definition.'
    pub(crate) fn span(&self) -> Span {
        self.span
    }
}