use alloc::string::String;
use core::{fmt, str::FromStr};
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum OutputFormat<T = String> {
#[default]
Jsonl,
Url,
Other(T),
}
impl<T: fmt::Display> fmt::Display for OutputFormat<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Jsonl => f.write_str("jsonl"),
Self::Url => f.write_str("url"),
Self::Other(value) => value.fmt(f),
}
}
}
impl<T: FromStr> FromStr for OutputFormat<T> {
type Err = T::Err;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"jsonl" => Ok(Self::Jsonl),
"url" => Ok(Self::Url),
_ => value.parse().map(Self::Other),
}
}
}
impl<T: AsRef<str>> OutputFormat<T> {
pub fn as_str(&self) -> &str {
match self {
Self::Jsonl => "jsonl",
Self::Url => "url",
Self::Other(value) => value.as_ref(),
}
}
}
impl<T: AsRef<str>> AsRef<str> for OutputFormat<T> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl From<String> for OutputFormat<String> {
fn from(value: String) -> Self {
match value.as_str() {
"jsonl" => Self::Jsonl,
"url" => Self::Url,
_ => Self::Other(value),
}
}
}
impl From<&str> for OutputFormat<String> {
fn from(value: &str) -> Self {
match value {
"jsonl" => Self::Jsonl,
"url" => Self::Url,
_ => Self::Other(value.into()),
}
}
}