use std::fmt::{Formatter, Display};
use ContentType::*;
pub struct Content {
pub content: Vec<u8>,
pub r#type: ContentType,
pub status_code: u16,
pub reason: String
}
impl Content {
pub fn new(content: Vec<u8>, r#type: ContentType, status_code: u16) -> Self {
Self::new_with_reason(content, r#type, status_code, "OK")
}
pub fn new_with_reason(content: Vec<u8>, r#type: ContentType, status_code: u16, reason: &str) -> Self {
Self {
content,
r#type,
status_code,
reason: reason.to_string()
}
}
}
#[derive(Debug)]
pub enum ContentType {
Html,
Json,
Png,
Jpeg,
Ico
}
impl Display for ContentType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let s = match self {
Html => "text/html",
Json => "text/json",
Png => "image/png",
Jpeg => "image/jpeg",
Ico => "image/x-icon"
};
write!(f, "{}", s)
}
}
impl Clone for ContentType {
fn clone(&self) -> Self {
*self
}
}
impl Copy for ContentType {}