use std::collections::HashMap;
use serde::{Deserialize, Serialize};
pub trait SaRequest {
fn get_header(&self, name: &str) -> Option<String>;
fn get_headers(&self) -> HashMap<String, String> {
HashMap::new() }
fn get_cookie(&self, name: &str) -> Option<String>;
fn get_cookies(&self) -> HashMap<String, String> {
HashMap::new() }
fn get_param(&self, name: &str) -> Option<String>;
fn get_params(&self) -> HashMap<String, String> {
HashMap::new() }
fn get_path(&self) -> String;
fn get_method(&self) -> String;
fn get_uri(&self) -> String {
self.get_path()
}
fn get_body_json<T: for<'de> Deserialize<'de>>(&self) -> Option<T> {
None }
fn get_client_ip(&self) -> Option<String> {
None }
fn get_user_agent(&self) -> Option<String> {
self.get_header("user-agent")
}
}
pub trait SaResponse {
fn set_header(&mut self, name: &str, value: &str);
fn set_cookie(&mut self, name: &str, value: &str, options: CookieOptions);
fn delete_cookie(&mut self, name: &str) {
self.set_cookie(name, "", CookieOptions {
max_age: Some(0),
..Default::default()
});
}
fn set_status(&mut self, status: u16);
fn set_json_body<T: Serialize>(&mut self, body: T) -> Result<(), serde_json::Error>;
}
#[derive(Debug, Clone, Default)]
pub struct CookieOptions {
pub domain: Option<String>,
pub path: Option<String>,
pub max_age: Option<i64>,
pub http_only: bool,
pub secure: bool,
pub same_site: Option<SameSite>,
}
#[derive(Debug, Clone, Copy)]
pub enum SameSite {
Strict,
Lax,
None,
}
impl std::fmt::Display for SameSite {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SameSite::Strict => write!(f, "Strict"),
SameSite::Lax => write!(f, "Lax"),
SameSite::None => write!(f, "None"),
}
}
}