use serde::{Deserialize, Serialize};
use std::{borrow::Borrow, fmt, ops::Deref, str::FromStr};
macro_rules! identity {
($name:ident, $valid:expr) => {
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct $name(String);
impl FromStr for $name {
type Err = String;
fn from_str(value: &str) -> Result<Self, Self::Err> {
if ($valid)(value) {
Ok(Self(value.to_owned()))
} else {
Err(format!("Invalid {}: {value:?}", stringify!($name)))
}
}
}
impl<'de> Deserialize<'de> for $name {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
use serde::de::Error;
String::deserialize(deserializer)?
.parse()
.map_err(D::Error::custom)
}
}
impl $name {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Deref for $name {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
impl AsRef<str> for $name {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Borrow<str> for $name {
fn borrow(&self) -> &str {
&self.0
}
}
impl Borrow<String> for $name {
fn borrow(&self) -> &String {
&self.0
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl PartialEq<str> for $name {
fn eq(&self, other: &str) -> bool {
self.0 == other
}
}
impl PartialEq<&str> for $name {
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}
impl PartialEq<String> for $name {
fn eq(&self, other: &String) -> bool {
&self.0 == other
}
}
impl PartialEq<$name> for String {
fn eq(&self, other: &$name) -> bool {
*self == other.0
}
}
impl PartialEq<$name> for str {
fn eq(&self, other: &$name) -> bool {
self == other.0
}
}
impl PartialEq<$name> for &str {
fn eq(&self, other: &$name) -> bool {
*self == other.0
}
}
};
}
const MAX_TOOL_NAME_BYTES: usize = MAX_TOOL_FQN_BYTES + 1 + 64;
const MAX_TOOL_FQN_BYTES: usize = 128;
const ENDPOINT_HEX_BYTES: usize = 64;
fn name(value: &str) -> bool {
super::identifier(value, "").is_ok()
}
pub use artifactize_provider::names::ModelId;
identity!(ProfileName, name);
identity!(ToolFqn, |value: &str| ToolFqn::namespace(value).is_ok());
identity!(ToolName, |value: &str| !value.is_empty()
&& value.len() <= MAX_TOOL_NAME_BYTES
&& value.bytes().all(
|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-')
));
identity!(ExecutionPinName, |value: &str| value
.strip_suffix(":start")
.or_else(|| value.strip_suffix(":stop"))
.unwrap_or(value)
.parse::<ToolName>()
.is_ok());
identity!(EndpointId, |value: &str| value.len() == ENDPOINT_HEX_BYTES
&& value.bytes().all(
|byte| byte.is_ascii_hexdigit() && !byte.is_ascii_uppercase()
));
identity!(HubEpoch, |value: &str| !value.is_empty()
&& value.len() <= crate::types::MAX_ID_BYTES
&& value.bytes().all(
|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-')
));
identity!(ReferenceName, |value: &str| super::artifact_name(value, "")
.is_ok());
impl From<&crate::types::ArtifactName> for ReferenceName {
fn from(value: &crate::types::ArtifactName) -> Self {
Self(value.to_string())
}
}
identity!(ChildPrefix, |value: &str| crate::config::validation::path(
value
)
.is_ok());
pub use artifactize_tools::scope::LogicalPath;
#[cfg(test)]
mod tests;
const MAX_ARTIFACT_TYPE_BYTES: usize = 64;
identity!(PluginName, |value: &str| {
value.len() <= MAX_ARTIFACT_TYPE_BYTES
&& value
.bytes()
.next()
.is_some_and(|byte| byte.is_ascii_lowercase())
&& value
.bytes()
.all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
&& crate::platform::paths::portable_component(std::ffi::OsStr::new(value)).is_ok()
});
impl ToolFqn {
pub fn namespace(value: &str) -> Result<(), String> {
if value.is_empty() || value.len() > MAX_TOOL_FQN_BYTES {
return Err(format!(
"A tool name is 1–{MAX_TOOL_FQN_BYTES} bytes: {value:?}."
));
}
for segment in value.split('.') {
if !segment
.as_bytes()
.first()
.is_some_and(u8::is_ascii_alphabetic)
{
return Err(format!(
"Each part of a tool name starts with a letter: {value:?}."
));
}
super::identifier(segment, "A tool name's part")?;
}
Ok(())
}
pub fn namespace_part(&self) -> &str {
self.0
.rsplit_once('.')
.map_or("", |(namespace, _)| namespace)
}
pub fn published(&self, artifact: &str) -> ToolName {
format!("{}_{artifact}", self.0.replace('.', "_"))
.parse()
.expect("a validated FQN and Artifact name form a provider-safe tool name")
}
}