Cargo fmt
This commit is contained in:
parent
d84f2f7076
commit
752c7513aa
134
src/calc.rs
134
src/calc.rs
@ -7,15 +7,17 @@ use crate::calc::entries::CalculatorEntry;
|
|||||||
use confy::{load, store};
|
use confy::{load, store};
|
||||||
use entries::{Entry, Number};
|
use entries::{Entry, Number};
|
||||||
use errors::{CalculatorError, CalculatorResult};
|
use errors::{CalculatorError, CalculatorResult};
|
||||||
use operations::{ArithmeticOperation,CalculatorOperation, CalculatorStateChange, MacroState, OpArgs};
|
use operations::{
|
||||||
|
ArithmeticOperation, CalculatorOperation, CalculatorStateChange, MacroState, OpArgs,
|
||||||
|
};
|
||||||
use serde::ser::Serializer;
|
use serde::ser::Serializer;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::{BTreeMap, HashMap};
|
use std::collections::{BTreeMap, HashMap};
|
||||||
use std::collections::{HashSet, VecDeque};
|
use std::collections::{HashSet, VecDeque};
|
||||||
use types::{
|
use types::{
|
||||||
CalculatorAlignment, CalculatorAngleMode, CalculatorConstant, CalculatorConstants,
|
CalculatorAlignment, CalculatorAngleMode, CalculatorConstant, CalculatorConstants,
|
||||||
CalculatorDisplayMode, CalculatorMacro, CalculatorMacros, CalculatorRegisters,
|
CalculatorDisplayMode, CalculatorMacro, CalculatorMacros, CalculatorRegisters, CalculatorState,
|
||||||
CalculatorState, RegisterState,
|
RegisterState,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The maximum precision allowed for the calculator
|
/// The maximum precision allowed for the calculator
|
||||||
@ -186,8 +188,7 @@ impl Calculator {
|
|||||||
}
|
}
|
||||||
fn normal_input(&mut self, c: char) -> CalculatorResult<()> {
|
fn normal_input(&mut self, c: char) -> CalculatorResult<()> {
|
||||||
match c {
|
match c {
|
||||||
c @ '0'..='9' | c @ '.' | c @ 'e' => {
|
c @ '0'..='9' | c @ '.' | c @ 'e' => match c {
|
||||||
match c {
|
|
||||||
'0'..='9' => {
|
'0'..='9' => {
|
||||||
self.l.push(c);
|
self.l.push(c);
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -212,27 +213,64 @@ impl Calculator {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
_ => Err(CalculatorError::ParseError),
|
_ => Err(CalculatorError::ParseError),
|
||||||
}
|
},
|
||||||
}
|
'+' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
'+' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Add)),
|
ArithmeticOperation::Add,
|
||||||
'-' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Subtract)),
|
)),
|
||||||
'*' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Multiply)),
|
'-' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
'/' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Divide)),
|
ArithmeticOperation::Subtract,
|
||||||
'n' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Negate)),
|
)),
|
||||||
'|' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::AbsoluteValue)),
|
'*' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
'i' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Inverse)),
|
ArithmeticOperation::Multiply,
|
||||||
'%' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Modulo)),
|
)),
|
||||||
'?' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::IntegerDivide)),
|
'/' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
's' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Sin)),
|
ArithmeticOperation::Divide,
|
||||||
'c' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Cos)),
|
)),
|
||||||
't' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Tan)),
|
'n' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
'S' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::ASin)),
|
ArithmeticOperation::Negate,
|
||||||
'C' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::ACos)),
|
)),
|
||||||
'T' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::ATan)),
|
'|' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
'v' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Sqrt)),
|
ArithmeticOperation::AbsoluteValue,
|
||||||
'^' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Pow)),
|
)),
|
||||||
'l' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Log)),
|
'i' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
'L' => self.op(CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Ln)),
|
ArithmeticOperation::Inverse,
|
||||||
|
)),
|
||||||
|
'%' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
|
ArithmeticOperation::Modulo,
|
||||||
|
)),
|
||||||
|
'?' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
|
ArithmeticOperation::IntegerDivide,
|
||||||
|
)),
|
||||||
|
's' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
|
ArithmeticOperation::Sin,
|
||||||
|
)),
|
||||||
|
'c' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
|
ArithmeticOperation::Cos,
|
||||||
|
)),
|
||||||
|
't' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
|
ArithmeticOperation::Tan,
|
||||||
|
)),
|
||||||
|
'S' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
|
ArithmeticOperation::ASin,
|
||||||
|
)),
|
||||||
|
'C' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
|
ArithmeticOperation::ACos,
|
||||||
|
)),
|
||||||
|
'T' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
|
ArithmeticOperation::ATan,
|
||||||
|
)),
|
||||||
|
'v' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
|
ArithmeticOperation::Sqrt,
|
||||||
|
)),
|
||||||
|
'^' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
|
ArithmeticOperation::Pow,
|
||||||
|
)),
|
||||||
|
'l' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
|
ArithmeticOperation::Log,
|
||||||
|
)),
|
||||||
|
'L' => self.op(CalculatorOperation::ArithmeticOperation(
|
||||||
|
ArithmeticOperation::Ln,
|
||||||
|
)),
|
||||||
// Special
|
// Special
|
||||||
'\\' => self.op(CalculatorOperation::Drop),
|
'\\' => self.op(CalculatorOperation::Drop),
|
||||||
' ' => self.op(CalculatorOperation::Dup),
|
' ' => self.op(CalculatorOperation::Dup),
|
||||||
@ -245,13 +283,11 @@ impl Calculator {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
'r' => {
|
'r' => {
|
||||||
self.state =
|
self.state = CalculatorState::WaitingForRegister(RegisterState::Load);
|
||||||
CalculatorState::WaitingForRegister(RegisterState::Load);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
'R' => {
|
'R' => {
|
||||||
self.state =
|
self.state = CalculatorState::WaitingForRegister(RegisterState::Save);
|
||||||
CalculatorState::WaitingForRegister(RegisterState::Save);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
'`' => {
|
'`' => {
|
||||||
@ -310,11 +346,7 @@ impl Calculator {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
fn register_input(
|
fn register_input(&mut self, register_state: RegisterState, c: char) -> CalculatorResult<()> {
|
||||||
&mut self,
|
|
||||||
register_state: RegisterState,
|
|
||||||
c: char,
|
|
||||||
) -> CalculatorResult<()> {
|
|
||||||
match register_state {
|
match register_state {
|
||||||
RegisterState::Save => {
|
RegisterState::Save => {
|
||||||
let f = self.pop()?;
|
let f = self.pop()?;
|
||||||
@ -341,14 +373,8 @@ impl Calculator {
|
|||||||
'r' => self.angle_mode = CalculatorAngleMode::Radians,
|
'r' => self.angle_mode = CalculatorAngleMode::Radians,
|
||||||
'g' => self.angle_mode = CalculatorAngleMode::Grads,
|
'g' => self.angle_mode = CalculatorAngleMode::Grads,
|
||||||
'_' => self.display_mode = CalculatorDisplayMode::Default,
|
'_' => self.display_mode = CalculatorDisplayMode::Default,
|
||||||
',' => {
|
',' => self.display_mode = CalculatorDisplayMode::Separated { separator: ',' },
|
||||||
self.display_mode =
|
' ' => self.display_mode = CalculatorDisplayMode::Separated { separator: ' ' },
|
||||||
CalculatorDisplayMode::Separated { separator: ',' }
|
|
||||||
}
|
|
||||||
' ' => {
|
|
||||||
self.display_mode =
|
|
||||||
CalculatorDisplayMode::Separated { separator: ' ' }
|
|
||||||
}
|
|
||||||
's' => {
|
's' => {
|
||||||
self.display_mode = CalculatorDisplayMode::Scientific {
|
self.display_mode = CalculatorDisplayMode::Scientific {
|
||||||
precision: DEFAULT_PRECISION,
|
precision: DEFAULT_PRECISION,
|
||||||
@ -561,18 +587,18 @@ impl Calculator {
|
|||||||
CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Sqrt) => {
|
CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Sqrt) => {
|
||||||
self.unary_op(|a| Ok(OpArgs::Unary(a.sqrt()?)))
|
self.unary_op(|a| Ok(OpArgs::Unary(a.sqrt()?)))
|
||||||
}
|
}
|
||||||
CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Log) => self.unary_op(|a| Ok(OpArgs::Unary(a.log()?))),
|
CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Log) => {
|
||||||
CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Ln) => self.unary_op(|a| Ok(OpArgs::Unary(a.ln()?))),
|
self.unary_op(|a| Ok(OpArgs::Unary(a.log()?)))
|
||||||
|
}
|
||||||
|
CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Ln) => {
|
||||||
|
self.unary_op(|a| Ok(OpArgs::Unary(a.ln()?)))
|
||||||
|
}
|
||||||
CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Pow) => {
|
CalculatorOperation::ArithmeticOperation(ArithmeticOperation::Pow) => {
|
||||||
self.binary_op(|[a, b]| Ok(OpArgs::Unary(b.pow(a)?)))
|
self.binary_op(|[a, b]| Ok(OpArgs::Unary(b.pow(a)?)))
|
||||||
}
|
}
|
||||||
CalculatorOperation::Dup => {
|
CalculatorOperation::Dup => self.unary_op(|a| Ok(OpArgs::Binary([a.clone(), a]))),
|
||||||
self.unary_op(|a| Ok(OpArgs::Binary([a.clone(), a])))
|
|
||||||
}
|
|
||||||
CalculatorOperation::Drop => self.unary_op(|_| Ok(OpArgs::None)),
|
CalculatorOperation::Drop => self.unary_op(|_| Ok(OpArgs::None)),
|
||||||
CalculatorOperation::Swap => {
|
CalculatorOperation::Swap => self.binary_op(|[a, b]| Ok(OpArgs::Binary([b, a]))),
|
||||||
self.binary_op(|[a, b]| Ok(OpArgs::Binary([b, a])))
|
|
||||||
}
|
|
||||||
CalculatorOperation::Undo => return self.history_op(false),
|
CalculatorOperation::Undo => return self.history_op(false),
|
||||||
CalculatorOperation::Redo => return self.history_op(true),
|
CalculatorOperation::Redo => return self.history_op(true),
|
||||||
// Macros are a no-op operator; need to insert for undo/redo
|
// Macros are a no-op operator; need to insert for undo/redo
|
||||||
@ -603,11 +629,7 @@ impl Calculator {
|
|||||||
}
|
}
|
||||||
.last()
|
.last()
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
CalculatorError::EmptyHistory(String::from(if forward {
|
CalculatorError::EmptyHistory(String::from(if forward { "redo" } else { "undo" }))
|
||||||
"redo"
|
|
||||||
} else {
|
|
||||||
"undo"
|
|
||||||
}))
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let target_history_mode = if forward {
|
let target_history_mode = if forward {
|
||||||
|
@ -221,15 +221,9 @@ impl CalculatorEntry for Number {
|
|||||||
fn format_entry(&self, display_mode: &CalculatorDisplayMode) -> String {
|
fn format_entry(&self, display_mode: &CalculatorDisplayMode) -> String {
|
||||||
match display_mode {
|
match display_mode {
|
||||||
CalculatorDisplayMode::Default => format!("{}", self.value),
|
CalculatorDisplayMode::Default => format!("{}", self.value),
|
||||||
CalculatorDisplayMode::Separated { separator } => {
|
CalculatorDisplayMode::Separated { separator } => separated(self.value, *separator),
|
||||||
separated(self.value, *separator)
|
CalculatorDisplayMode::Scientific { precision } => scientific(self.value, *precision),
|
||||||
}
|
CalculatorDisplayMode::Engineering { precision } => engineering(self.value, *precision),
|
||||||
CalculatorDisplayMode::Scientific { precision } => {
|
|
||||||
scientific(self.value, *precision)
|
|
||||||
}
|
|
||||||
CalculatorDisplayMode::Engineering { precision } => {
|
|
||||||
engineering(self.value, *precision)
|
|
||||||
}
|
|
||||||
CalculatorDisplayMode::Fixed { precision } => {
|
CalculatorDisplayMode::Fixed { precision } => {
|
||||||
format!("{:0>.precision$}", self.value, precision = precision)
|
format!("{:0>.precision$}", self.value, precision = precision)
|
||||||
}
|
}
|
||||||
@ -286,9 +280,7 @@ impl CalculatorEntry for Number {
|
|||||||
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(),
|
||||||
CalculatorAngleMode::Grads => {
|
CalculatorAngleMode::Grads => (self.value * std::f64::consts::PI / 200.0).sin(),
|
||||||
(self.value * std::f64::consts::PI / 200.0).sin()
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@ -297,9 +289,7 @@ impl CalculatorEntry for Number {
|
|||||||
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(),
|
||||||
CalculatorAngleMode::Grads => {
|
CalculatorAngleMode::Grads => (self.value * std::f64::consts::PI / 200.0).cos(),
|
||||||
(self.value * std::f64::consts::PI / 200.0).cos()
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@ -308,9 +298,7 @@ impl CalculatorEntry for Number {
|
|||||||
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(),
|
||||||
CalculatorAngleMode::Grads => {
|
CalculatorAngleMode::Grads => (self.value * std::f64::consts::PI / 200.0).tan(),
|
||||||
(self.value * std::f64::consts::PI / 200.0).tan()
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@ -319,9 +307,7 @@ impl CalculatorEntry for Number {
|
|||||||
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(),
|
||||||
CalculatorAngleMode::Grads => {
|
CalculatorAngleMode::Grads => self.value.asin() * std::f64::consts::PI / 200.0,
|
||||||
self.value.asin() * std::f64::consts::PI / 200.0
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@ -330,9 +316,7 @@ impl CalculatorEntry for Number {
|
|||||||
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(),
|
||||||
CalculatorAngleMode::Grads => {
|
CalculatorAngleMode::Grads => self.value.acos() * std::f64::consts::PI / 200.0,
|
||||||
self.value.acos() * std::f64::consts::PI / 200.0
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@ -341,9 +325,7 @@ impl CalculatorEntry for Number {
|
|||||||
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(),
|
||||||
CalculatorAngleMode::Grads => {
|
CalculatorAngleMode::Grads => self.value.atan() * std::f64::consts::PI / 200.0,
|
||||||
self.value.atan() * std::f64::consts::PI / 200.0
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@ -372,7 +354,8 @@ impl CalculatorEntry for Number {
|
|||||||
impl CalculatorEntry for Vector {
|
impl CalculatorEntry for Vector {
|
||||||
// Misc
|
// Misc
|
||||||
fn is_valid(&self) -> bool {
|
fn is_valid(&self) -> bool {
|
||||||
!self.values
|
!self
|
||||||
|
.values
|
||||||
.iter()
|
.iter()
|
||||||
.find(|number| !number.is_valid())
|
.find(|number| !number.is_valid())
|
||||||
.is_some()
|
.is_some()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user