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