use num::ToPrimitive;
use crate::value::Value;
#[derive(Clone, PartialEq)]
pub enum Format {
Base10,
Base16(usize),
}
#[derive(Clone)]
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 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;
}
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 = match self.format {
Format::Base10 => Format::Base16(0),
Format::Base16(count) => Format::Base16(count),
}
}
}