ptth/src/http_serde.rs

80 lines
1.6 KiB
Rust

use std::{
collections::*,
convert::{TryFrom},
};
use serde::{Deserialize, Serialize};
pub enum Error {
Unsupported,
InvalidHeaderName,
}
impl From <hyper::header::InvalidHeaderName> for Error {
fn from (_x: hyper::header::InvalidHeaderName) -> Self {
Self::InvalidHeaderName
}
}
#[derive (Deserialize, Serialize)]
pub enum Method {
Get,
Head,
}
impl TryFrom <hyper::Method> for Method {
type Error = Error;
fn try_from (x: hyper::Method) -> Result <Self, Error> {
match x {
hyper::Method::GET => Ok (Self::Get),
hyper::Method::HEAD => Ok (Self::Head),
_ => Err (Error::Unsupported),
}
}
}
#[derive (Deserialize, Serialize)]
pub struct RequestParts {
pub id: String,
pub method: Method,
// Technically URIs are subtle and complex but I don't care
pub uri: String,
// Technically Hyper has headers in a multi-map
// but I don't feel like doing that right now.
// Headers are _kinda_ ASCII but they can also be binary.
// HTTP is nutty.
pub headers: HashMap <String, Vec <u8>>,
}
#[derive (Deserialize, Serialize)]
pub enum StatusCode {
Ok,
NotFound,
PartialContent,
}
impl From <StatusCode> for hyper::StatusCode {
fn from (x: StatusCode) -> Self {
match x {
StatusCode::Ok => Self::OK,
StatusCode::NotFound => Self::NOT_FOUND,
StatusCode::PartialContent => Self::PARTIAL_CONTENT,
}
}
}
#[derive (Deserialize, Serialize)]
pub struct ResponseParts {
pub status_code: StatusCode,
// Technically Hyper has headers in a multi-map
// but I don't feel like doing that right now.
// We promise not to need this feature.
pub headers: HashMap <String, Vec <u8>>,
}