elm_parser/counter/
counter_instance.rs1use super::counter_types::{CounterType, CounterValueType};
2use numerals::roman::Roman;
3
4#[derive(Debug, Clone)]
5pub struct CounterInstance {
6 pub counter_type: CounterType,
7 pub name: String,
8 pub current_value: CounterValueType,
9 pub scope: usize,
10}
11
12impl CounterInstance {
13 pub fn new(name: &str, _type: &str, scope: usize, default_value: Option<&str>) -> Self {
14 Self {
15 name: name.to_string(),
16 counter_type: CounterType::from_str(_type).unwrap(),
17 current_value: CounterValueType::from_str(_type, default_value),
18 scope,
19 }
20 }
21
22 pub fn increment(&mut self) {
23 match self.counter_type {
24 CounterType::ARABIC => {
25 if let CounterValueType::ARABIC(i) = self.current_value {
26 self.current_value = CounterValueType::ARABIC(i + 1)
27 }
28 }
29 CounterType::ROMAN => {
30 if let CounterValueType::ROMAN(roman) = &self.current_value {
31 self.current_value = if roman == "0" {
32 CounterValueType::ROMAN("i".to_string())
33 } else {
34 let mut num: i16 =
35 Roman::parse(&roman).expect("unvalid roman chars").value();
36 num += 1;
37 let incremented_roman = format!("{:x}", Roman::from(num));
38 CounterValueType::ROMAN(incremented_roman)
39 }
40 }
41 }
42 CounterType::ALPHABITICAL => {}
43 }
44 }
45
46 pub fn decrement(&mut self) {
47 match self.counter_type {
48 CounterType::ARABIC => {
49 if let CounterValueType::ARABIC(i) = self.current_value {
50 self.current_value = CounterValueType::ARABIC(i - 1)
51 }
52 }
53 CounterType::ROMAN => {
54 if let CounterValueType::ROMAN(roman) = &self.current_value {
55 self.current_value = if roman == "i" {
56 CounterValueType::ROMAN("0".to_string())
57 } else {
58 if let Some(paresed) = Roman::parse(&roman) {
59 let mut num: i16 = paresed.value();
60 num -= 1;
61 let incremented_roman = format!("{:x}", Roman::from(num));
62 CounterValueType::ROMAN(incremented_roman)
63 } else {
64 CounterValueType::ROMAN("-".to_string())
65 }
66 }
67 }
68 }
69 CounterType::ALPHABITICAL => {}
70 }
71 }
72}