sa_token_adapter/
context.rs1use std::collections::HashMap;
6use serde::{Deserialize, Serialize};
7
8pub trait SaRequest {
12 fn get_header(&self, name: &str) -> Option<String>;
14
15 fn get_headers(&self) -> HashMap<String, String> {
17 HashMap::new() }
19
20 fn get_cookie(&self, name: &str) -> Option<String>;
22
23 fn get_cookies(&self) -> HashMap<String, String> {
25 HashMap::new() }
27
28 fn get_param(&self, name: &str) -> Option<String>;
30
31 fn get_params(&self) -> HashMap<String, String> {
33 HashMap::new() }
35
36 fn get_path(&self) -> String;
38
39 fn get_method(&self) -> String;
41
42 fn get_uri(&self) -> String {
44 self.get_path()
45 }
46
47 fn get_body_json<T: for<'de> Deserialize<'de>>(&self) -> Option<T> {
49 None }
51
52 fn get_client_ip(&self) -> Option<String> {
54 None }
56
57 fn get_user_agent(&self) -> Option<String> {
59 self.get_header("user-agent")
60 }
61}
62
63pub trait SaResponse {
67 fn set_header(&mut self, name: &str, value: &str);
69
70 fn set_cookie(&mut self, name: &str, value: &str, options: CookieOptions);
72
73 fn delete_cookie(&mut self, name: &str) {
75 self.set_cookie(name, "", CookieOptions {
76 max_age: Some(0),
77 ..Default::default()
78 });
79 }
80
81 fn set_status(&mut self, status: u16);
83
84 fn set_json_body<T: Serialize>(&mut self, body: T) -> Result<(), serde_json::Error>;
86}
87
88#[derive(Debug, Clone, Default)]
90pub struct CookieOptions {
91 pub domain: Option<String>,
93
94 pub path: Option<String>,
96
97 pub max_age: Option<i64>,
99
100 pub http_only: bool,
102
103 pub secure: bool,
105
106 pub same_site: Option<SameSite>,
108}
109
110#[derive(Debug, Clone, Copy)]
112pub enum SameSite {
113 Strict,
114 Lax,
115 None,
116}
117
118impl std::fmt::Display for SameSite {
119 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120 match self {
121 SameSite::Strict => write!(f, "Strict"),
122 SameSite::Lax => write!(f, "Lax"),
123 SameSite::None => write!(f, "None"),
124 }
125 }
126}