Skip to content

Commit

Permalink
Impl 'TryFrom<&str>' for 'Method'.
Browse files Browse the repository at this point in the history
Also implements 'From<Infallible>' for 'ParseMethodError'.
  • Loading branch information
SergioBenitez committed Aug 17, 2024
1 parent 5799f98 commit 6f40f1e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 14 additions & 0 deletions core/http/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ pub struct ParseMethodError;

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

impl From<std::convert::Infallible> for ParseMethodError {
fn from(infallible: std::convert::Infallible) -> Self {
match infallible {}
}
}

impl fmt::Display for ParseMethodError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("invalid HTTP method")
Expand All @@ -331,6 +337,14 @@ impl FromStr for Method {
}
}

impl TryFrom<&str> for Method {
type Error = ParseMethodError;

fn try_from(s: &str) -> Result<Self, Self::Error> {
Self::try_from(s.as_bytes())
}
}

impl AsRef<str> for Method {
fn as_ref(&self) -> &str {
self.as_str()
Expand Down
2 changes: 0 additions & 2 deletions testbench/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,4 @@ pub enum Error {
Ipc(#[from] ipc_channel::ipc::IpcError),
#[error("liftoff failed")]
Liftoff(String, String),
#[error("invalid method")]
InvalidMethod,
}

0 comments on commit 6f40f1e

Please sign in to comment.