use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FormatConversionError {
pub from: String,
pub to: String,
pub reason: String,
}
impl fmt::Display for FormatConversionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Cannot convert {} to {}: {}",
self.from, self.to, self.reason
)
}
}
impl std::error::Error for FormatConversionError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum WireFormat {
Soch,
Json,
Columnar,
}
impl Default for WireFormat {
fn default() -> Self {
Self::Soch
}
}
impl fmt::Display for WireFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Soch => write!(f, "toon"),
Self::Json => write!(f, "json"),
Self::Columnar => write!(f, "columnar"),
}
}
}
impl std::str::FromStr for WireFormat {
type Err = FormatConversionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"toon" => Ok(Self::Soch),
"json" => Ok(Self::Json),
"columnar" | "column" => Ok(Self::Columnar),
_ => Err(FormatConversionError {
from: s.to_string(),
to: "WireFormat".to_string(),
reason: format!("Unknown format '{}'. Valid: toon, json, columnar", s),
}),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ContextFormat {
Soch,
Json,
Markdown,
}
impl Default for ContextFormat {
fn default() -> Self {
Self::Soch
}
}
impl fmt::Display for ContextFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Soch => write!(f, "toon"),
Self::Json => write!(f, "json"),
Self::Markdown => write!(f, "markdown"),
}
}
}
impl std::str::FromStr for ContextFormat {
type Err = FormatConversionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"toon" => Ok(Self::Soch),
"json" => Ok(Self::Json),
"markdown" | "md" => Ok(Self::Markdown),
"text" | "plain" => Err(FormatConversionError {
from: s.to_string(),
to: "ContextFormat".to_string(),
reason: "Plain text format is not supported. Use 'markdown' for \
human-readable output or 'toon' for LLM-optimized output.".to_string(),
}),
_ => Err(FormatConversionError {
from: s.to_string(),
to: "ContextFormat".to_string(),
reason: format!("Unknown format '{}'. Valid: toon, json, markdown", s),
}),
}
}
}
impl TryFrom<WireFormat> for ContextFormat {
type Error = FormatConversionError;
fn try_from(wire: WireFormat) -> Result<Self, Self::Error> {
match wire {
WireFormat::Soch => Ok(ContextFormat::Soch),
WireFormat::Json => Ok(ContextFormat::Json),
WireFormat::Columnar => Err(FormatConversionError {
from: "Columnar".to_string(),
to: "ContextFormat".to_string(),
reason: "Columnar format is for analytics, not LLM context. \
Convert to Soch or Json first.".to_string(),
}),
}
}
}
impl From<ContextFormat> for WireFormat {
fn from(ctx: ContextFormat) -> Self {
match ctx {
ContextFormat::Soch => WireFormat::Soch,
ContextFormat::Json => WireFormat::Json,
ContextFormat::Markdown => WireFormat::Soch, }
}
}
#[derive(Debug, Clone)]
pub struct FormatCapabilities {
pub structured: bool,
pub nested: bool,
pub binary: bool,
pub streaming: bool,
pub token_efficiency: f32,
}
impl WireFormat {
pub fn capabilities(&self) -> FormatCapabilities {
match self {
Self::Soch => FormatCapabilities {
structured: true,
nested: true,
binary: true,
streaming: true,
token_efficiency: 0.4, },
Self::Json => FormatCapabilities {
structured: true,
nested: true,
binary: false, streaming: true,
token_efficiency: 1.0,
},
Self::Columnar => FormatCapabilities {
structured: true,
nested: false,
binary: true,
streaming: true,
token_efficiency: 0.3, },
}
}
}
impl ContextFormat {
pub fn capabilities(&self) -> FormatCapabilities {
match self {
Self::Soch => FormatCapabilities {
structured: true,
nested: true,
binary: true,
streaming: true,
token_efficiency: 0.4,
},
Self::Json => FormatCapabilities {
structured: true,
nested: true,
binary: false,
streaming: true,
token_efficiency: 1.0,
},
Self::Markdown => FormatCapabilities {
structured: true, nested: true, binary: false,
streaming: true,
token_efficiency: 0.7, },
}
}
pub fn recommended_for_llm() -> Self {
Self::Soch
}
pub fn recommended_for_human() -> Self {
Self::Markdown
}
pub fn recommended_for_api() -> Self {
Self::Json
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CanonicalFormat {
Context(ContextFormat),
Wire(WireFormat),
}
impl CanonicalFormat {
pub fn name(&self) -> &'static str {
match self {
Self::Context(ContextFormat::Soch) => "context:toon",
Self::Context(ContextFormat::Json) => "context:json",
Self::Context(ContextFormat::Markdown) => "context:markdown",
Self::Wire(WireFormat::Soch) => "wire:toon",
Self::Wire(WireFormat::Json) => "wire:json",
Self::Wire(WireFormat::Columnar) => "wire:columnar",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_wire_format_parsing() {
assert_eq!("toon".parse::<WireFormat>().unwrap(), WireFormat::Soch);
assert_eq!("json".parse::<WireFormat>().unwrap(), WireFormat::Json);
assert_eq!("columnar".parse::<WireFormat>().unwrap(), WireFormat::Columnar);
assert!("invalid".parse::<WireFormat>().is_err());
}
#[test]
fn test_context_format_parsing() {
assert_eq!("toon".parse::<ContextFormat>().unwrap(), ContextFormat::Soch);
assert_eq!("json".parse::<ContextFormat>().unwrap(), ContextFormat::Json);
assert_eq!("markdown".parse::<ContextFormat>().unwrap(), ContextFormat::Markdown);
assert!("text".parse::<ContextFormat>().is_err());
}
#[test]
fn test_wire_to_context_conversion() {
assert_eq!(
ContextFormat::try_from(WireFormat::Soch).unwrap(),
ContextFormat::Soch
);
assert_eq!(
ContextFormat::try_from(WireFormat::Json).unwrap(),
ContextFormat::Json
);
assert!(ContextFormat::try_from(WireFormat::Columnar).is_err());
}
#[test]
fn test_context_to_wire_conversion() {
assert_eq!(WireFormat::from(ContextFormat::Soch), WireFormat::Soch);
assert_eq!(WireFormat::from(ContextFormat::Json), WireFormat::Json);
assert_eq!(WireFormat::from(ContextFormat::Markdown), WireFormat::Soch);
}
#[test]
fn test_round_trip_property() {
for format in [WireFormat::Soch, WireFormat::Json, WireFormat::Columnar] {
let s = format.to_string();
let parsed: WireFormat = s.parse().unwrap();
assert_eq!(format, parsed, "Round-trip failed for WireFormat::{:?}", format);
}
for format in [ContextFormat::Soch, ContextFormat::Json, ContextFormat::Markdown] {
let s = format.to_string();
let parsed: ContextFormat = s.parse().unwrap();
assert_eq!(format, parsed, "Round-trip failed for ContextFormat::{:?}", format);
}
}
}