megacommerce_shared/models/
context.rs1use std::collections::HashMap;
2
3use derive_more::Display;
4
5pub type StringMap = HashMap<String, String>;
6
7#[derive(Clone, Debug, Default, Display)]
8#[display("Session: {id} {token} {created_at} {expires_at} {last_activity_at} {user_id} {device_id} {roles} {is_oauth} {props:?}")]
9pub struct Session {
10 pub id: String,
11 pub token: String,
12 pub created_at: i64,
13 pub expires_at: i64,
14 pub last_activity_at: i64,
15 pub user_id: String,
16 pub device_id: String,
17 pub roles: String,
18 pub is_oauth: bool,
19 pub props: StringMap,
20}
21
22impl Session {
23 pub fn id(&self) -> &str {
24 &self.id
25 }
26 pub fn token(&self) -> &str {
27 &self.token
28 }
29 pub fn created_at(&self) -> f64 {
30 self.created_at as f64
31 }
32 pub fn expires_at(&self) -> f64 {
33 self.expires_at as f64
34 }
35 pub fn last_activity_at(&self) -> f64 {
36 self.last_activity_at as f64
37 }
38 pub fn user_id(&self) -> &str {
39 &self.user_id
40 }
41 pub fn device_id(&self) -> &str {
42 &self.device_id
43 }
44 pub fn roles(&self) -> &str {
45 &self.roles
46 }
47 pub fn is_oauth(&self) -> bool {
48 self.is_oauth
49 }
50 pub fn props(&self) -> &StringMap {
51 &self.props
52 }
53}
54
55#[derive(Clone, Debug, Default, Display)]
56#[display("Context: {session} {request_id} {ip_address} {x_forwarded_for} {path} {user_agent} {accept_language}")]
57pub struct Context {
58 pub session: Session,
59 pub request_id: String,
60 pub ip_address: String,
61 pub x_forwarded_for: String,
62 pub path: String,
63 pub user_agent: String,
64 pub accept_language: String,
65}
66
67impl Context {
68 pub fn new(
69 session: Session,
70 request_id: String,
71 ip_address: String,
72 x_forwarded_for: String,
73 path: String,
74 user_agent: String,
75 accept_language: String,
76 ) -> Self {
77 Self {
78 session,
79 request_id,
80 ip_address,
81 x_forwarded_for,
82 path,
83 user_agent,
84 accept_language,
85 }
86 }
87
88 pub fn clone(&self) -> Self {
89 Self {
90 session: self.session.clone(),
91 request_id: self.request_id.clone(),
92 ip_address: self.ip_address.clone(),
93 x_forwarded_for: self.x_forwarded_for.clone(),
94 path: self.path.clone(),
95 user_agent: self.user_agent.clone(),
96 accept_language: self.accept_language.clone(),
97 }
98 }
99
100 pub fn session(&self) -> Session {
101 self.session.clone()
102 }
103 pub fn request_id(&self) -> &str {
104 &self.request_id
105 }
106 pub fn ip_address(&self) -> &str {
107 &self.ip_address
108 }
109 pub fn x_forwarded_for(&self) -> &str {
110 &self.x_forwarded_for
111 }
112 pub fn path(&self) -> &str {
113 &self.path
114 }
115 pub fn user_agent(&self) -> &str {
116 &self.user_agent
117 }
118 pub fn accept_language(&self) -> &str {
119 &self.accept_language
120 }
121}
122