Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ await_holding_lock = "warn"
dbg_macro = "warn"
empty_enum = "warn"
enum_glob_use = "warn"
equatable_if_let = "warn"
exit = "warn"
filter_map_next = "warn"
fn_params_excessive_bools = "warn"
Expand All @@ -33,6 +34,8 @@ inefficient_to_string = "warn"
linkedlist = "warn"
lossy_float_literal = "warn"
macro_use_imports = "warn"
manual_let_else = "warn"
match_same_arms = "warn"
match_wildcard_for_single_variants = "warn"
mem_forget = "warn"
must_use_candidate = "warn"
Expand All @@ -41,6 +44,7 @@ needless_continue = "warn"
option_option = "warn"
rest_pat_in_fully_bound_structs = "warn"
return_self_not_must_use = "warn"
single_match_else = "warn"
str_to_string = "warn"
suboptimal_flops = "warn"
todo = "warn"
Expand Down
15 changes: 8 additions & 7 deletions axum-extra/src/extract/cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ where
type Rejection = T::Rejection;

async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
match Extension::<CachedEntry<T>>::from_request_parts(parts, state).await {
Ok(Extension(CachedEntry(value))) => Ok(Self(value)),
Err(_) => {
let value = T::from_request_parts(parts, state).await?;
parts.extensions.insert(CachedEntry(value.clone()));
Ok(Self(value))
}
if let Ok(Extension(CachedEntry(value))) =
Extension::<CachedEntry<T>>::from_request_parts(parts, state).await
{
Ok(Self(value))
} else {
let value = T::from_request_parts(parts, state).await?;
parts.extensions.insert(CachedEntry(value.clone()));
Ok(Self(value))
}
}
}
Expand Down
12 changes: 3 additions & 9 deletions axum-extra/src/extract/json_deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,15 @@ composite_rejection! {
}

