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(),
|
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> {
|
fn negate(&self) -> CalculatorResult<Self> {
|
||||||
match self {
|
match self {
|
||||||
Self::Number(number) => number.negate(),
|
Self::Number(number) => number.negate(),
|
||||||
@ -274,6 +281,14 @@ impl CalculatorEntry for Matrix {
|
|||||||
// The dimensions are not zero
|
// The dimensions are not zero
|
||||||
&& self.dimensions.rows > 0 && self.dimensions.columns > 0
|
&& 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 {
|
fn format_entry(&self, display_mode: &CalculatorDisplayMode) -> String {
|
||||||
format!(
|
format!(
|
||||||
"[ {} ]",
|
"[ {} ]",
|
||||||
@ -459,15 +474,6 @@ impl Matrix {
|
|||||||
.validate()
|
.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(
|
fn iterated_unary(
|
||||||
&self,
|
&self,
|
||||||
op: impl Fn(&Vector) -> CalculatorResult<Entry>,
|
op: impl Fn(&Vector) -> CalculatorResult<Entry>,
|
||||||
@ -541,6 +547,14 @@ impl CalculatorEntry for Vector {
|
|||||||
fn is_valid(&self) -> bool {
|
fn is_valid(&self) -> bool {
|
||||||
self.values.iter().all(|number| number.is_valid())
|
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 {
|
fn format_entry(&self, display_mode: &CalculatorDisplayMode) -> String {
|
||||||
format!(
|
format!(
|
||||||
"[{}]",
|
"[{}]",
|
||||||
@ -691,14 +705,6 @@ impl Vector {
|
|||||||
.validate()
|
.validate()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate(self) -> CalculatorResult<Entry> {
|
|
||||||
if self.is_valid() {
|
|
||||||
Ok(Entry::Vector(self))
|
|
||||||
} else {
|
|
||||||
Err(CalculatorError::ArithmeticError)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn iterated_unary(
|
fn iterated_unary(
|
||||||
&self,
|
&self,
|
||||||
op: impl Fn(&Number) -> CalculatorResult<Entry>,
|
op: impl Fn(&Number) -> CalculatorResult<Entry>,
|
||||||
@ -783,6 +789,14 @@ impl CalculatorEntry for Number {
|
|||||||
fn is_valid(&self) -> bool {
|
fn is_valid(&self) -> bool {
|
||||||
!self.value.is_nan() && !self.value.is_infinite()
|
!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> {
|
fn negate(&self) -> CalculatorResult<Entry> {
|
||||||
Ok(Entry::Number(Self { value: -self.value }))
|
Ok(Entry::Number(Self { value: -self.value }))
|
||||||
}
|
}
|
||||||
@ -941,14 +955,6 @@ impl CalculatorEntry for Number {
|
|||||||
impl Number {
|
impl Number {
|
||||||
pub const ZERO: Self = Self { value: 0.0_f64 };
|
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(
|
fn iterated_binary_vec(
|
||||||
self,
|
self,
|
||||||
vector: &Vector,
|
vector: &Vector,
|
||||||
@ -997,6 +1003,8 @@ where
|
|||||||
{
|
{
|
||||||
// Misc
|
// Misc
|
||||||
fn is_valid(&self) -> bool;
|
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 format_entry(&self, display_mode: &CalculatorDisplayMode) -> String;
|
||||||
fn to_editable_string(&self) -> CalculatorResult<String>;
|
fn to_editable_string(&self) -> CalculatorResult<String>;
|
||||||
// Mathematical operations
|
// Mathematical operations
|
||||||
|
Loading…
x
Reference in New Issue
Block a user