Make validate part of the trait
This commit is contained in:
parent
677444b7ff
commit
f80011652b
@ -122,6 +122,13 @@ impl CalculatorEntry for Entry {
|
||||
Self::Matrix(matrix) => matrix.is_valid(),
|
||||
}
|
||||
}
|
||||
fn validate(self) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Self::Number(number) => number.validate(),
|
||||
Self::Vector(vector) => vector.validate(),
|
||||
Self::Matrix(matrix) => matrix.validate(),
|
||||
}
|
||||
}
|
||||
fn negate(&self) -> CalculatorResult<Self> {
|
||||
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<Entry> {
|
||||
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<Entry> {
|
||||
if self.is_valid() {
|
||||
Ok(Entry::Matrix(self))
|
||||
} else {
|
||||
Err(CalculatorError::ArithmeticError)
|
||||
}
|
||||
}
|
||||
|
||||
fn iterated_unary(
|
||||
&self,
|
||||
op: impl Fn(&Vector) -> CalculatorResult<Entry>,
|
||||
@ -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<Entry> {
|
||||
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<Entry> {
|
||||
if self.is_valid() {
|
||||
Ok(Entry::Vector(self))
|
||||
} else {
|
||||
Err(CalculatorError::ArithmeticError)
|
||||
}
|
||||
}
|
||||
|
||||
fn iterated_unary(
|
||||
&self,
|
||||
op: impl Fn(&Number) -> CalculatorResult<Entry>,
|
||||
@ -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<Entry> {
|
||||
if self.is_valid() {
|
||||
Ok(Entry::Number(self))
|
||||
} else {
|
||||
Err(CalculatorError::ArithmeticError)
|
||||
}
|
||||
}
|
||||
|
||||
fn negate(&self) -> CalculatorResult<Entry> {
|
||||
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<Entry> {
|
||||
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<Entry>;
|
||||
fn format_entry(&self, display_mode: &CalculatorDisplayMode) -> String;
|
||||
fn to_editable_string(&self) -> CalculatorResult<String>;
|
||||
// Mathematical operations
|
||||
|
Loading…
Reference in New Issue
Block a user