systemprompt_identifiers/
locale.rs1use crate::error::IdValidationError;
11use crate::{DbValue, ToDbValue};
12use schemars::JsonSchema;
13use serde::{Deserialize, Serialize};
14use std::fmt;
15
16const MAX_LEN: usize = 35;
17
18#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, JsonSchema)]
19#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
20#[cfg_attr(feature = "sqlx", sqlx(transparent))]
21#[serde(transparent)]
22pub struct LocaleCode(String);
23
24impl LocaleCode {
25 pub fn try_new(value: impl Into<String>) -> Result<Self, IdValidationError> {
26 let value = value.into();
27 if value.is_empty() {
28 return Err(IdValidationError::empty("LocaleCode"));
29 }
30 if value.len() > MAX_LEN {
31 return Err(IdValidationError::invalid(
32 "LocaleCode",
33 "exceeds 35 characters",
34 ));
35 }
36 let mut subtags = value.split('-');
37 let primary = subtags.next().unwrap_or("");
38 let plen = primary.len();
39 if !(2..=3).contains(&plen) || !primary.chars().all(|c| c.is_ascii_lowercase()) {
40 return Err(IdValidationError::invalid(
41 "LocaleCode",
42 "primary subtag must be 2-3 lowercase ASCII letters",
43 ));
44 }
45 for sub in subtags {
46 let len = sub.len();
47 if !(2..=8).contains(&len) || !sub.chars().all(|c| c.is_ascii_alphanumeric()) {
48 return Err(IdValidationError::invalid(
49 "LocaleCode",
50 "subtag must be 2-8 alphanumeric ASCII characters",
51 ));
52 }
53 }
54 Ok(Self(value))
55 }
56
57 #[must_use]
58 #[allow(clippy::expect_used)]
59 pub fn new(value: impl Into<String>) -> Self {
60 Self::try_new(value).expect("LocaleCode validation failed")
65 }
66
67 #[must_use]
68 pub fn as_str(&self) -> &str {
69 &self.0
70 }
71}
72
73impl fmt::Display for LocaleCode {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 write!(f, "{}", self.0)
76 }
77}
78
79impl TryFrom<String> for LocaleCode {
80 type Error = IdValidationError;
81
82 fn try_from(s: String) -> Result<Self, Self::Error> {
83 Self::try_new(s)
84 }
85}
86
87impl TryFrom<&str> for LocaleCode {
88 type Error = IdValidationError;
89
90 fn try_from(s: &str) -> Result<Self, Self::Error> {
91 Self::try_new(s)
92 }
93}
94
95impl std::str::FromStr for LocaleCode {
96 type Err = IdValidationError;
97
98 fn from_str(s: &str) -> Result<Self, Self::Err> {
99 Self::try_new(s)
100 }
101}
102
103impl AsRef<str> for LocaleCode {
104 fn as_ref(&self) -> &str {
105 &self.0
106 }
107}
108
109impl<'de> Deserialize<'de> for LocaleCode {
110 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
111 where
112 D: serde::Deserializer<'de>,
113 {
114 let s = String::deserialize(deserializer)?;
115 Self::try_new(s).map_err(serde::de::Error::custom)
116 }
117}
118
119impl ToDbValue for LocaleCode {
120 fn to_db_value(&self) -> DbValue {
121 DbValue::String(self.0.clone())
122 }
123}
124
125impl ToDbValue for &LocaleCode {
126 fn to_db_value(&self) -> DbValue {
127 DbValue::String(self.0.clone())
128 }
129}