use crate::output_parser::ParseError;
use serde_json::Value;
use std::sync::Arc;
pub type CustomParseFn = Arc<dyn Fn(&str) -> Result<Value, ParseError> + Send + Sync>;
#[derive(Clone)]
pub enum OutputStrategy {
Lossy,
Json,
StringList,
XmlTag(String),
Choice(Vec<String>),
Number,
NumberInRange(f64, f64),
Text,
Custom(CustomParseFn),
}
impl Default for OutputStrategy {
#[inline]
fn default() -> Self {
Self::Lossy
}
}
impl std::fmt::Debug for OutputStrategy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
OutputStrategy::Lossy => write!(f, "Lossy"),
OutputStrategy::Json => write!(f, "Json"),
OutputStrategy::StringList => write!(f, "StringList"),
OutputStrategy::XmlTag(tag) => write!(f, "XmlTag({:?})", tag),
OutputStrategy::Choice(choices) => write!(f, "Choice({:?})", choices),
OutputStrategy::Number => write!(f, "Number"),
OutputStrategy::NumberInRange(min, max) => {
write!(f, "NumberInRange({}, {})", min, max)
}
OutputStrategy::Text => write!(f, "Text"),
OutputStrategy::Custom(_) => write!(f, "Custom(...)"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_is_lossy() {
let strategy = OutputStrategy::default();
assert!(matches!(strategy, OutputStrategy::Lossy));
}
#[test]
fn test_debug_output() {
assert_eq!(format!("{:?}", OutputStrategy::Json), "Json");
assert_eq!(
format!("{:?}", OutputStrategy::Choice(vec!["a".into(), "b".into()])),
"Choice([\"a\", \"b\"])"
);
}
}