Cleanup numeric entries

This commit is contained in:
Austen Adler 2021-05-30 23:10:11 -04:00
parent dab0333b31
commit 39e3c83abc
2 changed files with 664 additions and 621 deletions

File diff suppressed because it is too large Load Diff

View File

@ -34,6 +34,7 @@ impl PartialEq for Number {
// pub value: Vec<Number>, // pub value: Vec<Number>,
// } // }
// TODO: Remove the copy trait
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)] #[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum Entry { pub enum Entry {
@ -194,61 +195,61 @@ impl CalculatorEntry for Number {
} }
fn add(&self, arg: Entry) -> CalculatorResult<Entry> { fn add(&self, arg: Entry) -> CalculatorResult<Entry> {
match arg { match arg {
Entry::Number(Number { value }) => Ok(Entry::Number(Number { Entry::Number(Self { value }) => Ok(Entry::Number(Self {
value: value + self.value, value: value + self.value,
})), })),
} }
} }
fn sub(&self, arg: Entry) -> CalculatorResult<Entry> { fn sub(&self, arg: Entry) -> CalculatorResult<Entry> {
match arg { match arg {
Entry::Number(Number { value }) => Ok(Entry::Number(Number { Entry::Number(Self { value }) => Ok(Entry::Number(Self {
value: value - self.value, value: value - self.value,
})), })),
} }
} }
fn mul(&self, arg: Entry) -> CalculatorResult<Entry> { fn mul(&self, arg: Entry) -> CalculatorResult<Entry> {
match arg { match arg {
Entry::Number(Number { value }) => Ok(Entry::Number(Number { Entry::Number(Self { value }) => Ok(Entry::Number(Self {
value: value * self.value, value: value * self.value,
})), })),
} }
} }
fn div(&self, arg: Entry) -> CalculatorResult<Entry> { fn div(&self, arg: Entry) -> CalculatorResult<Entry> {
match arg { match arg {
Entry::Number(Number { value }) => Ok(Entry::Number(Number { Entry::Number(Self { value }) => Ok(Entry::Number(Self {
value: value / self.value, value: value / self.value,
})), })),
} }
} }
fn int_divide(&self, arg: Entry) -> CalculatorResult<Entry> { fn int_divide(&self, arg: Entry) -> CalculatorResult<Entry> {
match arg { match arg {
Entry::Number(Number { value }) => Ok(Entry::Number(Number { Entry::Number(Self { value }) => Ok(Entry::Number(Self {
value: value.div_euclid(self.value), value: value.div_euclid(self.value),
})), })),
} }
} }
fn negate(&self) -> CalculatorResult<Entry> { fn negate(&self) -> CalculatorResult<Entry> {
Ok(Entry::Number(Number { value: -self.value })) Ok(Entry::Number(Self { value: -self.value }))
} }
fn abs(&self) -> CalculatorResult<Entry> { fn abs(&self) -> CalculatorResult<Entry> {
Ok(Entry::Number(Number { Ok(Entry::Number(Self {
value: self.value.abs(), value: self.value.abs(),
})) }))
} }
fn inverse(&self) -> CalculatorResult<Entry> { fn inverse(&self) -> CalculatorResult<Entry> {
Ok(Entry::Number(Number { Ok(Entry::Number(Self {
value: self.value.recip(), value: self.value.recip(),
})) }))
} }
fn modulo(&self, arg: Entry) -> CalculatorResult<Entry> { fn modulo(&self, arg: Entry) -> CalculatorResult<Entry> {
match arg { match arg {
Entry::Number(Number { value }) => Ok(Entry::Number(Number { Entry::Number(Self { value }) => Ok(Entry::Number(Self {
value: value % self.value, value: value % self.value,
})), })),
} }
} }
fn sin(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> { fn sin(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> {
Ok(Entry::Number(Number { Ok(Entry::Number(Self {
value: match angle_mode { value: match angle_mode {
CalculatorAngleMode::Degrees => self.value.to_radians().sin(), CalculatorAngleMode::Degrees => self.value.to_radians().sin(),
CalculatorAngleMode::Radians => self.value.sin(), CalculatorAngleMode::Radians => self.value.sin(),
@ -259,7 +260,7 @@ impl CalculatorEntry for Number {
})) }))
} }
fn cos(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> { fn cos(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> {
Ok(Entry::Number(Number { Ok(Entry::Number(Self {
value: match angle_mode { value: match angle_mode {
CalculatorAngleMode::Degrees => self.value.to_radians().cos(), CalculatorAngleMode::Degrees => self.value.to_radians().cos(),
CalculatorAngleMode::Radians => self.value.cos(), CalculatorAngleMode::Radians => self.value.cos(),
@ -270,7 +271,7 @@ impl CalculatorEntry for Number {
})) }))
} }
fn tan(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> { fn tan(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> {
Ok(Entry::Number(Number { Ok(Entry::Number(Self {
value: match angle_mode { value: match angle_mode {
CalculatorAngleMode::Degrees => self.value.to_radians().tan(), CalculatorAngleMode::Degrees => self.value.to_radians().tan(),
CalculatorAngleMode::Radians => self.value.tan(), CalculatorAngleMode::Radians => self.value.tan(),
@ -281,7 +282,7 @@ impl CalculatorEntry for Number {
})) }))
} }
fn asin(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> { fn asin(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> {
Ok(Entry::Number(Number { Ok(Entry::Number(Self {
value: match angle_mode { value: match angle_mode {
CalculatorAngleMode::Degrees => self.value.asin().to_degrees(), CalculatorAngleMode::Degrees => self.value.asin().to_degrees(),
CalculatorAngleMode::Radians => self.value.asin(), CalculatorAngleMode::Radians => self.value.asin(),
@ -292,7 +293,7 @@ impl CalculatorEntry for Number {
})) }))
} }
fn acos(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> { fn acos(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> {
Ok(Entry::Number(Number { Ok(Entry::Number(Self {
value: match angle_mode { value: match angle_mode {
CalculatorAngleMode::Degrees => self.value.acos().to_degrees(), CalculatorAngleMode::Degrees => self.value.acos().to_degrees(),
CalculatorAngleMode::Radians => self.value.acos(), CalculatorAngleMode::Radians => self.value.acos(),
@ -303,7 +304,7 @@ impl CalculatorEntry for Number {
})) }))
} }
fn atan(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> { fn atan(&self, angle_mode: CalculatorAngleMode) -> CalculatorResult<Entry> {
Ok(Entry::Number(Number { Ok(Entry::Number(Self {
value: match angle_mode { value: match angle_mode {
CalculatorAngleMode::Degrees => self.value.atan().to_degrees(), CalculatorAngleMode::Degrees => self.value.atan().to_degrees(),
CalculatorAngleMode::Radians => self.value.atan(), CalculatorAngleMode::Radians => self.value.atan(),
@ -314,23 +315,23 @@ impl CalculatorEntry for Number {
})) }))
} }
fn sqrt(&self) -> CalculatorResult<Entry> { fn sqrt(&self) -> CalculatorResult<Entry> {
Ok(Entry::Number(Number { Ok(Entry::Number(Self {
value: self.value.sqrt(), value: self.value.sqrt(),
})) }))
} }
fn log(&self) -> CalculatorResult<Entry> { fn log(&self) -> CalculatorResult<Entry> {
Ok(Entry::Number(Number { Ok(Entry::Number(Self {
value: self.value.log10(), value: self.value.log10(),
})) }))
} }
fn ln(&self) -> CalculatorResult<Entry> { fn ln(&self) -> CalculatorResult<Entry> {
Ok(Entry::Number(Number { Ok(Entry::Number(Self {
value: self.value.ln(), value: self.value.ln(),
})) }))
} }
fn pow(&self, arg: Entry) -> CalculatorResult<Entry> { fn pow(&self, arg: Entry) -> CalculatorResult<Entry> {
match arg { match arg {
Entry::Number(Number { value }) => Ok(Entry::Number(Number { Entry::Number(Self { value }) => Ok(Entry::Number(Self {
value: value.powf(self.value), value: value.powf(self.value),
})), })),
} }