Skip to main content

cognis_core/output_parsers/
string.rs

1//! Identity parser — passes the LLM output through unchanged.
2
3use async_trait::async_trait;
4
5use crate::output_parsers::OutputParser;
6use crate::runnable::{Runnable, RunnableConfig};
7use crate::Result;
8
9/// Identity parser: returns the input string unchanged.
10#[derive(Debug, Default, Clone, Copy)]
11pub struct StringParser;
12
13impl StringParser {
14    /// Construct a `StringParser`.
15    pub fn new() -> Self {
16        Self
17    }
18}
19
20impl OutputParser<String> for StringParser {
21    fn parse(&self, text: &str) -> Result<String> {
22        Ok(text.to_string())
23    }
24}
25
26#[async_trait]
27impl Runnable<String, String> for StringParser {
28    async fn invoke(&self, input: String, _: RunnableConfig) -> Result<String> {
29        Ok(input)
30    }
31    fn name(&self) -> &str {
32        "StringParser"
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[tokio::test]
41    async fn passes_through() {
42        let p = StringParser::new();
43        let out = p
44            .invoke("hello".into(), RunnableConfig::default())
45            .await
46            .unwrap();
47        assert_eq!(out, "hello");
48        assert_eq!(p.parse("x").unwrap(), "x");
49    }
50}