Skip to main content

synaptic_parsers/
boolean_parser.rs

1use async_trait::async_trait;
2use synaptic_core::{RunnableConfig, SynapticError};
3use synaptic_runnables::Runnable;
4
5use crate::FormatInstructions;
6
7/// Parses yes/no, true/false, y/n style responses into a boolean.
8pub struct BooleanOutputParser;
9
10impl FormatInstructions for BooleanOutputParser {
11    fn get_format_instructions(&self) -> String {
12        "Your response should be a boolean value: true/false, yes/no, y/n, or 1/0.".to_string()
13    }
14}
15
16#[async_trait]
17impl Runnable<String, bool> for BooleanOutputParser {
18    async fn invoke(&self, input: String, _config: &RunnableConfig) -> Result<bool, SynapticError> {
19        let normalized = input.trim().to_lowercase();
20
21        match normalized.as_str() {
22            "true" | "yes" | "y" | "1" => Ok(true),
23            "false" | "no" | "n" | "0" => Ok(false),
24            _ => Err(SynapticError::Parsing(format!(
25                "cannot parse '{normalized}' as boolean; expected one of: true, false, yes, no, y, n, 1, 0"
26            ))),
27        }
28    }
29}