Skip to main content

axum_security/cookie/
id.rs

1use std::borrow::Cow;
2
3use cookie_monster::Cookie;
4use uuid::Uuid;
5
6#[derive(Debug, Hash, Clone, PartialEq, Eq)]
7pub struct SessionId(Box<str>);
8
9impl SessionId {
10    pub fn new() -> Self {
11        SessionId(Uuid::now_v7().to_string().into_boxed_str())
12    }
13
14    pub fn from_cookie(cookie: &Cookie) -> Self {
15        SessionId(cookie.value().into())
16    }
17
18    pub fn as_str(&self) -> &str {
19        &self.0
20    }
21}
22
23impl From<SessionId> for Cow<'static, str> {
24    fn from(value: SessionId) -> Self {
25        Cow::Owned(value.0.into_string())
26    }
27}
28
29impl From<String> for SessionId {
30    fn from(value: String) -> Self {
31        Self(value.into_boxed_str())
32    }
33}
34
35impl Default for SessionId {
36    fn default() -> Self {
37        Self::new()
38    }
39}