use std::borrow::Cow;
use std::fmt;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub enum WireFormat {
#[serde(rename = "openai_chat")]
OpenAiChat,
#[serde(rename = "anthropic_messages")]
AnthropicMessages,
#[serde(rename = "openai_responses")]
OpenAiResponses,
}
impl WireFormat {
pub const fn as_str(self) -> &'static str {
match self {
Self::OpenAiChat => "openai_chat",
Self::AnthropicMessages => "anthropic_messages",
Self::OpenAiResponses => "openai_responses",
}
}
}
impl fmt::Display for WireFormat {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(transparent)]
pub struct FormatId(String);
impl FormatId {
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
pub fn known(format: WireFormat) -> Self {
Self(format.as_str().to_string())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for FormatId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
impl From<WireFormat> for FormatId {
fn from(format: WireFormat) -> Self {
Self::known(format)
}
}
impl From<&WireFormat> for FormatId {
fn from(format: &WireFormat) -> Self {
Self::known(*format)
}
}
impl From<&str> for FormatId {
fn from(id: &str) -> Self {
Self::new(id)
}
}
impl From<String> for FormatId {
fn from(id: String) -> Self {
Self::new(id)
}
}
impl From<&String> for FormatId {
fn from(id: &String) -> Self {
Self::new(id.clone())
}
}
impl From<Cow<'_, str>> for FormatId {
fn from(id: Cow<'_, str>) -> Self {
Self::new(id.into_owned())
}
}