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
//! Infrastructure for parsing boolean fields indicating child presence.
use proc_macro2::{Span, TokenStream};

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

use crate::error_message::{self, ParentRef};
use crate::meta::{Name, NameRef, NamespaceRef, StaticNamespace};

use super::{Field, FieldParsePart};

/// A field parsed from the presence of an empty XML child.
///
/// Maps to `#[xml(flag)]`.
#[derive(Debug)]
pub(crate) struct FlagField {
    /// The XML namespace of the child element to look for.
    namespace: StaticNamespace,

    /// The XML name of the child element to look for.
    name: Name,
}

impl FlagField {
    /// Construct a new `#[xml(flag)]` field.
    ///
    /// `namespace` and `name` must both be set and `namespace` must be a
    /// [`NamespaceRef::Static`].
    ///
    /// `attr_span` is used for emitting error messages when no better span
    /// can be constructed. This should point at the `#[xml(..)]` meta of the
    /// field or another closely-related object.
    pub(super) fn new(
        attr_span: &Span,
        namespace: Option<NamespaceRef>,
        name: Option<NameRef>,
    ) -> Result<Self> {
        let namespace = match namespace {
            None => {
                return Err(Error::new(
                    attr_span.clone(),
                    "#[xml(flag)] requires namespace attribute",
                ))
            }
            Some(NamespaceRef::Static(ns)) => ns,
            Some(NamespaceRef::Dyn(ns)) => {
                return Err(Error::new_spanned(
                    ns,
                    "dynamic namespaces cannot be used with #[xml(flag)]",
                ))
            }
            Some(NamespaceRef::Super(ns)) => {
                return Err(Error::new_spanned(
                    ns,
                    "flag elements cannot refer to the parent namespace",
                ))
            }
        };
        let name = match name {
            None => {
                return Err(Error::new(
                    attr_span.clone(),
                    "#[xml(flag)] requires name attribute",
                ))
            }
            Some(name) => name,
        };
        Ok(Self {
            namespace,
            name: name.into(),
        })
    }
}

impl Field for FlagField {
    fn build_try_from_element(
        &self,
        container_name: &ParentRef,
        _container_namespace_expr: &Expr,
        tempname: Ident,
        member: &Member,
        _ty: &Type,
    ) -> Result<FieldParsePart> {
        let field_name = &self.name;
        let field_namespace = &self.namespace;
        let duperr = error_message::on_duplicate_child(container_name, member);
        Ok(FieldParsePart {
            tempinit: quote! {
                let mut #tempname = false;
            },
            childiter: quote! {
                residual = if residual.is(#field_name, #field_namespace) {
                    // TODO: reject contents
                    if #tempname {
                        return Err(::xso::error::Error::ParseError(#duperr));
                    }
                    #tempname = true;
                    continue;
                } else {
                    residual
                };
            },
            value: quote! { #tempname },
            ..FieldParsePart::default()
        })
    }

    fn build_into_element(
        &self,
        _container_name: &ParentRef,
        _container_namespace_expr: &Expr,
        _member: &Member,
        _ty: &Type,
        access: Expr,
    ) -> Result<TokenStream> {
        let child_name = &self.name;
        let child_namespace = &self.namespace;
        Ok(quote! {
            if #access {
                builder.append(::xso::exports::minidom::Node::Element(::xso::exports::minidom::Element::builder(#child_name, #child_namespace).build()))
            } else {
                builder
            }
        })
    }

    fn build_set_namespace(
        &self,
        _input: &Ident,
        _ty: &Type,
        _access: Expr,
    ) -> Result<TokenStream> {
        Ok(TokenStream::default())
    }
}