Skip to main content

synwire_core/output_parsers/
traits.rs

1//! Core output parser trait.
2
3use crate::error::SynwireError;
4
5/// Trait for parsing model output into structured types.
6///
7/// Output parsers transform raw text from language models into structured data.
8/// Implement this trait to create custom parsers for specific output formats.
9///
10/// # Examples
11///
12/// ```
13/// use synwire_core::output_parsers::OutputParser;
14/// use synwire_core::error::SynwireError;
15///
16/// struct UpperCaseParser;
17///
18/// impl OutputParser for UpperCaseParser {
19///     type Output = String;
20///
21///     fn parse(&self, text: &str) -> Result<String, SynwireError> {
22///         Ok(text.to_uppercase())
23///     }
24/// }
25/// ```
26pub trait OutputParser: Send + Sync {
27    /// The output type this parser produces.
28    type Output;
29
30    /// Parse raw text output from a model.
31    ///
32    /// # Errors
33    ///
34    /// Returns `SynwireError` if the text cannot be parsed into the expected type.
35    fn parse(&self, text: &str) -> Result<Self::Output, SynwireError>;
36
37    /// Get format instructions to include in prompts.
38    ///
39    /// These instructions guide the model to produce output in the format
40    /// expected by this parser. Returns an empty string by default.
41    fn get_format_instructions(&self) -> String {
42        String::new()
43    }
44}