artifactize_provider/
names.rs1use serde::{Deserialize, Serialize};
4
5#[derive(
7 Debug,
8 Clone,
9 Copy,
10 PartialEq,
11 Eq,
12 PartialOrd,
13 Ord,
14 Hash,
15 Serialize,
16 Deserialize,
17 clap::ValueEnum,
18)]
19#[serde(rename_all = "lowercase")]
20pub enum HttpProvider {
21 Codex,
22 Openai,
23 Anthropic,
24 Gemini,
25}
26
27impl HttpProvider {
28 pub const ALL: [Self; 4] = [Self::Codex, Self::Openai, Self::Anthropic, Self::Gemini];
29
30 pub fn as_str(self) -> &'static str {
31 match self {
32 Self::Codex => "codex",
33 Self::Openai => "openai",
34 Self::Anthropic => "anthropic",
35 Self::Gemini => "gemini",
36 }
37 }
38
39 pub fn named(name: &str) -> Option<Self> {
40 Self::ALL
41 .into_iter()
42 .find(|provider| provider.as_str() == name)
43 }
44}
45
46impl std::fmt::Display for HttpProvider {
47 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48 formatter.write_str(self.as_str())
49 }
50}
51
52pub fn is_reserved_custom(name: &str) -> bool {
55 HttpProvider::ALL
56 .iter()
57 .any(|provider| provider.as_str() == name)
58 || crate::agents::agent(name).is_some()
59}
60
61#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
64#[serde(transparent)]
65pub struct CustomBackendName(String);
66
67impl CustomBackendName {
68 pub fn as_str(&self) -> &str {
69 &self.0
70 }
71}
72
73impl std::str::FromStr for CustomBackendName {
74 type Err = String;
75
76 fn from_str(name: &str) -> Result<Self, Self::Err> {
77 if is_reserved_custom(name) {
78 return Err(format!(
79 "{name} is a built-in provider name and cannot be used as a custom HTTP backend."
80 ));
81 }
82 const MAX_NAME_BYTES: usize = 64;
85 if name.is_empty()
86 || name.len() > MAX_NAME_BYTES
87 || !name.as_bytes()[0].is_ascii_alphanumeric()
88 || !name
89 .bytes()
90 .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-'))
91 {
92 return Err("custom backend must match [A-Za-z0-9][A-Za-z0-9_-]{0,63}.".into());
93 }
94 Ok(Self(name.to_owned()))
95 }
96}
97
98impl<'de> Deserialize<'de> for CustomBackendName {
99 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
100 String::deserialize(deserializer)?
101 .parse()
102 .map_err(serde::de::Error::custom)
103 }
104}
105
106impl std::borrow::Borrow<str> for CustomBackendName {
107 fn borrow(&self) -> &str {
108 self.as_str()
109 }
110}
111
112impl std::fmt::Display for CustomBackendName {
113 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
114 formatter.write_str(&self.0)
115 }
116}
117
118#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
120#[serde(tag = "kind", rename_all = "camelCase")]
121pub enum DirectBackend {
122 Builtin {
123 provider: HttpProvider,
124 },
125 Custom {
127 name: CustomBackendName,
128 },
129}
130
131impl DirectBackend {
132 pub fn as_str(&self) -> &str {
133 match self {
134 Self::Builtin { provider } => provider.as_str(),
135 Self::Custom { name } => name.as_str(),
136 }
137 }
138}
139
140impl std::fmt::Display for DirectBackend {
141 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
142 formatter.write_str(self.as_str())
143 }
144}
145
146use std::{borrow::Borrow, fmt, ops::Deref, str::FromStr};
147
148macro_rules! identity {
149 ($name:ident, $valid:expr) => {
150 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
151 #[serde(transparent)]
152 pub struct $name(String);
153 impl FromStr for $name {
154 type Err = String;
155 fn from_str(value: &str) -> Result<Self, Self::Err> {
156 if ($valid)(value) {
157 Ok(Self(value.to_owned()))
158 } else {
159 Err(format!("Invalid {}: {value:?}", stringify!($name)))
160 }
161 }
162 }
163 impl<'de> Deserialize<'de> for $name {
164 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
165 use serde::de::Error;
166 String::deserialize(deserializer)?
167 .parse()
168 .map_err(D::Error::custom)
169 }
170 }
171 impl $name {
172 pub fn as_str(&self) -> &str {
173 &self.0
174 }
175 }
176 impl Deref for $name {
177 type Target = str;
178 fn deref(&self) -> &str {
179 &self.0
180 }
181 }
182 impl AsRef<str> for $name {
183 fn as_ref(&self) -> &str {
184 &self.0
185 }
186 }
187 impl Borrow<str> for $name {
188 fn borrow(&self) -> &str {
189 &self.0
190 }
191 }
192 impl Borrow<String> for $name {
193 fn borrow(&self) -> &String {
194 &self.0
195 }
196 }
197 impl fmt::Display for $name {
198 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199 self.0.fmt(f)
200 }
201 }
202 impl PartialEq<str> for $name {
203 fn eq(&self, other: &str) -> bool {
204 self.0 == other
205 }
206 }
207 impl PartialEq<&str> for $name {
208 fn eq(&self, other: &&str) -> bool {
209 self.0 == *other
210 }
211 }
212 impl PartialEq<String> for $name {
213 fn eq(&self, other: &String) -> bool {
214 &self.0 == other
215 }
216 }
217 impl PartialEq<$name> for String {
218 fn eq(&self, other: &$name) -> bool {
219 *self == other.0
220 }
221 }
222 impl PartialEq<$name> for str {
223 fn eq(&self, other: &$name) -> bool {
224 self == other.0
225 }
226 }
227 impl PartialEq<$name> for &str {
228 fn eq(&self, other: &$name) -> bool {
229 *self == other.0
230 }
231 }
232 };
233}
234identity!(ModelId, |value: &str| !value.trim().is_empty());
235
236pub(crate) const ITEM_ID_BYTES: usize = 1024;
238const OAUTH_STATE_HEX_BYTES: usize = 32;
239identity!(OAuthState, |value: &str| value.len()
242 == OAUTH_STATE_HEX_BYTES
243 && value
244 .bytes()
245 .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)));
246identity!(ItemId, |value: &str| value.len() <= ITEM_ID_BYTES);
249identity!(CodexAccountId, |value: &str| !value.is_empty());