mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-23 00:00:15 +02:00
* Only allow last extractor to mutate the request * Change `FromRequest` and add `FromRequestParts` trait (#1275) * Add `Once`/`Mut` type parameter for `FromRequest` and `RequestParts` * 🪄 * split traits * `FromRequest` for tuples * Remove `BodyAlreadyExtracted` * don't need fully qualified path * don't export `Once` and `Mut` * remove temp tests * depend on axum again Co-authored-by: Jonas Platte <[email protected]> * Port `Handler` and most extractors (#1277) * Port `Handler` and most extractors * Put `M` inside `Handler` impls, not trait itself * comment out tuples for now * fix lints * Reorder arguments to `Handler` (#1281) I think `Request<B>, Arc<S>` is better since its consistent with `FromRequest` and `FromRequestParts`. * Port most things in axum-extra (#1282) * Port `#[derive(TypedPath)]` and `#[debug_handler]` (#1283) * port #[derive(TypedPath)] * wip: #[debug_handler] * fix #[debug_handler] * don't need itertools * also require `Send` * update expected error * support fully qualified `self` * Implement FromRequest[Parts] for tuples (#1286) * Port docs for axum and axum-core (#1285) * Port axum-extra (#1287) * Port axum-extra * Update axum-core/Cargo.toml Co-authored-by: Jonas Platte <[email protected]> * remove `impl FromRequest for Either*` Co-authored-by: Jonas Platte <[email protected]> * New FromRequest[Parts] trait cleanup (#1288) * Make private module truly private again * Simplify tuple FromRequest implementation * Port `#[derive(FromRequest)]` (#1289) * fix tests * fix docs * revert examples * fix docs link * fix intra docs links * Port examples (#1291) * Document wrapping other extractors (#1292) * axum-extra doesn't need to depend on axum-core (#1294) Missed this in https://github.com/tokio-rs/axum/pull/1287 * Add `FromRequest` changes to changelogs (#1293) * Update changelog * Remove default type for `S` in `Handler` * Clarify which types have default types for `S` * Apply suggestions from code review Co-authored-by: Jonas Platte <[email protected]> Co-authored-by: Jonas Platte <[email protected]> * remove unused import * Rename `Mut` and `Once` (#1296) * fix trybuild expected output Co-authored-by: Jonas Platte <[email protected]>
167 lines
4.5 KiB
Rust
167 lines
4.5 KiB
Rust
use quote::ToTokens;
|
|
use syn::{
|
|
parse::{Parse, ParseStream},
|
|
punctuated::Punctuated,
|
|
Token,
|
|
};
|
|
|
|
#[derive(Default)]
|
|
pub(crate) struct FromRequestFieldAttr {
|
|
pub(crate) via: Option<(kw::via, syn::Path)>,
|
|
}
|
|
|
|
pub(crate) enum FromRequestContainerAttr {
|
|
Via {
|
|
path: syn::Path,
|
|
rejection: Option<syn::Path>,
|
|
},
|
|
Rejection(syn::Path),
|
|
None,
|
|
}
|
|
|
|
pub(crate) mod kw {
|
|
syn::custom_keyword!(via);
|
|
syn::custom_keyword!(rejection);
|
|
syn::custom_keyword!(Display);
|
|
syn::custom_keyword!(Debug);
|
|
syn::custom_keyword!(Error);
|
|
}
|
|
|
|
pub(crate) fn parse_field_attrs(attrs: &[syn::Attribute]) -> syn::Result<FromRequestFieldAttr> {
|
|
let attrs = parse_attrs(attrs)?;
|
|
|
|
let mut out = FromRequestFieldAttr::default();
|
|
|
|
for from_request_attr in attrs {
|
|
match from_request_attr {
|
|
FieldAttr::Via { via, path } => {
|
|
if out.via.is_some() {
|
|
return Err(double_attr_error("via", via));
|
|
} else {
|
|
out.via = Some((via, path));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(out)
|
|
}
|
|
|
|
pub(crate) fn parse_container_attrs(
|
|
attrs: &[syn::Attribute],
|
|
) -> syn::Result<FromRequestContainerAttr> {
|
|
let attrs = parse_attrs::<ContainerAttr>(attrs)?;
|
|
|
|
let mut out_via = None;
|
|
let mut out_rejection = None;
|
|
|
|
// we track the index of the attribute to know which comes last
|
|
// used to give more accurate error messages
|
|
for (idx, from_request_attr) in attrs.into_iter().enumerate() {
|
|
match from_request_attr {
|
|
ContainerAttr::Via { via, path } => {
|
|
if out_via.is_some() {
|
|
return Err(double_attr_error("via", via));
|
|
} else {
|
|
out_via = Some((idx, via, path));
|
|
}
|
|
}
|
|
ContainerAttr::Rejection { rejection, path } => {
|
|
if out_rejection.is_some() {
|
|
return Err(double_attr_error("rejection", rejection));
|
|
} else {
|
|
out_rejection = Some((idx, rejection, path));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
match (out_via, out_rejection) {
|
|
(Some((_, _, path)), None) => Ok(FromRequestContainerAttr::Via {
|
|
path,
|
|
rejection: None,
|
|
}),
|
|
|
|
(Some((_, _, path)), Some((_, _, rejection))) => Ok(FromRequestContainerAttr::Via {
|
|
path,
|
|
rejection: Some(rejection),
|
|
}),
|
|
|
|
(None, Some((_, _, rejection))) => Ok(FromRequestContainerAttr::Rejection(rejection)),
|
|
|
|
(None, None) => Ok(FromRequestContainerAttr::None),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn parse_attrs<T>(attrs: &[syn::Attribute]) -> syn::Result<Punctuated<T, Token![,]>>
|
|
where
|
|
T: Parse,
|
|
{
|
|
let attrs = attrs
|
|
.iter()
|
|
.filter(|attr| attr.path.is_ident("from_request"))
|
|
.map(|attr| attr.parse_args_with(Punctuated::<T, Token![,]>::parse_terminated))
|
|
.collect::<syn::Result<Vec<_>>>()?
|
|
.into_iter()
|
|
.flatten()
|
|
.collect::<Punctuated<T, Token![,]>>();
|
|
Ok(attrs)
|
|
}
|
|
|
|
fn double_attr_error<T>(ident: &str, spanned: T) -> syn::Error
|
|
where
|
|
T: ToTokens,
|
|
{
|
|
syn::Error::new_spanned(spanned, format!("`{}` specified more than once", ident))
|
|
}
|
|
|
|
enum ContainerAttr {
|
|
Via {
|
|
via: kw::via,
|
|
path: syn::Path,
|
|
},
|
|
Rejection {
|
|
rejection: kw::rejection,
|
|
path: syn::Path,
|
|
},
|
|
}
|
|
|
|
impl Parse for ContainerAttr {
|
|
fn parse(input: ParseStream) -> syn::Result<Self> {
|
|
let lh = input.lookahead1();
|
|
if lh.peek(kw::via) {
|
|
let via = input.parse::<kw::via>()?;
|
|
let content;
|
|
syn::parenthesized!(content in input);
|
|
content.parse().map(|path| Self::Via { via, path })
|
|
} else if lh.peek(kw::rejection) {
|
|
let rejection = input.parse::<kw::rejection>()?;
|
|
let content;
|
|
syn::parenthesized!(content in input);
|
|
content
|
|
.parse()
|
|
.map(|path| Self::Rejection { rejection, path })
|
|
} else {
|
|
Err(lh.error())
|
|
}
|
|
}
|
|
}
|
|
|
|
enum FieldAttr {
|
|
Via { via: kw::via, path: syn::Path },
|
|
}
|
|
|
|
impl Parse for FieldAttr {
|
|
fn parse(input: ParseStream) -> syn::Result<Self> {
|
|
let lh = input.lookahead1();
|
|
if lh.peek(kw::via) {
|
|
let via = input.parse::<kw::via>()?;
|
|
let content;
|
|
syn::parenthesized!(content in input);
|
|
content.parse().map(|path| Self::Via { via, path })
|
|
} else {
|
|
Err(lh.error())
|
|
}
|
|
}
|
|
}
|