🚧 checkpoint

main
_ 2020-10-30 17:01:37 -05:00
parent 2e2e6a5f32
commit 1615f0d075
2 changed files with 40 additions and 1 deletions

View File

@ -79,6 +79,13 @@ impl <'a> ResponseHandle <'a> {
println! ("Err: {:?}", e); println! ("Err: {:?}", e);
} }
} }
async fn respond_2 (
self,
r: http_serde::Response
) {
self.respond (r.parts, r.body).await
}
} }
async fn serve_dir ( async fn serve_dir (

View File

@ -5,6 +5,9 @@ use std::{
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
// Hyper doesn't seem to make it easy to de/ser requests
// and responses and stuff like that, so I do it by hand here.
pub enum Error { pub enum Error {
Unsupported, Unsupported,
InvalidHeaderName, InvalidHeaderName,
@ -57,6 +60,12 @@ pub enum StatusCode {
PartialContent, PartialContent,
} }
impl Default for StatusCode {
fn default () -> Self {
Self::Ok
}
}
impl From <StatusCode> for hyper::StatusCode { impl From <StatusCode> for hyper::StatusCode {
fn from (x: StatusCode) -> Self { fn from (x: StatusCode) -> Self {
match x { match x {
@ -67,7 +76,7 @@ impl From <StatusCode> for hyper::StatusCode {
} }
} }
#[derive (Deserialize, Serialize)] #[derive (Default, Deserialize, Serialize)]
pub struct ResponseParts { pub struct ResponseParts {
pub status_code: StatusCode, pub status_code: StatusCode,
@ -77,3 +86,26 @@ pub struct ResponseParts {
pub headers: HashMap <String, Vec <u8>>, pub headers: HashMap <String, Vec <u8>>,
} }
#[derive (Default)]
pub struct Response {
pub parts: ResponseParts,
pub body: Option <reqwest::Body>,
}
impl Response {
pub fn status_code (&mut self, c: StatusCode) -> &mut Self {
self.parts.status_code = c;
self
}
pub fn header (&mut self, k: String, v: Vec <u8>) -> &mut Self {
self.parts.headers.insert (k, v);
self
}
pub fn body (&mut self, b: reqwest::Body) -> &mut Self {
self.body = Some (b);
self
}
}