1use crate::calc::value::ValueRef;
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_hex(mut self, hex: bool) -> Self {
28 self.format = if hex { Format::Base16(0) } else { Format::Base10 };
29 self
30 }
31
32 pub fn with_format(mut self, format: Format) -> Self {
33 self.format = format;
34 self
35 }
36
37 pub fn set_dec(&mut self) {
38 self.format = Format::Base10;
39 }
40
41 pub fn set_hex(&mut self) {
42 self.format = match self.format {
43 Format::Base10 => Format::Base16(0),
44 Format::Base16(count) => Format::Base16(count),
45 }
46 }
47
48 pub fn with_sep(mut self, sep: bool) -> Self {
49 self.sep = sep;
50 self
51 }
52
53 pub fn swap_sep(&mut self, mut sep: bool) -> bool {
54 swap(&mut self.sep, &mut sep);
55 sep
56 }
57
58 pub fn set_sep(&mut self) {
59 self.sep = true;
60 }
61
62 pub fn no_sep(&mut self) {
63 self.sep = false;
64 }
65
66 pub fn with_dp(mut self, dp: Option<u8>) -> Self {
67 self.dp = dp;
68 self
69 }
70
71 pub fn set_dp(&mut self, dp: ValueRef) {
72 if let Some(number) = &dp.borrow().number {
73 self.dp = number.to_u8();
74 } else {
75 self.dp = None;
76 }
77 }
78
79 pub fn set_dp_u8(&mut self, dp: u8) {
80 self.dp = Some(dp);
81 }
82
83 pub fn no_dp(&mut self) {
84 self.dp = None;
85 }
86}