utoipa_gen/path/media_type/
encoding.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
use proc_macro2::{Ident, TokenStream};
use quote::{quote, ToTokens};
use syn::parse::{Parse, ParseStream};
use syn::{parenthesized, Error, Token};

use crate::parse_utils;

// (content_type = "...", explode = true, allow_reserved = false,)
#[derive(Default)]
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct Encoding {
    pub(super) content_type: Option<parse_utils::LitStrOrExpr>,
    // pub(super) headers: BTreeMap<String, Header>,
    // pub(super) style: Option<ParameterStyle>,
    pub(super) explode: Option<bool>,
    pub(super) allow_reserved: Option<bool>,
    // pub(super) extensions: Option<Extensions>,
}

impl Parse for Encoding {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let content;
        parenthesized!(content in input);

        let mut encoding = Encoding::default();

        while !content.is_empty() {
            let ident = content.parse::<Ident>()?;
            let attribute_name = &*ident.to_string();
            match attribute_name {
                "content_type" => {
                    encoding.content_type = Some(
                        parse_utils::parse_next_literal_str_or_expr(&content)?
                    )
                }
                // "headers" => {}
                // "style" => {}
                "explode" => {
                    encoding.explode = Some(
                        parse_utils::parse_bool_or_true(&content)?
                    )
                }
                "allow_reserved" => {
                    encoding.allow_reserved = Some(
                        parse_utils::parse_bool_or_true(&content)?
                    )
                }
                // "extensions"  => {}
                _ => {
                    return Err(
                        Error::new(
                            ident.span(),
                            format!("unexpected attribute: {attribute_name}, expected one of: content_type, explode, allow_reserved")
                        )
                    )
                }
            }

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

        Ok(encoding)
    }
}

impl ToTokens for Encoding {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let content_type = self
            .content_type
            .as_ref()
            .map(|content_type| quote!(.content_type(Some(#content_type))));
        let explode = self
            .explode
            .as_ref()
            .map(|value| quote!(.explode(Some(#value))));
        let allow_reserved = self
            .allow_reserved
            .as_ref()
            .map(|allow_reserved| quote!(.allow_reserved(Some(#allow_reserved))));

        tokens.extend(quote! {
            utoipa::openapi::encoding::EncodingBuilder::new()
                #content_type
                #explode
                #allow_reserved
        })
    }
}