use async_trait::async_trait;
use futures_util::Stream;
use std::pin::Pin;
use crate::core::runnables::{Runnable, RunnableConfig};
use super::base::{BaseOutputParser, OutputParserError, OutputParserResult};
pub struct StrOutputParser;
impl StrOutputParser {
pub fn new() -> Self {
Self
}
}
impl Default for StrOutputParser {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl BaseOutputParser<String> for StrOutputParser {
async fn parse(&self, text: &str) -> OutputParserResult<String> {
Ok(text.to_string())
}
}
#[async_trait]
impl Runnable<String, String> for StrOutputParser {
type Error = OutputParserError;
async fn invoke(&self, input: String, _config: Option<RunnableConfig>) -> Result<String, Self::Error> {
self.parse(&input).await
}
async fn stream(
&self,
input: String,
_config: Option<RunnableConfig>,
) -> Result<Pin<Box<dyn Stream<Item = Result<String, Self::Error>> + Send>>, Self::Error> {
let result = self.parse(&input).await?;
let stream = futures_util::stream::once(async move { Ok(result) });
Ok(Box::pin(stream))
}
}