Skip to content

Commit

Permalink
impl Display
Browse files Browse the repository at this point in the history
  • Loading branch information
Diane Huxley committed Oct 3, 2024
1 parent e004bd0 commit f151a77
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion crates/web5/src/http.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -82,6 +84,30 @@ pub struct HttpResponse {
pub body: Vec<u8>,
}

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<Self> {
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<Arc<dyn HttpClient>> = OnceCell::new();

#[cfg(feature = "http_reqwest")]
Expand Down

0 comments on commit f151a77

Please sign in to comment.