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

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

use crate::common::Scope;
use crate::error_message::ParentRef;

use super::{Field, FieldFromEventsPart, FieldIntoEventsPart, FieldTempInit};

/// 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
    }

    fn build_from_events_builder(
        &self,
        scope: &Scope,
        _container_name: &ParentRef,
        _tempname: Ident,
        _member: &Member,
        ty: &Type,
    ) -> Result<FieldFromEventsPart> {
        let Scope {
            ref namespace_local,
            ..
        } = scope;
        Ok(FieldFromEventsPart::Init {
            value: FieldTempInit {
                ty: ty.clone(),
                init: quote! { #namespace_local },
            },
        })
    }

    /// 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! {})
    }

    fn build_into_events_iterator(
        &self,
        _scope: &Scope,
        _container_name: &ParentRef,
        _tempname: Ident,
        _member: &Member,
        _ty: &Type,
    ) -> Result<FieldIntoEventsPart> {
        Ok(FieldIntoEventsPart::Passive)
    }
}