cryo_sessions/
session.rs

1use rand::{distributions::Alphanumeric, thread_rng, Rng};
2
3use crate::Uuid;
4
5#[derive(Debug, Clone)]
6pub struct SessionInfo {
7    uuid: Uuid,
8    user_agent: String
9}
10
11impl SessionInfo {
12    pub fn new(uuid: Uuid, user_agent: String) -> Self {
13        Self {
14            uuid,
15            user_agent
16        }
17    }
18    pub fn uuid(&self) -> &Uuid {
19        &self.uuid
20    }
21    pub fn user_agent(&self) -> &str {
22        &self.user_agent
23    }
24}
25
26#[derive(Debug, Clone)]
27pub struct Session {
28    session: String,
29}
30
31impl Session {
32    pub fn new() -> Self {
33        Self {
34            session: thread_rng()
35                .sample_iter(&Alphanumeric)
36                .take(64)
37                .map(char::from)
38                .collect(),
39        }
40    }
41    pub fn session(&self) -> &str {
42        &self.session
43    }
44}
45
46impl Default for Session {
47    fn default() -> Self {
48        Self::new()
49    }
50}
51
52impl From<String> for Session {
53    fn from(value: String) -> Self {
54        Self {
55            session: value
56        }
57    }
58}
59
60impl From<&str> for Session {
61    fn from(value: &str) -> Self {
62        Self {
63            session: value.into()
64        }
65    }
66}