axum_macros/from_request/
attr.rs1use crate::attr_parsing::{combine_attribute, parse_parenthesized_attribute, Combine};
2use syn::{
3 parse::{Parse, ParseStream},
4 Token,
5};
6
7pub(crate) mod kw {
8 syn::custom_keyword!(via);
9 syn::custom_keyword!(rejection);
10 syn::custom_keyword!(state);
11}
12
13#[derive(Default)]
14pub(super) struct FromRequestContainerAttrs {
15 pub(super) via: Option<(kw::via, syn::Path)>,
16 pub(super) rejection: Option<(kw::rejection, syn::Path)>,
17 pub(super) state: Option<(kw::state, syn::Type)>,
18}
19
20impl Parse for FromRequestContainerAttrs {
21 fn parse(input: ParseStream) -> syn::Result<Self> {
22 let mut via = None;
23 let mut rejection = None;
24 let mut state = None;
25
26 while !input.is_empty() {
27 let lh = input.lookahead1();
28 if lh.peek(kw::via) {
29 parse_parenthesized_attribute(input, &mut via)?;
30 } else if lh.peek(kw::rejection) {
31 parse_parenthesized_attribute(input, &mut rejection)?;
32 } else if lh.peek(kw::state) {
33 parse_parenthesized_attribute(input, &mut state)?;
34 } else {
35 return Err(lh.error());
36 }
37
38 let _ = input.parse::<Token![,]>();
39 }
40
41 Ok(Self {
42 via,
43 rejection,
44 state,
45 })
46 }
47}
48
49impl Combine for FromRequestContainerAttrs {
50 fn combine(mut self, other: Self) -> syn::Result<Self> {
51 let Self {
52 via,
53 rejection,
54 state,
55 } = other;
56 combine_attribute(&mut self.via, via)?;
57 combine_attribute(&mut self.rejection, rejection)?;
58 combine_attribute(&mut self.state, state)?;
59 Ok(self)
60 }
61}
62
63#[derive(Default)]
64pub(super) struct FromRequestFieldAttrs {
65 pub(super) via: Option<(kw::via, syn::Path)>,
66}
67
68impl Parse for FromRequestFieldAttrs {
69 fn parse(input: ParseStream) -> syn::Result<Self> {
70 let mut via = None;
71
72 while !input.is_empty() {
73 let lh = input.lookahead1();
74 if lh.peek(kw::via) {
75 parse_parenthesized_attribute(input, &mut via)?;
76 } else {
77 return Err(lh.error());
78 }
79
80 let _ = input.parse::<Token![,]>();
81 }
82
83 Ok(Self { via })
84 }
85}
86
87impl Combine for FromRequestFieldAttrs {
88 fn combine(mut self, other: Self) -> syn::Result<Self> {
89 let Self { via } = other;
90 combine_attribute(&mut self.via, via)?;
91 Ok(self)
92 }
93}