Skip to main content

systemprompt_identifiers/
profile.rs

1//! Profile name identifier type with validation.
2
3use 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    #[expect(
40        clippy::expect_used,
41        reason = "infallible constructor reserved for already-validated inputs; untrusted input \
42                  must go through try_new"
43    )]
44    pub fn new(value: impl Into<String>) -> Self {
45        // SAFETY: `new` is the infallible constructor reserved for inputs the caller
46        // has already validated (compile-time literals, values that
47        // round-tripped through `try_new` at a boundary). Untrusted input must
48        // go through `try_new`.
49        Self::try_new(value).expect("ProfileName validation failed")
50    }
51
52    #[must_use]
53    pub fn as_str(&self) -> &str {
54        &self.0
55    }
56
57    #[must_use]
58    pub fn default_profile() -> Self {
59        Self("default".to_owned())
60    }
61}
62
63impl fmt::Display for ProfileName {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        write!(f, "{}", self.0)
66    }
67}
68
69impl TryFrom<String> for ProfileName {
70    type Error = IdValidationError;
71
72    fn try_from(s: String) -> Result<Self, Self::Error> {
73        Self::try_new(s)
74    }
75}
76
77impl TryFrom<&str> for ProfileName {
78    type Error = IdValidationError;
79
80    fn try_from(s: &str) -> Result<Self, Self::Error> {
81        Self::try_new(s)
82    }
83}
84
85impl std::str::FromStr for ProfileName {
86    type Err = IdValidationError;
87
88    fn from_str(s: &str) -> Result<Self, Self::Err> {
89        Self::try_new(s)
90    }
91}
92
93impl AsRef<str> for ProfileName {
94    fn as_ref(&self) -> &str {
95        &self.0
96    }
97}
98
99impl<'de> Deserialize<'de> for ProfileName {
100    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
101    where
102        D: serde::Deserializer<'de>,
103    {
104        let s = String::deserialize(deserializer)?;
105        Self::try_new(s).map_err(serde::de::Error::custom)
106    }
107}
108
109impl ToDbValue for ProfileName {
110    fn to_db_value(&self) -> DbValue {
111        DbValue::String(self.0.clone())
112    }
113}
114
115impl ToDbValue for &ProfileName {
116    fn to_db_value(&self) -> DbValue {
117        DbValue::String(self.0.clone())
118    }
119}