artifactize_core/config/
identities.rs1use serde::{Deserialize, Serialize};
4use std::{borrow::Borrow, fmt, ops::Deref, str::FromStr};
5
6macro_rules! identity {
7 ($name:ident, $valid:expr) => {
8 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
9 #[serde(transparent)]
10 pub struct $name(String);
11 impl FromStr for $name {
12 type Err = String;
13 fn from_str(value: &str) -> Result<Self, Self::Err> {
14 if ($valid)(value) {
15 Ok(Self(value.to_owned()))
16 } else {
17 Err(format!("Invalid {}: {value:?}", stringify!($name)))
18 }
19 }
20 }
21 impl<'de> Deserialize<'de> for $name {
22 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
23 use serde::de::Error;
24 String::deserialize(deserializer)?
25 .parse()
26 .map_err(D::Error::custom)
27 }
28 }
29 impl $name {
30 pub fn as_str(&self) -> &str {
31 &self.0
32 }
33 }
34 impl Deref for $name {
35 type Target = str;
36 fn deref(&self) -> &str {
37 &self.0
38 }
39 }
40 impl AsRef<str> for $name {
41 fn as_ref(&self) -> &str {
42 &self.0
43 }
44 }
45 impl Borrow<str> for $name {
46 fn borrow(&self) -> &str {
47 &self.0
48 }
49 }
50 impl Borrow<String> for $name {
51 fn borrow(&self) -> &String {
52 &self.0
53 }
54 }
55 impl fmt::Display for $name {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 self.0.fmt(f)
58 }
59 }
60 impl PartialEq<str> for $name {
61 fn eq(&self, other: &str) -> bool {
62 self.0 == other
63 }
64 }
65 impl PartialEq<&str> for $name {
66 fn eq(&self, other: &&str) -> bool {
67 self.0 == *other
68 }
69 }
70 impl PartialEq<String> for $name {
71 fn eq(&self, other: &String) -> bool {
72 &self.0 == other
73 }
74 }
75 impl PartialEq<$name> for String {
76 fn eq(&self, other: &$name) -> bool {
77 *self == other.0
78 }
79 }
80 impl PartialEq<$name> for str {
81 fn eq(&self, other: &$name) -> bool {
82 self == other.0
83 }
84 }
85 impl PartialEq<$name> for &str {
86 fn eq(&self, other: &$name) -> bool {
87 *self == other.0
88 }
89 }
90 };
91}
92const MAX_TOOL_NAME_BYTES: usize = 129;
94const ENDPOINT_HEX_BYTES: usize = 64;
96
97fn name(value: &str) -> bool {
98 super::identifier(value, "").is_ok()
99}
100identity!(ModelId, |value: &str| !value.trim().is_empty());
101identity!(ProfileAliasName, name);
102identity!(ProfileVariantName, name);
103identity!(ToolOperationName, name);
104identity!(ToolName, |value: &str| !value.is_empty()
107 && value.len() <= MAX_TOOL_NAME_BYTES
108 && value.bytes().all(
109 |byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-')
110 ));
111identity!(ExecutionPinName, |value: &str| value
113 .strip_suffix(".start")
114 .or_else(|| value.strip_suffix(".stop"))
115 .unwrap_or(value)
116 .parse::<ToolName>()
117 .is_ok());
118identity!(EndpointId, |value: &str| value.len() == ENDPOINT_HEX_BYTES
119 && value.bytes().all(
120 |byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase()
121 ));
122identity!(HubEpoch, |value: &str| !value.is_empty()
123 && value.len() <= crate::types::MAX_ID_BYTES
124 && value.bytes().all(
125 |byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-')
126 ));
127
128identity!(ReferenceName, |value: &str| super::artifact_name(value, "")
130 .is_ok());
131impl From<&crate::types::ArtifactName> for ReferenceName {
132 fn from(value: &crate::types::ArtifactName) -> Self {
133 Self(value.to_string())
135 }
136}
137identity!(ChildPrefix, |value: &str| crate::config::validation::path(
138 value
139)
140.is_ok());
141pub use artifactize_tools::scope::LogicalPath;
142
143#[cfg(test)]
144mod tests {
145 use super::*;
146 use serde_json::json;
147
148 #[test]
149 fn declaration_ids_validate_at_json_boundary_and_keep_string_forms() {
150 for invalid in ["", " ", "bad/name", "_bad", "a\nb"] {
151 assert!(serde_json::from_value::<ProfileVariantName>(json!(invalid)).is_err());
152 assert!(serde_json::from_value::<ToolOperationName>(json!(invalid)).is_err());
153 }
154 let model: ModelId = serde_json::from_value(json!("provider/model-v1")).unwrap();
155 assert_eq!(
156 serde_json::to_value(model).unwrap(),
157 json!("provider/model-v1")
158 );
159 for invalid in [
160 "",
161 "a/../b",
162 "a\\b",
163 &crate::test_os::windows_drive("outside"),
164 ] {
165 assert!(serde_json::from_value::<LogicalPath>(json!(invalid)).is_err());
166 }
167 for logical in [".", "folder/file.txt"] {
168 let path: LogicalPath = serde_json::from_value(json!(logical)).unwrap();
169 assert_eq!(serde_json::to_value(path).unwrap(), json!(logical));
170 }
171 }
172}
173
174const MAX_ARTIFACT_TYPE_BYTES: usize = 64;
176
177identity!(ArtifactType, |value: &str| {
178 value.len() <= MAX_ARTIFACT_TYPE_BYTES
179 && value
180 .bytes()
181 .next()
182 .is_some_and(|byte| byte.is_ascii_lowercase())
183 && value
184 .bytes()
185 .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
186 && crate::platform::paths::portable_filename(value).is_ok()
187});