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
//! Infrastructure for parsing dynamic namespaces.
use proc_macro2::TokenStream;

use quote::quote;
use syn::*;

use crate::error_message::ParentRef;

use super::{Field, FieldParsePart};

/// A field holding the dynamic namespace of a compound with
/// `#[xml(namespace = dyn)]`.
///
/// See also
/// [`StructNamespace::Dyn`][`crate::structs::StructNamespace::Dyn`].
///
/// Maps to `#[xml(namespace)]`.
#[derive(Debug)]
pub(crate) struct NamespaceField;

impl NamespaceField {
    /// Construct a new `#[xml(namespace)]` field.
    pub(super) fn new() -> Self {
        Self
    }
}

impl Field for NamespaceField {
    fn is_namespace(&self) -> bool {
        true
    }

    /// Construct code which copies the value from the `namespace_tempname`
    /// into the value of the field.
    ///
    /// The actual parsing is special, because it has to happen during type
    /// matching, and happens in
    /// [`Compound::build_try_from_element`][`crate::compound::Compound::build_try_from_element`].
    fn build_try_from_element(
        &self,
        _container_name: &ParentRef,
        container_namespace_expr: &Expr,
        _tempname: Ident,
        _member: &Member,
        _ty: &Type,
    ) -> Result<FieldParsePart> {
        Ok(FieldParsePart {
            value: quote! { #container_namespace_expr },
            ..FieldParsePart::default()
        })
    }

    /// This is a no-op, because the actual implementation is in
    /// [`crate::compound::DynCompound::build_set_namespace`].
    fn build_set_namespace(
        &self,
        _input: &Ident,
        _ty: &Type,
        _access: Expr,
    ) -> Result<TokenStream> {
        // NOTE: does nothing because this is handled specially by
        // Compound::build_set_namespace
        Ok(quote! {})
    }

    /// This is a no-op.
    ///
    /// The actual implementation of the specialities of dynamic namespace
    /// fields resides in
    /// [`Compound::build_into_element`][`crate::compound::Compound::build_into_element`].
    fn build_into_element(
        &self,
        _container_name: &ParentRef,
        _container_namespace_expr: &Expr,
        _member: &Member,
        _ty: &Type,
        _access: Expr,
    ) -> Result<TokenStream> {
        Ok(quote! {builder})
    }
}