rpn_cli/
context.rs

1use crate::value::Value;
2use num::ToPrimitive;
3use std::mem::swap;
4
5#[derive(Clone, PartialEq)]
6pub enum Format {
7    Base10,
8    Base16(usize),
9}
10
11#[derive(Clone)]
12pub struct Context {
13    pub format: Format,
14    pub sep: bool,
15    pub dp: Option<u8>,
16}
17
18impl Context {
19    pub fn new() -> Self {
20        Self {
21            format: Format::Base10,
22            sep: false,
23            dp: None,
24        }
25    }
26
27    pub fn with_format(mut self, format: Format) -> Self {
28        self.format = format;
29        self
30    }
31
32    pub fn set_dec(&mut self) {
33        self.format = Format::Base10;
34    }
35
36    pub fn set_hex(&mut self) {
37        self.format = match self.format {
38            Format::Base10 => Format::Base16(0),
39            Format::Base16(count) => Format::Base16(count),
40        }
41    }
42
43    pub fn with_sep(mut self, sep: bool) -> Self {
44        self.sep = sep;
45        self
46    }
47
48    pub fn swap_sep(&mut self, mut sep: bool) -> bool {
49        swap(&mut self.sep, &mut sep);
50        sep
51    }
52
53    pub fn set_sep(&mut self) {
54        self.sep = true;
55    }
56
57    pub fn no_sep(&mut self) {
58        self.sep = false;
59    }
60
61    pub fn with_dp(mut self, dp: Option<u8>) -> Self {
62        self.dp = dp;
63        self
64    }
65
66    pub fn set_dp(&mut self, dp: Value) {
67        self.dp = dp.number.and_then(|x| x.to_u8());
68    }
69
70    pub fn set_dp_u8(&mut self, dp: u8) {
71        self.dp = Some(dp);
72    }
73
74    pub fn no_dp(&mut self) {
75        self.dp = None;
76    }
77}