use mime::Mime;
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Clone, Copy)]
pub enum HumusFormatFamily {
Template,
API,
}
pub trait HumusFormat: ToString + Clone + Default + Send {
fn get_family(&self) -> HumusFormatFamily {
match self.get_name().as_str() {
"json" => HumusFormatFamily::API,
_ => HumusFormatFamily::Template,
}
}
fn get_file_extension(&self) -> String {
match self.get_name().as_str() {
"text" => ".txt".to_string(),
_ => ".".to_owned() + &self.get_name(),
}
}
fn get_name(&self) -> String {
self.to_string()
}
fn get_less_well_known_mimetype(&self) -> Option<Mime> {
None
}
fn get_mime_type(&self) -> Mime {
match self.get_name().as_str() {
"text" => mime::TEXT_PLAIN_UTF_8,
"html" => mime::TEXT_HTML_UTF_8,
"json" => mime::APPLICATION_JSON,
"xml" => "application/xml"
.parse()
.expect("Parse static application/xml"),
"rss" => "application/rss+xml"
.parse()
.expect("Parse static application/rss+xml"),
"atom" => "application/atom+xml"
.parse()
.expect("Parse static application/atom+xml"),
_ => self
.get_less_well_known_mimetype()
.unwrap_or(mime::APPLICATION_OCTET_STREAM),
}
}
fn from_name(name: &str) -> Option<Self>;
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum HtmlTextJsonFormat {
#[default]
Html,
Text,
Json,
}
impl ToString for HtmlTextJsonFormat {
fn to_string(&self) -> String {
match self {
Self::Html => "html",
Self::Text => "text",
Self::Json => "json",
}
.to_owned()
}
}
impl HumusFormat for HtmlTextJsonFormat {
fn get_family(&self) -> HumusFormatFamily {
match self {
Self::Json => HumusFormatFamily::API,
_ => HumusFormatFamily::Template,
}
}
fn get_file_extension(&self) -> String {
match self {
Self::Text => ".txt",
Self::Html => ".html",
Self::Json => ".json",
}
.to_string()
}
fn get_mime_type(&self) -> Mime {
match self {
Self::Text => mime::TEXT_PLAIN_UTF_8,
Self::Html => mime::TEXT_HTML_UTF_8,
Self::Json => mime::APPLICATION_JSON,
}
}
fn from_name(name: &str) -> Option<Self> {
match name {
"html" => Some(Self::Html),
"text" => Some(Self::Text),
"json" => Some(Self::Json),
_ => None,
}
}
}