fn json_content_type(headers: &HeaderMap) -> bool {
let content_type = if let Some(content_type) = headers.get(header::CONTENT_TYPE) {
content_type
} else {
let Some(content_type) = headers.get(header::CONTENT_TYPE) else {
return false;
};

let content_type = if let Ok(content_type) = content_type.to_str() {
content_type
} else {
let Ok(content_type) = content_type.to_str() else {
return false;
};

let mime = if let Ok(mime) = content_type.parse::<mime::Mime>() {
mime
} else {
let Ok(mime) = content_type.parse::<mime::Mime>() else {
return false;
};

Expand Down
77 changes: 35 additions & 42 deletions axum-macros/src/from_request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,22 @@ pub(crate) fn expand(item: syn::Item, tr: Trait) -> syn::Result<TokenStream> {
state,
} = parse_attrs("from_request", &attrs)?;

let state = match state {
Some((_, state)) => State::Custom(state),
None => {
let mut inferred_state_types: HashSet<_> =
infer_state_type_from_field_types(&fields)
.chain(infer_state_type_from_field_attributes(&fields))
.collect();

if let Some((_, via)) = &via {
inferred_state_types.extend(state_from_via(&ident, via));
}
let state = if let Some((_, state)) = state {
State::Custom(state)
} else {
let mut inferred_state_types: HashSet<_> =
infer_state_type_from_field_types(&fields)
.chain(infer_state_type_from_field_attributes(&fields))
.collect();

match inferred_state_types.len() {
0 => State::Default(syn::parse_quote!(S)),
1 => State::Custom(inferred_state_types.iter().next().unwrap().to_owned()),
_ => State::CannotInfer,
}
if let Some((_, via)) = &via {
inferred_state_types.extend(state_from_via(&ident, via));
}

match inferred_state_types.len() {
0 => State::Default(syn::parse_quote!(S)),
1 => State::Custom(inferred_state_types.iter().next().unwrap().to_owned()),
_ => State::CannotInfer,
}
};

Expand All @@ -147,7 +146,7 @@ pub(crate) fn expand(item: syn::Item, tr: Trait) -> syn::Result<TokenStream> {
}
};

if let State::CannotInfer = state {
if matches!(state, State::CannotInfer) {
let attr_name = match tr {
Trait::FromRequest => "from_request",
Trait::FromRequestParts => "from_request_parts",
Expand Down Expand Up @@ -335,17 +334,16 @@ fn impl_struct_by_extracting_each_field(
state: &State,
tr: Trait,
) -> syn::Result<TokenStream> {
let trait_fn_body = match state {
State::CannotInfer => quote! {
let trait_fn_body = if matches!(state, State::CannotInfer) {
quote! {
::std::unimplemented!()
},
_ => {
let extract_fields = extract_fields(&fields, &rejection, tr)?;
quote! {
::std::result::Result::Ok(Self {
#(#extract_fields)*
})
}
}
} else {
let extract_fields = extract_fields(&fields, &rejection, tr)?;
quote! {
::std::result::Result::Ok(Self {
#(#extract_fields)*
})
}
};

Expand Down Expand Up @@ -417,15 +415,14 @@ fn extract_fields(
tr: Trait,
) -> syn::Result<Vec<TokenStream>> {
fn member(field: &syn::Field, index: usize) -> TokenStream {
match &field.ident {
Some(ident) => quote! { #ident },
_ => {
let member = syn::Member::Unnamed(syn::Index {
index: index as u32,
span: field.span(),
});
quote! { #member }
}
if let Some(ident) = &field.ident {
quote! { #ident }
} else {
let member = syn::Member::Unnamed(syn::Index {
index: index as u32,
span: field.span(),
});
quote! { #member }
}
}

Expand Down Expand Up @@ -642,9 +639,7 @@ fn extract_fields(
}

fn peel_option(ty: &syn::Type) -> Option<&syn::Type> {
let type_path = if let syn::Type::Path(type_path) = ty {
type_path
} else {
let syn::Type::Path(type_path) = ty else {
return None;
};

Expand Down Expand Up @@ -673,9 +668,7 @@ fn peel_option(ty: &syn::Type) -> Option<&syn::Type> {
}

fn peel_result_ok(ty: &syn::Type) -> Option<&syn::Type> {
let type_path = if let syn::Type::Path(type_path) = ty {
type_path
} else {
let syn::Type::Path(type_path) = ty else {
return None;
};

Expand Down
4 changes: 1 addition & 3 deletions axum/src/extract/matched_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ pub(crate) fn set_matched_path_for_request(
route_id_to_path: &HashMap<RouteId, Arc<str>>,
extensions: &mut http::Extensions,
) {
let matched_path = if let Some(matched_path) = route_id_to_path.get(&id) {
matched_path
} else {
let Some(matched_path) = route_id_to_path.get(&id) else {
#[cfg(debug_assertions)]
panic!("should always have a matched path for a route id");
#[cfg(not(debug_assertions))]
Expand Down
8 changes: 2 additions & 6 deletions axum/src/extract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,11 @@ pub use self::ws::WebSocketUpgrade;

// this is duplicated in `axum-extra/src/extract/form.rs`
pub(super) fn has_content_type(headers: &HeaderMap, expected_content_type: &mime::Mime) -> bool {
let content_type = if let Some(content_type) = headers.get(header::CONTENT_TYPE) {
content_type
} else {
let Some(content_type) = headers.get(header::CONTENT_TYPE) else {
return false;
};

let content_type = if let Ok(content_type) = content_type.to_str() {
content_type
} else {
let Ok(content_type) = content_type.to_str() else {
return false;
};

Expand Down
3 changes: 1 addition & 2 deletions axum/src/extract/path/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,7 @@ enum KeyOrIdx<'de> {
impl<'de> KeyOrIdx<'de> {
fn key(&self) -> &'de str {
match &self {
Self::Key(key) => key,
Self::Idx { key, .. } => key,
Self::Idx { key, .. } | Self::Key(key) => key,
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions axum/src/extract/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,7 @@ fn header_eq(headers: &HeaderMap, key: HeaderName, value: &'static str) -> bool
}

fn header_contains(headers: &HeaderMap, key: HeaderName, value: &'static str) -> bool {
let header = if let Some(header) = headers.get(&key) {
header
} else {
let Some(header) = headers.get(&key) else {
return false;
};

Expand Down
3 changes: 1 addition & 2 deletions axum/src/routing/method_routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,7 @@ impl AllowHeader {
match (self, other) {
(Self::Skip, _) | (_, Self::Skip) => Self::Skip,
(Self::None, Self::None) => Self::None,
(Self::None, Self::Bytes(pick)) => Self::Bytes(pick),
(Self::Bytes(pick), Self::None) => Self::Bytes(pick),
(Self::None, Self::Bytes(pick)) | (Self::Bytes(pick), Self::None) => Self::Bytes(pick),
(Self::Bytes(mut a), Self::Bytes(b)) => {
a.extend_from_slice(b",");
a.extend_from_slice(&b);
Expand Down
23 changes: 9 additions & 14 deletions axum/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,11 @@ where
T::Response: IntoResponse,
T::Future: Send + 'static,
{
let service = match try_downcast::<Self, _>(service) {
Ok(_) => {
panic!(
"Invalid route: `Router::route_service` cannot be used with `Router`s. \
Use `Router::nest` instead"
);
}
Err(service) => service,
let Err(service) = try_downcast::<Self, _>(service) else {
panic!(
"Invalid route: `Router::route_service` cannot be used with `Router`s. \
Use `Router::nest` instead"
);
};

tap_inner!(self, mut this => {
Expand Down Expand Up @@ -256,16 +253,13 @@ where

map_inner!(self, mut this => {
match (this.default_fallback, default_fallback) {
// both have the default fallback
// other has a default fallback
// use the one from other
(true, true) => {}
(_, true) => {}
// this has default fallback, other has a custom fallback
(true, false) => {
this.default_fallback = false;
}
// this has a custom fallback, other has a default
(false, true) => {
}
// both have a custom fallback, not allowed
(false, false) => {
panic!("Cannot merge two `Router`s that both have a fallback")
Expand Down Expand Up @@ -707,8 +701,9 @@ where
{
fn merge(self, other: Self) -> Option<Self> {
match (self, other) {
(Self::Default(_), pick @ Self::Default(_)) => Some(pick),
// If either are `Default`, return the opposite one.
(Self::Default(_), pick) | (pick, Self::Default(_)) => Some(pick),
// Otherwise, return None
_ => None,
}
}
Expand Down