Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seanaye/feat/serverside http #312

Merged
merged 17 commits into from
May 2, 2023
Merged
1 change: 1 addition & 0 deletions crates/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ http = [
'web-sys/RequestInit',
'web-sys/RequestMode',
'web-sys/Response',
'web-sys/ResponseInit',
'web-sys/ResponseType',
'web-sys/Window',
'web-sys/RequestCache',
Expand Down
2 changes: 1 addition & 1 deletion crates/net/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ HTTP requests library for WASM Apps. It provides idiomatic Rust bindings for the
### HTTP

```rust
let resp = Request::get("/path")
let resp = Request:get("/path")
seanaye marked this conversation as resolved.
Show resolved Hide resolved
.send()
.await
.unwrap();
Expand Down
57 changes: 57 additions & 0 deletions crates/net/src/http/method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::Error;
use std::fmt;
use std::str::FromStr;

#[allow(
missing_docs,
seanaye marked this conversation as resolved.
Show resolved Hide resolved
missing_debug_implementations,
seanaye marked this conversation as resolved.
Show resolved Hide resolved
clippy::upper_case_acronyms
seanaye marked this conversation as resolved.
Show resolved Hide resolved
)]
/// Valid request methods.
#[derive(Clone, Copy, Debug)]
pub enum Method {
GET,
HEAD,
POST,
PUT,
DELETE,
CONNECT,
OPTIONS,
TRACE,
PATCH,
}

impl fmt::Display for Method {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Method::GET => "GET",
Method::HEAD => "HEAD",
Method::POST => "POST",
Method::PUT => "PUT",
Method::DELETE => "DELETE",
Method::CONNECT => "CONNECT",
Method::OPTIONS => "OPTIONS",
Method::TRACE => "TRACE",
Method::PATCH => "PATCH",
};
write!(f, "{}", s)
}
}

impl FromStr for Method {
type Err = Error;
fn from_str(input: &str) -> Result<Method, Error> {
match input {
"GET" => Ok(Method::GET),
"HEAD" => Ok(Method::HEAD),
"POST" => Ok(Method::POST),
"PUT" => Ok(Method::PUT),
"DELETE" => Ok(Method::DELETE),
"CONNECT" => Ok(Method::CONNECT),
"OPTIONS" => Ok(Method::OPTIONS),
"TRACE" => Ok(Method::TRACE),
"PATCH" => Ok(Method::PATCH),
_ => Err(Error::GlooError("Tried to parse invalid method".into())),
}
}
}