alith_client/primitives/
boolean.rs

1use super::PrimitiveTrait;
2use crate::components::grammar::{BooleanGrammar, Grammar};
3use crate::workflows::reason::ReasonTrait;
4use anyhow::Result;
5
6#[derive(Default)]
7pub struct BooleanPrimitive {}
8
9impl BooleanPrimitive {
10    fn grammar_inner(&self) -> BooleanGrammar {
11        Grammar::boolean()
12    }
13}
14
15impl PrimitiveTrait for BooleanPrimitive {
16    type PrimitiveResult = bool;
17
18    fn clear_primitive(&mut self) {}
19
20    fn type_description(&self, result_can_be_none: bool) -> &str {
21        if result_can_be_none {
22            "boolean or 'Neither.'"
23        } else {
24            "boolean"
25        }
26    }
27
28    fn solution_description(&self, result_can_be_none: bool) -> String {
29        if result_can_be_none {
30            "a boolean or 'neither'; If the answer is true/yes/affirmative, then it's 'true'. If the answer is false/no/negative, then it's 'false'. If it's neither, then it's 'Neither'".to_owned()
31        } else {
32            "a boolean; If the answer is true/yes/affirmative, then it's 'true'. If the answer is false/no/negative, then it's 'false'".to_owned()
33        }
34    }
35
36    fn stop_word_result_is_none(&self, result_can_be_none: bool) -> Option<String> {
37        if result_can_be_none {
38            Some("Neither.".to_string())
39        } else {
40            None
41        }
42    }
43
44    fn grammar(&self) -> Grammar {
45        self.grammar_inner().wrap()
46    }
47
48    fn parse_to_primitive(&self, content: &str) -> Result<Self::PrimitiveResult> {
49        let parsed: Self::PrimitiveResult = self.grammar_inner().grammar_parse(content)?;
50        Ok(parsed)
51    }
52}
53
54impl ReasonTrait for BooleanPrimitive {
55    fn primitive_to_result_index(&self, content: &str) -> u32 {
56        let output = self.parse_to_primitive(content).unwrap();
57        if output { 1 } else { 0 }
58    }
59
60    fn result_index_to_primitive(&self, result_index: Option<u32>) -> Result<Option<bool>> {
61        if let Some(result_index) = result_index {
62            Ok(match result_index {
63                0 => Some(false),
64                1 => Some(true),
65                _ => return Err(anyhow::format_err!("Decision: no winner")),
66            })
67        } else {
68            Ok(None)
69        }
70    }
71}