use super::error::ParseError;
use serde::{Deserialize, Serialize};
pub trait ResponseParser<T> {
fn parse(&self, content: &str) -> Result<T, ParseError>;
fn extract_content(&self, text: &str) -> String;
fn fallback_parse(&self, content: &str, error: &ParseError) -> Result<T, ParseError>;
}
pub trait ContentExtractor {
fn extract_tagged(&self, text: &str, tag: &str) -> Option<String>;
fn extract_json_like(&self, text: &str) -> Option<String>;
fn extract_pattern(&self, text: &str, pattern: &str) -> Option<String>;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ExtractionStrategy {
TaggedContent(String),
JsonBrackets,
FirstJsonObject,
KeywordSearch(Vec<String>),
RegexPattern(String),
OriginalText,
}
#[derive(Debug, Clone)]
pub struct ParsingConfig {
pub primary_tag: String,
pub extraction_strategies: Vec<ExtractionStrategy>,
pub debug_mode: bool,
pub max_content_length: Option<usize>,
}
impl Default for ParsingConfig {
fn default() -> Self {
Self {
primary_tag: "answer".to_string(),
extraction_strategies: vec![
ExtractionStrategy::TaggedContent("answer".to_string()),
ExtractionStrategy::JsonBrackets,
ExtractionStrategy::OriginalText,
],
debug_mode: false,
max_content_length: Some(50_000), }
}
}
impl ParsingConfig {
pub fn with_tag(tag: &str) -> Self {
Self {
primary_tag: tag.to_string(),
extraction_strategies: vec![ExtractionStrategy::TaggedContent(tag.to_string())],
..Default::default()
}
}
pub fn add_strategy(mut self, strategy: ExtractionStrategy) -> Self {
self.extraction_strategies.push(strategy);
self
}
pub fn with_debug(mut self) -> Self {
self.debug_mode = true;
self
}
}