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
use proc_macro2::Ident;
use quote::{quote, ToTokens};
use syn::{parenthesized, parse::Parse, token::Paren, Error, LitStr, Token};

use crate::parse_utils;

#[derive(Default, Clone)]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct XmlAttr {
    pub name: Option<String>,
    pub namespace: Option<String>,
    pub prefix: Option<String>,
    pub is_attribute: bool,
    pub is_wrapped: Option<Ident>,
    pub wrap_name: Option<String>,
}

impl XmlAttr {
    pub fn with_wrapped(is_wrapped: Option<Ident>, wrap_name: Option<String>) -> Self {
        Self {
            is_wrapped,
            wrap_name,
            ..Default::default()
        }
    }
}

impl Parse for XmlAttr {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        const EXPECTED_ATTRIBUTE_MESSAGE: &str =
            "unexpected attribute, expected any of: name, namespace, prefix, attribute, wrapped";
        let mut xml = XmlAttr::default();

        while !input.is_empty() {
            let attribute = input
                .parse::<Ident>()
                .map_err(|error| Error::new(error.span(), EXPECTED_ATTRIBUTE_MESSAGE))?;
            let attribute_name = &*attribute.to_string();

            match attribute_name {
                "name" => {
                    xml.name =
                        Some(parse_utils::parse_next(input, || input.parse::<LitStr>())?.value())
                }
                "namespace" => {
                    xml.namespace =
                        Some(parse_utils::parse_next(input, || input.parse::<LitStr>())?.value())
                }
                "prefix" => {
                    xml.prefix =
                        Some(parse_utils::parse_next(input, || input.parse::<LitStr>())?.value())
                }
                "attribute" => xml.is_attribute = parse_utils::parse_bool_or_true(input)?,
                "wrapped" => {
                    // wrapped or wrapped(name = "wrap_name")
                    if input.peek(Paren) {
                        let group;
                        parenthesized!(group in input);

                        let wrapped_attribute = group.parse::<Ident>().map_err(|error| {
                            Error::new(
                                error.span(),
                                format!("unexpected attribute, expected: name, {error}"),
                            )
                        })?;
                        if wrapped_attribute != "name" {
                            return Err(Error::new(
                                wrapped_attribute.span(),
                                "unexpected wrapped attribute, expected: name",
                            ));
                        }
                        group.parse::<Token![=]>()?;
                        xml.wrap_name = Some(group.parse::<LitStr>()?.value());
                    }
                    xml.is_wrapped = Some(attribute);
                }
                _ => return Err(Error::new(attribute.span(), EXPECTED_ATTRIBUTE_MESSAGE)),
            }

            if !input.is_empty() {
                input.parse::<Token![,]>()?;
            }
        }

        Ok(xml)
    }
}

impl ToTokens for XmlAttr {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        tokens.extend(quote! {
            utoipa::openapi::xml::XmlBuilder::new()
        });

        if let Some(ref name) = self.name {
            tokens.extend(quote! {
                .name(Some(#name))
            })
        }

        if let Some(ref namespace) = self.namespace {
            tokens.extend(quote! {
                .namespace(Some(#namespace))
            })
        }

        if let Some(ref prefix) = self.prefix {
            tokens.extend(quote! {
                .prefix(Some(#prefix))
            })
        }

        if self.is_attribute {
            tokens.extend(quote! {
                .attribute(Some(true))
            })
        }

        if self.is_wrapped.is_some() {
            tokens.extend(quote! {
                .wrapped(Some(true))
            });

            // if is wrapped and wrap name is defined use wrap name instead
            if let Some(ref wrap_name) = self.wrap_name {
                tokens.extend(quote! {
                    .name(Some(#wrap_name))
                })
            }
        }

        tokens.extend(quote! { .build() })
    }
}