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