use json::JsonValue;
use std::collections::BTreeMap;
#[derive(Clone, Debug)]
pub struct Request {
pub config: JsonValue,
pub method: Method,
pub query: BTreeMap<String, String>,
pub header: BTreeMap<String, String>,
pub cookie: BTreeMap<String, String>,
pub custom: BTreeMap<String, JsonValue>,
pub body: JsonValue,
pub client_ip: String,
}
#[derive(Clone, Debug)]
pub enum Method {
Post,
Get,
Head,
Put,
Delete,
Options,
Patch,
Trace,
None,
}
impl Method {
pub fn from(name: &str) -> Self {
match name.to_lowercase().as_str() {
"post" => Self::Post,
"get" => Self::Get,
"head" => Self::Head,
"put" => Self::Put,
"delete" => Self::Delete,
"options" => Self::Options,
"patch" => Self::Patch,
"trace" => Self::Trace,
_ => Self::None,
}
}
pub fn str(&mut self) -> &'static str {
match self {
Method::Post => "POST",
Method::Get => "GET",
Method::Head => "HEAD",
Method::Put => "PUT",
Method::Delete => "DELETE",
Method::Options => "OPTIONS",
Method::Patch => "PATCH",
Method::Trace => "TRACE",
Method::None => "",
}
}
}
#[derive(Debug, Clone)]
pub enum ContentType {
FormData,
FormUrlencoded,
Json,
Xml,
Javascript,
Text,
Html,
Other(String),
}
impl ContentType {
pub fn from(name: &str) -> Self {
match name {
"multipart/form-data" => Self::FormData,
"application/x-www-form-urlencoded" => Self::FormUrlencoded,
"application/json" => Self::Json,
"application/xml" | "text/xml" => Self::Xml,
"application/javascript" => Self::Javascript,
"text/html" => Self::Html,
"text/plain" => Self::Text,
_ => Self::Other(name.to_string()),
}
}
pub fn str(&mut self) -> String {
match self {
Self::FormData => "multipart/form-data",
Self::FormUrlencoded => "application/x-www-form-urlencoded",
Self::Json => "application/json",
Self::Xml => "application/xml",
Self::Javascript => "application/javascript",
Self::Text => "text/plain",
Self::Html => "text/html",
Self::Other(name) => name,
}
.to_string()
}
}