alith_client/primitives/
words.rs1use super::PrimitiveTrait;
2use crate::components::grammar::{Grammar, WordsGrammar};
3use anyhow::Result;
4pub struct WordsPrimitive {
5 pub min_count: u8,
6 pub max_count: u8,
7 pub word_char_length: u8,
8 pub concatenator: String,
9}
10
11impl Default for WordsPrimitive {
12 fn default() -> Self {
13 WordsPrimitive {
14 min_count: 1,
15 max_count: 3,
16 word_char_length: 12,
17 concatenator: " ".to_string(),
18 }
19 }
20}
21
22impl WordsPrimitive {
23 pub fn min_count(&mut self, min_count: u8) -> &mut Self {
25 if self.min_count != min_count {
26 self.min_count = min_count;
27 }
28 self
29 }
30
31 pub fn max_count(&mut self, max_count: u8) -> &mut Self {
33 if self.max_count != max_count {
34 self.max_count = max_count;
35 }
36 self
37 }
38
39 pub fn word_char_length(&mut self, word_char_length: u8) -> &mut Self {
40 self.word_char_length = word_char_length;
41 self
42 }
43
44 pub fn concatenator(&mut self, concatenator: &str) -> &mut Self {
45 self.concatenator = concatenator.to_string();
46 self
47 }
48
49 fn grammar_inner(&self) -> WordsGrammar {
50 Grammar::words()
51 .min_count(self.min_count)
52 .max_count(self.max_count)
53 .word_char_length(self.word_char_length)
54 .concatenator(&self.concatenator)
55 }
56}
57
58impl PrimitiveTrait for WordsPrimitive {
59 type PrimitiveResult = String;
60
61 fn clear_primitive(&mut self) {}
62
63 fn type_description(&self, result_can_be_none: bool) -> &str {
64 if result_can_be_none {
65 "words or 'None.'"
66 } else {
67 "words"
68 }
69 }
70
71 fn solution_description(&self, result_can_be_none: bool) -> String {
72 if result_can_be_none {
73 format!(
74 "between {}-{} words, or, possibly, 'None.'",
75 self.min_count, self.max_count
76 )
77 } else {
78 format!("between {}-{} words", self.min_count, self.max_count)
79 }
80 }
81
82 fn stop_word_result_is_none(&self, result_can_be_none: bool) -> Option<String> {
83 if result_can_be_none {
84 Some("None.".to_string())
85 } else {
86 None
87 }
88 }
89
90 fn grammar(&self) -> Grammar {
91 self.grammar_inner().wrap()
92 }
93
94 fn parse_to_primitive(&self, content: &str) -> Result<Self::PrimitiveResult> {
95 let parsed: Self::PrimitiveResult = self.grammar_inner().grammar_parse(content)?;
96 Ok(parsed)
97 }
98}