Skip to main content

systemprompt_identifiers/
session.rs

1crate::define_id!(SessionId, schema);
2
3impl SessionId {
4    pub fn generate() -> Self {
5        Self(format!("sess_{}", uuid::Uuid::new_v4()))
6    }
7
8    pub fn system() -> Self {
9        Self("system".to_string())
10    }
11}
12
13#[derive(
14    Debug, Clone, Copy, Default, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
15)]
16#[serde(rename_all = "lowercase")]
17pub enum SessionSource {
18    Web,
19    Api,
20    Cli,
21    Oauth,
22    Mcp,
23    #[default]
24    Unknown,
25}
26
27impl SessionSource {
28    pub const fn as_str(self) -> &'static str {
29        match self {
30            Self::Web => "web",
31            Self::Api => "api",
32            Self::Cli => "cli",
33            Self::Oauth => "oauth",
34            Self::Mcp => "mcp",
35            Self::Unknown => "unknown",
36        }
37    }
38
39    pub fn from_client_id(client_id: &str) -> Self {
40        match client_id {
41            "sp_web" => Self::Web,
42            "sp_cli" => Self::Cli,
43            _ => Self::Unknown,
44        }
45    }
46}
47
48impl std::fmt::Display for SessionSource {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        write!(f, "{}", self.as_str())
51    }
52}
53
54impl std::str::FromStr for SessionSource {
55    type Err = std::convert::Infallible;
56
57    fn from_str(s: &str) -> Result<Self, Self::Err> {
58        Ok(match s.to_lowercase().as_str() {
59            "web" => Self::Web,
60            "api" => Self::Api,
61            "cli" => Self::Cli,
62            "oauth" => Self::Oauth,
63            "mcp" => Self::Mcp,
64            _ => Self::Unknown,
65        })
66    }
67}