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