axum_macros/from_request/
attr.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
91
92
93
use crate::attr_parsing::{combine_attribute, parse_parenthesized_attribute, Combine};
use syn::{
    parse::{Parse, ParseStream},
    Token,
};

pub(crate) mod kw {
    syn::custom_keyword!(via);
    syn::custom_keyword!(rejection);
    syn::custom_keyword!(state);
}

#[derive(Default)]
pub(super) struct FromRequestContainerAttrs {
    pub(super) via: Option<(kw::via, syn::Path)>,
    pub(super) rejection: Option<(kw::rejection, syn::Path)>,
    pub(super) state: Option<(kw::state, syn::Type)>,
}

impl Parse for FromRequestContainerAttrs {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let mut via = None;
        let mut rejection = None;
        let mut state = None;

        while !input.is_empty() {
            let lh = input.lookahead1();
            if lh.peek(kw::via) {
                parse_parenthesized_attribute(input, &mut via)?;
            } else if lh.peek(kw::rejection) {
                parse_parenthesized_attribute(input, &mut rejection)?;
            } else if lh.peek(kw::state) {
                parse_parenthesized_attribute(input, &mut state)?;
            } else {
                return Err(lh.error());
            }

            let _ = input.parse::<Token![,]>();
        }

        Ok(Self {
            via,
            rejection,
            state,
        })
    }
}

impl Combine for FromRequestContainerAttrs {
    fn combine(mut self, other: Self) -> syn::Result<Self> {
        let Self {
            via,
            rejection,
            state,
        } = other;
        combine_attribute(&mut self.via, via)?;
        combine_attribute(&mut self.rejection, rejection)?;
        combine_attribute(&mut self.state, state)?;
        Ok(self)
    }
}

#[derive(Default)]
pub(super) struct FromRequestFieldAttrs {
    pub(super) via: Option<(kw::via, syn::Path)>,
}

impl Parse for FromRequestFieldAttrs {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let mut via = None;

        while !input.is_empty() {
            let lh = input.lookahead1();
            if lh.peek(kw::via) {
                parse_parenthesized_attribute(input, &mut via)?;
            } else {
                return Err(lh.error());
            }

            let _ = input.parse::<Token![,]>();
        }

        Ok(Self { via })
    }
}

impl Combine for FromRequestFieldAttrs {
    fn combine(mut self, other: Self) -> syn::Result<Self> {
        let Self { via } = other;
        combine_attribute(&mut self.via, via)?;
        Ok(self)
    }
}