diff --git a/crates/web5/src/http.rs b/crates/web5/src/http.rs index f08a5e2e..3602dfd6 100644 --- a/crates/web5/src/http.rs +++ b/crates/web5/src/http.rs @@ -1,7 +1,9 @@ use async_trait::async_trait; use once_cell::sync::OnceCell; use serde::{Deserialize, Serialize}; -use std::{collections::HashMap, sync::Arc}; +use std::{collections::HashMap, fmt, str::FromStr, sync::Arc}; + +use crate::errors::Web5Error; #[async_trait] pub trait HttpClient: Send + Sync { @@ -82,6 +84,30 @@ pub struct HttpResponse { pub body: Vec, } +impl fmt::Display for Method { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let method_str = match self { + Method::Get => "GET", + Method::Post => "POST", + Method::Put => "PUT", + }; + write!(f, "{}", method_str) + } +} + +impl FromStr for Method { + type Err = Web5Error; + + fn from_str(s: &str) -> crate::errors::Result { + match s.to_ascii_uppercase().as_ref() { + "GET" => Ok(Method::Get), + "POST" => Ok(Method::Post), + "PUT" => Ok(Method::Put), + _ => Err(Web5Error::Parameter(format!("unknown method {}", s))), + } + } +} + static HTTP_CLIENT: OnceCell> = OnceCell::new(); #[cfg(feature = "http_reqwest")]