1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use super::counter_types::{CounterType, CounterValueType};
use numerals::roman::Roman;

#[derive(Debug, Clone)]
pub struct CounterInstance {
    pub counter_type: CounterType,
    pub name: String,
    pub current_value: CounterValueType,
    pub scope: usize,
}

impl CounterInstance {
    pub fn new(name: &str, _type: &str, scope: usize, default_value: Option<&str>) -> Self {
        Self {
            name: name.to_string(),
            counter_type: CounterType::from_str(_type).unwrap(),
            current_value: CounterValueType::from_str(_type, default_value),
            scope,
        }
    }

    pub fn increment(&mut self) {
        match self.counter_type {
            CounterType::ARABIC => {
                if let CounterValueType::ARABIC(i) = self.current_value {
                    self.current_value = CounterValueType::ARABIC(i + 1)
                }
            }
            CounterType::ROMAN => {
                if let CounterValueType::ROMAN(roman) = &self.current_value {
                    self.current_value = if roman == "0" {
                        CounterValueType::ROMAN("i".to_string())
                    } else {
                        let mut num: i16 =
                            Roman::parse(&roman).expect("unvalid roman chars").value();
                        num += 1;
                        let incremented_roman = format!("{:x}", Roman::from(num));
                        CounterValueType::ROMAN(incremented_roman)
                    }
                }
            }
            CounterType::ALPHABITICAL => {}
        }
    }

    pub fn decrement(&mut self) {
        match self.counter_type {
            CounterType::ARABIC => {
                if let CounterValueType::ARABIC(i) = self.current_value {
                    self.current_value = CounterValueType::ARABIC(i - 1)
                }
            }
            CounterType::ROMAN => {
                if let CounterValueType::ROMAN(roman) = &self.current_value {
                    self.current_value = if roman == "i" {
                        CounterValueType::ROMAN("0".to_string())
                    } else {
                        if let Some(paresed) = Roman::parse(&roman) {
                            let mut num: i16 = paresed.value();
                            num -= 1;
                            let incremented_roman = format!("{:x}", Roman::from(num));
                            CounterValueType::ROMAN(incremented_roman)
                        } else {
                            CounterValueType::ROMAN("-".to_string())
                        }
                    }
                }
            }
            CounterType::ALPHABITICAL => {}
        }
    }
}