Skip to content

Commit

Permalink
UnsupportedMethod error type for try_from(..)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolaLS committed Jun 30, 2022
1 parent 50c710d commit 7cec360
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/axum.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 23 additions & 4 deletions axum/src/routing/method_filter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fmt, fmt::{Debug, Formatter}};
use bitflags::bitflags;
use http::Method;
use axum_core::Error;

bitflags! {
/// A filter that matches one or more HTTP methods.
Expand All @@ -24,10 +24,29 @@ bitflags! {
}
}

#[derive(Debug)]
pub struct UnsupportedMethod {
method: http::Method,
}

impl UnsupportedMethod {
pub fn method(&self) -> &http::Method {
&self.method
}
}

impl fmt::Display for UnsupportedMethod {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "Failed to match http method: {}", self.method.as_str())
}
}

impl std::error::Error for UnsupportedMethod {}

impl TryFrom<Method> for MethodFilter {
type Error = Error;
type Error = UnsupportedMethod;

fn try_from(m: Method) -> Result<Self, Error> {
fn try_from(m: Method) -> Result<Self, UnsupportedMethod> {
match m {
Method::DELETE => Ok(MethodFilter::DELETE),
Method::GET => Ok(MethodFilter::GET),
Expand All @@ -37,7 +56,7 @@ impl TryFrom<Method> for MethodFilter {
Method::POST => Ok(MethodFilter::POST),
Method::PUT => Ok(MethodFilter::PUT),
Method::TRACE => Ok(MethodFilter::TRACE),
_ => Err(Error::new("could not map method")),
_ => Err(UnsupportedMethod { method: m}),
}
}
}
7 changes: 7 additions & 0 deletions axum/src/routing/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::{
task::{Context, Poll},
time::Duration,
};
use std::fmt::format;
use tower::{service_fn, timeout::TimeoutLayer, ServiceBuilder, ServiceExt};
use tower_http::{auth::RequireAuthorizationLayer, limit::RequestBodyLimitLayer};
use tower_service::Service;
Expand Down Expand Up @@ -731,4 +732,10 @@ fn from_http_method() {
assert_eq!(MethodFilter::try_from(Method::POST).unwrap(), MethodFilter::POST);
assert_eq!(MethodFilter::try_from(Method::PUT).unwrap(), MethodFilter::PUT);
assert_eq!(MethodFilter::try_from(Method::TRACE).unwrap(), MethodFilter::TRACE);
match MethodFilter::try_from(http::Method::CONNECT) {
Ok(_) => panic!("test failed, the method 'CONNECT' is not supported"),
Err(e) => {
assert!(format!("{}", e).contains(e.method().as_str()))
}
}
}

0 comments on commit 7cec360

Please sign in to comment.