use std::borrow::Borrow;
use std::fmt;
use std::str::FromStr;
use std::sync::Arc;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ToolName(Arc<str>);
impl ToolName {
#[must_use]
pub fn new(s: impl Into<Arc<str>>) -> Self {
Self(s.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Default for ToolName {
fn default() -> Self {
Self(Arc::from(""))
}
}
impl fmt::Display for ToolName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl AsRef<str> for ToolName {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Borrow<str> for ToolName {
fn borrow(&self) -> &str {
&self.0
}
}
impl From<&str> for ToolName {
fn from(s: &str) -> Self {
Self(Arc::from(s))
}
}
impl From<String> for ToolName {
fn from(s: String) -> Self {
Self(Arc::from(s.as_str()))
}
}
impl FromStr for ToolName {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from(s))
}
}
impl PartialEq<str> for ToolName {
fn eq(&self, other: &str) -> bool {
self.0.as_ref() == other
}
}
impl PartialEq<&str> for ToolName {
fn eq(&self, other: &&str) -> bool {
self.0.as_ref() == *other
}
}
impl PartialEq<String> for ToolName {
fn eq(&self, other: &String) -> bool {
self.0.as_ref() == other.as_str()
}
}
impl PartialEq<ToolName> for str {
fn eq(&self, other: &ToolName) -> bool {
self == other.0.as_ref()
}
}
impl PartialEq<ToolName> for String {
fn eq(&self, other: &ToolName) -> bool {
self.as_str() == other.0.as_ref()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct SessionId(String);
impl SessionId {
pub fn new(s: impl Into<String>) -> Self {
let s = s.into();
debug_assert!(!s.is_empty(), "SessionId must not be empty");
Self(s)
}
#[must_use]
pub fn generate() -> Self {
Self(uuid::Uuid::new_v4().to_string())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Default for SessionId {
fn default() -> Self {
Self::generate()
}
}
impl fmt::Display for SessionId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl AsRef<str> for SessionId {
fn as_ref(&self) -> &str {
&self.0
}
}
impl std::ops::Deref for SessionId {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}
impl From<String> for SessionId {
fn from(s: String) -> Self {
Self::new(s)
}
}
impl From<&str> for SessionId {
fn from(s: &str) -> Self {
Self::new(s)
}
}
impl From<uuid::Uuid> for SessionId {
fn from(u: uuid::Uuid) -> Self {
Self(u.to_string())
}
}
impl FromStr for SessionId {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::new(s))
}
}
impl PartialEq<str> for SessionId {
fn eq(&self, other: &str) -> bool {
self.0 == other
}
}
impl PartialEq<&str> for SessionId {
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}
impl PartialEq<String> for SessionId {
fn eq(&self, other: &String) -> bool {
self.0 == *other
}
}
impl PartialEq<SessionId> for str {
fn eq(&self, other: &SessionId) -> bool {
self == other.0
}
}
impl PartialEq<SessionId> for String {
fn eq(&self, other: &SessionId) -> bool {
*self == other.0
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ToolDefinition {
pub name: ToolName,
pub description: String,
pub parameters: serde_json::Value,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tool_name_construction_and_equality() {
let name = ToolName::new("shell");
assert_eq!(name.as_str(), "shell");
assert_eq!(name, "shell");
assert_eq!(name, "shell".to_owned());
assert_eq!(name, "shell"); }
#[test]
fn tool_name_clone_is_cheap() {
let name = ToolName::new("web_scrape");
let name2 = name.clone();
assert_eq!(name, name2);
assert!(Arc::ptr_eq(&name.0, &name2.0));
}
#[test]
fn tool_name_from_impls() {
let from_str: ToolName = ToolName::from("bash");
let from_string: ToolName = ToolName::from("bash".to_owned());
let parsed: ToolName = "bash".parse().unwrap();
assert_eq!(from_str, from_string);
assert_eq!(from_str, parsed);
}
#[test]
fn tool_name_as_hashmap_key() {
use std::collections::HashMap;
let mut map: HashMap<ToolName, u32> = HashMap::new();
map.insert(ToolName::new("shell"), 1);
assert_eq!(map.get("shell"), Some(&1));
}
#[test]
fn tool_name_display() {
let name = ToolName::new("my_tool");
assert_eq!(format!("{name}"), "my_tool");
}
#[test]
fn tool_name_serde_transparent() {
let name = ToolName::new("shell");
let json = serde_json::to_string(&name).unwrap();
assert_eq!(json, r#""shell""#);
let back: ToolName = serde_json::from_str(&json).unwrap();
assert_eq!(back, name);
}
#[test]
fn session_id_new_roundtrip() {
let id = SessionId::new("test-session");
assert_eq!(id.as_str(), "test-session");
assert_eq!(id.to_string(), "test-session");
}
#[test]
fn session_id_generate_is_uuid() {
let id = SessionId::generate();
assert_eq!(id.as_str().len(), 36);
assert!(uuid::Uuid::parse_str(id.as_str()).is_ok());
}
#[test]
fn session_id_default_is_generated() {
let id = SessionId::default();
assert!(!id.as_str().is_empty());
assert_eq!(id.as_str().len(), 36);
}
#[test]
fn session_id_from_uuid() {
let u = uuid::Uuid::new_v4();
let id = SessionId::from(u);
assert_eq!(id.as_str(), u.to_string());
}
#[test]
fn session_id_deref_slicing() {
let id = SessionId::new("abcdefgh");
assert_eq!(&id[..4], "abcd");
}
#[test]
fn session_id_serde_transparent() {
let id = SessionId::new("sess-abc");
let json = serde_json::to_string(&id).unwrap();
assert_eq!(json, r#""sess-abc""#);
let back: SessionId = serde_json::from_str(&json).unwrap();
assert_eq!(back, id);
}
#[test]
fn session_id_from_str_parses() {
let id: SessionId = "my-session".parse().unwrap();
assert_eq!(id.as_str(), "my-session");
}
}