Skip to main content

systemprompt_identifiers/
session.rs

1//! Session identifier type.
2
3use crate::{DbValue, ToDbValue};
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6use std::fmt;
7
8#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
9#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
10#[cfg_attr(feature = "sqlx", sqlx(transparent))]
11#[serde(transparent)]
12pub struct SessionId(String);
13
14impl SessionId {
15    pub fn new(id: impl Into<String>) -> Self {
16        Self(id.into())
17    }
18
19    pub fn generate() -> Self {
20        Self(format!("sess_{}", uuid::Uuid::new_v4()))
21    }
22
23    pub fn system() -> Self {
24        Self("system".to_string())
25    }
26
27    pub fn as_str(&self) -> &str {
28        &self.0
29    }
30}
31
32impl fmt::Display for SessionId {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        write!(f, "{}", self.0)
35    }
36}
37
38impl From<String> for SessionId {
39    fn from(s: String) -> Self {
40        Self(s)
41    }
42}
43
44impl From<&str> for SessionId {
45    fn from(s: &str) -> Self {
46        Self(s.to_string())
47    }
48}
49
50impl AsRef<str> for SessionId {
51    fn as_ref(&self) -> &str {
52        &self.0
53    }
54}
55
56impl ToDbValue for SessionId {
57    fn to_db_value(&self) -> DbValue {
58        DbValue::String(self.0.clone())
59    }
60}
61
62impl ToDbValue for &SessionId {
63    fn to_db_value(&self) -> DbValue {
64        DbValue::String(self.0.clone())
65    }
66}
67
68#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
69#[serde(rename_all = "lowercase")]
70pub enum SessionSource {
71    Web,
72    Api,
73    Cli,
74    Tui,
75    Oauth,
76    Mcp,
77    #[default]
78    Unknown,
79}
80
81impl SessionSource {
82    pub const fn as_str(self) -> &'static str {
83        match self {
84            Self::Web => "web",
85            Self::Api => "api",
86            Self::Cli => "cli",
87            Self::Tui => "tui",
88            Self::Oauth => "oauth",
89            Self::Mcp => "mcp",
90            Self::Unknown => "unknown",
91        }
92    }
93
94    pub fn from_client_id(client_id: &str) -> Self {
95        match client_id {
96            "sp_web" => Self::Web,
97            "sp_cli" => Self::Cli,
98            "sp_tui" => Self::Tui,
99            _ => Self::Unknown,
100        }
101    }
102}
103
104impl fmt::Display for SessionSource {
105    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106        write!(f, "{}", self.as_str())
107    }
108}
109
110impl std::str::FromStr for SessionSource {
111    type Err = std::convert::Infallible;
112
113    fn from_str(s: &str) -> Result<Self, Self::Err> {
114        Ok(match s.to_lowercase().as_str() {
115            "web" => Self::Web,
116            "api" => Self::Api,
117            "cli" => Self::Cli,
118            "tui" => Self::Tui,
119            "oauth" => Self::Oauth,
120            "mcp" => Self::Mcp,
121            _ => Self::Unknown,
122        })
123    }
124}