Skip to main content

synwire_core/output_parsers/
string.rs

1//! String output parser that returns raw text unchanged.
2
3use crate::error::SynwireError;
4use crate::output_parsers::OutputParser;
5
6/// Parser that returns raw text unchanged.
7///
8/// This is the simplest output parser, useful when no structured parsing
9/// is needed and the model's raw text output is sufficient.
10///
11/// # Examples
12///
13/// ```
14/// use synwire_core::output_parsers::{OutputParser, StrOutputParser};
15///
16/// let parser = StrOutputParser;
17/// let result = parser.parse("Hello, world!").unwrap();
18/// assert_eq!(result, "Hello, world!");
19/// ```
20pub struct StrOutputParser;
21
22impl OutputParser for StrOutputParser {
23    type Output = String;
24
25    fn parse(&self, text: &str) -> Result<String, SynwireError> {
26        Ok(text.to_string())
27    }
28}
29
30#[cfg(test)]
31#[allow(clippy::unwrap_used)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn test_str_parser_returns_unchanged() {
37        let parser = StrOutputParser;
38        let input = "Hello, world!";
39        let result = parser.parse(input).unwrap();
40        assert_eq!(result, input);
41    }
42
43    #[test]
44    fn test_str_parser_empty_string() {
45        let parser = StrOutputParser;
46        let result = parser.parse("").unwrap();
47        assert_eq!(result, "");
48    }
49
50    #[test]
51    fn test_str_parser_preserves_whitespace() {
52        let parser = StrOutputParser;
53        let input = "  hello\n  world  ";
54        let result = parser.parse(input).unwrap();
55        assert_eq!(result, input);
56    }
57
58    #[test]
59    fn test_str_parser_format_instructions_empty() {
60        let parser = StrOutputParser;
61        assert!(parser.get_format_instructions().is_empty());
62    }
63}