From f80011652bf7a8a0fb7d36d1463937694624ee61 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Thu, 3 Jun 2021 22:28:51 -0400 Subject: [PATCH] Make validate part of the trait --- src/calc/entries.rs | 58 ++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/src/calc/entries.rs b/src/calc/entries.rs index b6dbbb6..4a03d22 100644 --- a/src/calc/entries.rs +++ b/src/calc/entries.rs @@ -122,6 +122,13 @@ impl CalculatorEntry for Entry { Self::Matrix(matrix) => matrix.is_valid(), } } + fn validate(self) -> CalculatorResult { + match self { + Self::Number(number) => number.validate(), + Self::Vector(vector) => vector.validate(), + Self::Matrix(matrix) => matrix.validate(), + } + } fn negate(&self) -> CalculatorResult { match self { Self::Number(number) => number.negate(), @@ -274,6 +281,14 @@ impl CalculatorEntry for Matrix { // The dimensions are not zero && self.dimensions.rows > 0 && self.dimensions.columns > 0 } + fn validate(self) -> CalculatorResult { + if self.is_valid() { + Ok(Entry::Matrix(self)) + } else { + Err(CalculatorError::ArithmeticError) + } + } + fn format_entry(&self, display_mode: &CalculatorDisplayMode) -> String { format!( "[ {} ]", @@ -459,15 +474,6 @@ impl Matrix { .validate() } - // TODO: Should validate be put in the trait? - pub fn validate(self) -> CalculatorResult { - if self.is_valid() { - Ok(Entry::Matrix(self)) - } else { - Err(CalculatorError::ArithmeticError) - } - } - fn iterated_unary( &self, op: impl Fn(&Vector) -> CalculatorResult, @@ -541,6 +547,14 @@ impl CalculatorEntry for Vector { fn is_valid(&self) -> bool { self.values.iter().all(|number| number.is_valid()) } + fn validate(self) -> CalculatorResult { + if self.is_valid() { + Ok(Entry::Vector(self)) + } else { + Err(CalculatorError::ArithmeticError) + } + } + fn format_entry(&self, display_mode: &CalculatorDisplayMode) -> String { format!( "[{}]", @@ -691,14 +705,6 @@ impl Vector { .validate() } - fn validate(self) -> CalculatorResult { - if self.is_valid() { - Ok(Entry::Vector(self)) - } else { - Err(CalculatorError::ArithmeticError) - } - } - fn iterated_unary( &self, op: impl Fn(&Number) -> CalculatorResult, @@ -783,6 +789,14 @@ impl CalculatorEntry for Number { fn is_valid(&self) -> bool { !self.value.is_nan() && !self.value.is_infinite() } + fn validate(self) -> CalculatorResult { + if self.is_valid() { + Ok(Entry::Number(self)) + } else { + Err(CalculatorError::ArithmeticError) + } + } + fn negate(&self) -> CalculatorResult { Ok(Entry::Number(Self { value: -self.value })) } @@ -941,14 +955,6 @@ impl CalculatorEntry for Number { impl Number { pub const ZERO: Self = Self { value: 0.0_f64 }; - fn validate(&self) -> CalculatorResult { - if self.is_valid() { - Ok(Entry::Number(*self)) - } else { - Err(CalculatorError::ArithmeticError) - } - } - fn iterated_binary_vec( self, vector: &Vector, @@ -997,6 +1003,8 @@ where { // Misc fn is_valid(&self) -> bool; + /// Turns self into a valid entry + fn validate(self) -> CalculatorResult; fn format_entry(&self, display_mode: &CalculatorDisplayMode) -> String; fn to_editable_string(&self) -> CalculatorResult; // Mathematical operations