use std::fmt;
use std::str::FromStr;
use super::kebab::check_kebab_name;
use crate::types::name::InvalidName;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ToolName(String);
impl ToolName {
pub fn new(value: &str) -> Result<Self, InvalidName> {
check_kebab_name(value)?;
Ok(Self(value.to_owned()))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl FromStr for ToolName {
type Err = InvalidName;
fn from_str(value: &str) -> Result<Self, Self::Err> {
Self::new(value)
}
}
impl TryFrom<&str> for ToolName {
type Error = InvalidName;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::new(value)
}
}
impl fmt::Display for ToolName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tool_name_parses_and_refuses() {
assert_eq!(
ToolName::new("claude").as_ref().map(ToolName::as_str),
Ok("claude")
);
assert_eq!(
ToolName::new("Claude").map_err(|e| e.to_string()),
Err("\"Claude\" is not a valid name".to_owned())
);
}
#[test]
fn from_str_try_from_and_display_agree_with_new() {
assert_eq!("claude".parse::<ToolName>(), ToolName::new("claude"));
assert_eq!(ToolName::try_from("claude"), ToolName::new("claude"));
assert_eq!(
ToolName::new("claude").map(|n| n.to_string()),
Ok("claude".to_owned())
);
}
}