alith_client/primitives/
sentences.rs

1use super::PrimitiveTrait;
2use crate::components::grammar::{Grammar, SentencesGrammar};
3use anyhow::Result;
4
5#[derive(Debug, Clone)]
6pub struct SentencesPrimitive {
7    pub min_count: u8,
8    pub max_count: u8,
9    pub capitalize_first: bool,
10    pub concatenator: String,
11    pub disallowed_chars: Vec<char>,
12}
13
14impl Default for SentencesPrimitive {
15    fn default() -> Self {
16        SentencesPrimitive {
17            min_count: 1,
18            max_count: 1,
19            capitalize_first: true,
20            concatenator: " ".to_string(),
21            disallowed_chars: vec![],
22        }
23    }
24}
25
26impl SentencesPrimitive {
27    /// Set the lower bound of the integer range. Default is 0.
28    pub fn min_count(&mut self, min_count: u8) -> &mut Self {
29        if self.min_count != min_count {
30            self.min_count = min_count;
31        }
32        self
33    }
34
35    /// Set the upper bound of the integer range. Default is 9.
36    pub fn max_count(&mut self, max_count: u8) -> &mut Self {
37        if self.max_count != max_count {
38            self.max_count = max_count;
39        }
40        self
41    }
42
43    pub fn capitalize_first(&mut self, capitalize_first: bool) -> &mut Self {
44        self.capitalize_first = capitalize_first;
45        self
46    }
47
48    pub fn concatenator(&mut self, concatenator: &str) -> &mut Self {
49        self.concatenator = concatenator.to_string();
50        self
51    }
52
53    pub fn disallowed_char(&mut self, disallowed_char: char) -> &mut Self {
54        self.disallowed_chars.push(disallowed_char);
55        self
56    }
57
58    pub fn disallowed_chars(&mut self, disallowed_chars: Vec<char>) -> &mut Self {
59        self.disallowed_chars.extend(disallowed_chars);
60        self
61    }
62
63    fn grammar_inner(&self) -> SentencesGrammar {
64        Grammar::sentences()
65            .min_count(self.min_count)
66            .max_count(self.max_count)
67            .capitalize_first(self.capitalize_first)
68            .concatenator(&self.concatenator)
69            .disallowed_chars(self.disallowed_chars.clone())
70    }
71}
72
73impl PrimitiveTrait for SentencesPrimitive {
74    type PrimitiveResult = String;
75
76    fn clear_primitive(&mut self) {}
77
78    fn type_description(&self, result_can_be_none: bool) -> &str {
79        if result_can_be_none {
80            "sentences or 'None.'"
81        } else {
82            "sentences"
83        }
84    }
85
86    fn solution_description(&self, result_can_be_none: bool) -> String {
87        if result_can_be_none {
88            format!(
89                "between {}-{} sentences, or, possibly, 'None.'",
90                self.min_count, self.max_count
91            )
92        } else {
93            format!("between {}-{} sentences", self.min_count, self.max_count)
94        }
95    }
96
97    fn stop_word_result_is_none(&self, result_can_be_none: bool) -> Option<String> {
98        if result_can_be_none {
99            Some("None.".to_string())
100        } else {
101            None
102        }
103    }
104
105    fn grammar(&self) -> Grammar {
106        self.grammar_inner().wrap()
107    }
108
109    fn parse_to_primitive(&self, content: &str) -> Result<Self::PrimitiveResult> {
110        let parsed: Self::PrimitiveResult = self.grammar_inner().grammar_parse(content)?;
111        Ok(parsed)
112    }
113}