use std::borrow::Cow;
use std::convert::Infallible;
use std::fmt;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
macro_rules! string_id {
($(#[$meta:meta])* $name:ident) => {
$(#[$meta])*
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct $name(String);
impl $name {
#[inline]
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
#[inline]
pub fn as_str(&self) -> &str {
&self.0
}
#[inline]
pub fn into_inner(self) -> String {
self.0
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<String> for $name {
#[inline]
fn from(value: String) -> Self {
Self(value)
}
}
impl From<&str> for $name {
#[inline]
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl From<Cow<'_, str>> for $name {
#[inline]
fn from(value: Cow<'_, str>) -> Self {
Self(value.into_owned())
}
}
impl From<$name> for String {
#[inline]
fn from(value: $name) -> Self {
value.0
}
}
impl AsRef<str> for $name {
#[inline]
fn as_ref(&self) -> &str {
&self.0
}
}
impl std::ops::Deref for $name {
type Target = str;
#[inline]
fn deref(&self) -> &str {
&self.0
}
}
impl FromStr for $name {
type Err = Infallible;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.to_owned()))
}
}
impl PartialEq<str> for $name {
#[inline]
fn eq(&self, other: &str) -> bool {
self.0 == other
}
}
impl PartialEq<&str> for $name {
#[inline]
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}
impl PartialEq<$name> for str {
#[inline]
fn eq(&self, other: &$name) -> bool {
self == other.0
}
}
};
}
string_id! {
ThreadId
}
string_id! {
RunId
}
string_id! {
MessageId
}
string_id! {
ToolCallId
}
string_id! {
AgentId
}
string_id! {
StepName
}