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;
}
}
}