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
use num::ToPrimitive;

use crate::value::Value;

#[derive(PartialEq)]
pub enum Format {
    Base10,
    Base16(usize),
}

pub struct Context {
    pub separator: bool,
    pub precision: Option<u8>,
    pub format: Format,
}

impl Context {
    pub fn new() -> Context {
        Self {
            separator: false,
            precision: None,
            format: Format::Base10,
        }
    }

    pub fn set_separator(&mut self) {
        self.separator = true;
    }

    pub fn no_separator(&mut self) {
        self.separator = false;
    }

    pub fn set_precision(&mut self, precision: Value) {
        self.precision = precision.number.and_then(|x| x.to_u8());
    }

    pub fn no_precision(&mut self) {
        self.precision = None;
    }

    pub fn set_decimal(&mut self) {
        self.format = Format::Base10;
    }

    pub fn set_hexadecimal(&mut self) {
        self.format = Format::Base16(0);
    }
}

#[cfg(test)]
mod tests {
    use crate::context::{Context, Format};

    impl Context {
        pub fn with_separator(mut self, separator: bool) -> Context {
            self.separator = separator;
            return self;
        }

        pub fn with_precision(mut self, precision: Option<u8>) -> Context {
            self.precision = precision;
            return self;
        }

        pub fn with_format(mut self, format: Format) -> Context {
            self.format = format;
            return self;
        }
    }
}