use async_trait::async_trait;
use std::fmt;
#[derive(Debug, Clone)]
pub enum OutputParserError {
ParseError(String),
JsonError(String),
TypeError(String),
Custom(String),
}
impl fmt::Display for OutputParserError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
OutputParserError::ParseError(msg) => write!(f, "Parse error: {}", msg),
OutputParserError::JsonError(msg) => write!(f, "JSON error: {}", msg),
OutputParserError::TypeError(msg) => write!(f, "Type error: {}", msg),
OutputParserError::Custom(msg) => write!(f, "{}", msg),
}
}
}
impl std::error::Error for OutputParserError {}
impl From<serde_json::Error> for OutputParserError {
fn from(e: serde_json::Error) -> Self {
OutputParserError::JsonError(e.to_string())
}
}
pub type OutputParserResult<T> = Result<T, OutputParserError>;
#[async_trait]
pub trait BaseOutputParser<Output: Send + Sync + 'static>: Send + Sync {
async fn parse(&self, text: &str) -> OutputParserResult<Output>;
async fn parse_with_retry(
&self,
text: &str,
_max_retries: usize,
) -> OutputParserResult<Output> {
self.parse(text).await
}
fn get_format_instructions(&self) -> String {
String::new()
}
}