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 { session, request_id, ip_address, x_forwarded_for, path, user_agent, accept_language }
78 }
79
80 pub fn clone(&self) -> Self {
81 Self {
82 session: self.session.clone(),
83 request_id: self.request_id.clone(),
84 ip_address: self.ip_address.clone(),
85 x_forwarded_for: self.x_forwarded_for.clone(),
86 path: self.path.clone(),
87 user_agent: self.user_agent.clone(),
88 accept_language: self.accept_language.clone(),
89 }
90 }
91
92 pub fn session(&self) -> Session {
93 self.session.clone()
94 }
95 pub fn request_id(&self) -> &str {
96 &self.request_id
97 }
98 pub fn ip_address(&self) -> &str {
99 &self.ip_address
100 }
101 pub fn x_forwarded_for(&self) -> &str {
102 &self.x_forwarded_for
103 }
104 pub fn path(&self) -> &str {
105 &self.path
106 }
107 pub fn user_agent(&self) -> &str {
108 &self.user_agent
109 }
110 pub fn accept_language(&self) -> &str {
111 &self.accept_language
112 }
113}