systemprompt_identifiers/
profile.rs1use crate::error::IdValidationError;
4use crate::{DbValue, ToDbValue};
5use serde::{Deserialize, Serialize};
6use std::fmt;
7
8#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
9#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
10#[cfg_attr(feature = "sqlx", sqlx(transparent))]
11#[serde(transparent)]
12pub struct ProfileName(String);
13
14impl ProfileName {
15 pub fn try_new(value: impl Into<String>) -> Result<Self, IdValidationError> {
16 let value = value.into();
17 if value.is_empty() {
18 return Err(IdValidationError::empty("ProfileName"));
19 }
20 if value.contains('/') {
21 return Err(IdValidationError::invalid(
22 "ProfileName",
23 "cannot contain path separator '/'",
24 ));
25 }
26 if !value
27 .chars()
28 .all(|c| c.is_alphanumeric() || c == '-' || c == '_')
29 {
30 return Err(IdValidationError::invalid(
31 "ProfileName",
32 "can only contain alphanumeric characters, hyphens, and underscores",
33 ));
34 }
35 Ok(Self(value))
36 }
37
38 #[must_use]
39 #[allow(clippy::expect_used)]
40 pub fn new(value: impl Into<String>) -> Self {
41 Self::try_new(value).expect("ProfileName validation failed")
42 }
43
44 #[must_use]
45 pub fn as_str(&self) -> &str {
46 &self.0
47 }
48
49 #[must_use]
50 pub fn default_profile() -> Self {
51 Self("default".to_string())
52 }
53}
54
55impl fmt::Display for ProfileName {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 write!(f, "{}", self.0)
58 }
59}
60
61impl TryFrom<String> for ProfileName {
62 type Error = IdValidationError;
63
64 fn try_from(s: String) -> Result<Self, Self::Error> {
65 Self::try_new(s)
66 }
67}
68
69impl TryFrom<&str> for ProfileName {
70 type Error = IdValidationError;
71
72 fn try_from(s: &str) -> Result<Self, Self::Error> {
73 Self::try_new(s)
74 }
75}
76
77impl std::str::FromStr for ProfileName {
78 type Err = IdValidationError;
79
80 fn from_str(s: &str) -> Result<Self, Self::Err> {
81 Self::try_new(s)
82 }
83}
84
85impl AsRef<str> for ProfileName {
86 fn as_ref(&self) -> &str {
87 &self.0
88 }
89}
90
91impl<'de> Deserialize<'de> for ProfileName {
92 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
93 where
94 D: serde::Deserializer<'de>,
95 {
96 let s = String::deserialize(deserializer)?;
97 Self::try_new(s).map_err(serde::de::Error::custom)
98 }
99}
100
101impl ToDbValue for ProfileName {
102 fn to_db_value(&self) -> DbValue {
103 DbValue::String(self.0.clone())
104 }
105}
106
107impl ToDbValue for &ProfileName {
108 fn to_db_value(&self) -> DbValue {
109 DbValue::String(self.0.clone())
110 }
111}