systemprompt_identifiers/
user.rs

1//! User 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 UserId(String);
13
14impl UserId {
15    pub fn new(id: impl Into<String>) -> Self {
16        Self(id.into())
17    }
18
19    pub fn anonymous() -> Self {
20        Self("anonymous".to_string())
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    pub fn is_system(&self) -> bool {
32        self.0 == "system"
33    }
34
35    pub fn is_anonymous(&self) -> bool {
36        self.0 == "anonymous"
37    }
38}
39
40impl fmt::Display for UserId {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        write!(f, "{}", self.0)
43    }
44}
45
46impl From<String> for UserId {
47    fn from(s: String) -> Self {
48        Self(s)
49    }
50}
51
52impl From<&str> for UserId {
53    fn from(s: &str) -> Self {
54        Self(s.to_string())
55    }
56}
57
58impl AsRef<str> for UserId {
59    fn as_ref(&self) -> &str {
60        &self.0
61    }
62}
63
64impl ToDbValue for UserId {
65    fn to_db_value(&self) -> DbValue {
66        DbValue::String(self.0.clone())
67    }
68}
69
70impl ToDbValue for &UserId {
71    fn to_db_value(&self) -> DbValue {
72        DbValue::String(self.0.clone())
73    }
74}
75
76impl From<UserId> for String {
77    fn from(id: UserId) -> Self {
78        id.0
79    }
80}
81
82impl From<&UserId> for String {
83    fn from(id: &UserId) -> Self {
84        id.0.clone()
85    }
86}
87
88impl PartialEq<&str> for UserId {
89    fn eq(&self, other: &&str) -> bool {
90        self.0 == *other
91    }
92}
93
94impl PartialEq<str> for UserId {
95    fn eq(&self, other: &str) -> bool {
96        self.0 == other
97    }
98}
99
100impl PartialEq<UserId> for &str {
101    fn eq(&self, other: &UserId) -> bool {
102        *self == other.0
103    }
104}
105
106impl PartialEq<UserId> for str {
107    fn eq(&self, other: &UserId) -> bool {
108        self == other.0
109    }
110}