From 4d1badc36bd497d76c6238c1b4838bfac48ab747 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Wed, 12 May 2021 23:04:54 -0400 Subject: [PATCH] Fix serialization issues --- src/calc.rs | 16 ++++++++-------- src/calc/constants.rs | 17 +++++++++-------- src/main.rs | 10 +++++----- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/calc.rs b/src/calc.rs index 6b29869..4906d89 100644 --- a/src/calc.rs +++ b/src/calc.rs @@ -262,28 +262,28 @@ impl Calculator { 'r' => self.angle_mode = CalculatorAngleMode::Radians, 'g' => self.angle_mode = CalculatorAngleMode::Grads, '_' => self.display_mode = CalculatorDisplayMode::Default, - ',' => self.display_mode = CalculatorDisplayMode::Separated(','), - ' ' => self.display_mode = CalculatorDisplayMode::Separated(' '), - 's' => self.display_mode = CalculatorDisplayMode::Scientific(DEFAULT_PRECISION), + ',' => self.display_mode = CalculatorDisplayMode::Separated{separator: ','}, + ' ' => self.display_mode = CalculatorDisplayMode::Separated{separator: ' '}, + 's' => self.display_mode = CalculatorDisplayMode::Scientific{precision: DEFAULT_PRECISION}, 'S' => { let precision = self.checked_get(0)? as usize; if precision > MAX_PRECISION { return Err(CalculatorError::PrecisionTooHigh); } - self.display_mode = CalculatorDisplayMode::Scientific(self.pop_usize()?) + self.display_mode = CalculatorDisplayMode::Scientific{precision: self.pop_usize()?} } 'e' => { - self.display_mode = CalculatorDisplayMode::Engineering(DEFAULT_PRECISION) + self.display_mode = CalculatorDisplayMode::Engineering{precision: DEFAULT_PRECISION} } 'E' => { let precision = self.checked_get(0)? as usize; if precision > MAX_PRECISION { return Err(CalculatorError::PrecisionTooHigh); } - self.display_mode = CalculatorDisplayMode::Engineering(self.pop_usize()?) + self.display_mode = CalculatorDisplayMode::Engineering{precision: self.pop_usize()?} } - 'f' => self.display_mode = CalculatorDisplayMode::Fixed(DEFAULT_PRECISION), - 'F' => self.display_mode = CalculatorDisplayMode::Fixed(self.pop_usize()?), + 'f' => self.display_mode = CalculatorDisplayMode::Fixed{precision: DEFAULT_PRECISION}, + 'F' => self.display_mode = CalculatorDisplayMode::Fixed{precision: self.pop_usize()?}, _ => return Err(CalculatorError::NoSuchSetting(c)), }; self.state = CalculatorState::Normal; diff --git a/src/calc/constants.rs b/src/calc/constants.rs index 75740ab..40da934 100644 --- a/src/calc/constants.rs +++ b/src/calc/constants.rs @@ -73,22 +73,23 @@ impl fmt::Display for CalculatorAngleMode { #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(tag = "display_mode")] +// Could also have added content="precision" pub enum CalculatorDisplayMode { Default, - Separated(char), - Scientific(usize), - Engineering(usize), - Fixed(usize), + Separated { separator: char }, + Scientific { precision: usize }, + Engineering { precision: usize }, + Fixed { precision: usize }, } impl fmt::Display for CalculatorDisplayMode { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { CalculatorDisplayMode::Default => write!(f, "DEF"), - CalculatorDisplayMode::Separated(c) => write!(f, "SEP({})", c), - CalculatorDisplayMode::Scientific(p) => write!(f, "SCI({})", p), - CalculatorDisplayMode::Engineering(p) => write!(f, "ENG({})", p), - CalculatorDisplayMode::Fixed(p) => write!(f, "FIX({})", p), + CalculatorDisplayMode::Separated{separator} => write!(f, "SEP({})", separator), + CalculatorDisplayMode::Scientific{precision} => write!(f, "SCI({})", precision), + CalculatorDisplayMode::Engineering{precision} => write!(f, "ENG({})", precision), + CalculatorDisplayMode::Fixed{precision} => write!(f, "FIX({})", precision), } } } diff --git a/src/main.rs b/src/main.rs index 4823d4b..4b8cd94 100644 --- a/src/main.rs +++ b/src/main.rs @@ -119,16 +119,16 @@ fn main() -> Result<(), Box> { let content = vec![Spans::from(Span::raw( match app.calculator.get_display_mode() { CalculatorDisplayMode::Default => format!("{:>2}: {}", i, *m), - CalculatorDisplayMode::Separated(c) => { - format!("{:>2}: {}", i, fmt_separated(*m, *c)) + CalculatorDisplayMode::Separated{separator} => { + format!("{:>2}: {}", i, fmt_separated(*m, *separator)) } - CalculatorDisplayMode::Scientific(precision) => { + CalculatorDisplayMode::Scientific{precision} => { format!("{:>2}: {}", i, fmt_scientific(*m, *precision)) } - CalculatorDisplayMode::Engineering(precision) => { + CalculatorDisplayMode::Engineering{precision} => { format!("{:>2}: {}", i, fmt_engineering(*m, *precision)) } - CalculatorDisplayMode::Fixed(precision) => { + CalculatorDisplayMode::Fixed{precision} => { format!("{:>2}: {:.precision$}", i, m, precision = precision) } },