Start work on complex types
This commit is contained in:
parent
4b0e6e7e10
commit
445ae3f535
1385
src/calc.rs
1385
src/calc.rs
File diff suppressed because it is too large
Load Diff
336
src/calc/entries.rs
Normal file
336
src/calc/entries.rs
Normal file
@ -0,0 +1,336 @@
|
||||
// use super::operations::CalculatorStateChange;
|
||||
use super::errors::CalculatorResult;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
pub struct Number {
|
||||
pub value: f64,
|
||||
}
|
||||
|
||||
// #[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
// pub struct Vector {
|
||||
// pub value: Vec<Number>,
|
||||
// }
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize)]
|
||||
pub enum Entry {
|
||||
Number(Number),
|
||||
// Vector(Vector),
|
||||
// Matrix(Vec<Vec<f64>>),
|
||||
}
|
||||
|
||||
impl CalculatorEntry for Entry {
|
||||
fn is_valid(&self) -> bool {
|
||||
match self {
|
||||
Entry::Number(number) => number.is_valid(),
|
||||
// Entry::Vector(vector) => vector.add(),
|
||||
}
|
||||
}
|
||||
fn add(&self, arg: Entry) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.add(arg),
|
||||
// Entry::Vector(vector) => vector.add(),
|
||||
}
|
||||
}
|
||||
fn sub(&self, arg: Entry) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.sub(arg),
|
||||
// Entry::Vector(vector) => vector.sub(),
|
||||
}
|
||||
}
|
||||
fn mul(&self, arg: Entry) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.mul(arg),
|
||||
// Entry::Vector(vector) => vector.mul(),
|
||||
}
|
||||
}
|
||||
fn div(&self, arg: Entry) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.div(arg),
|
||||
// Entry::Vector(vector) => vector.div(),
|
||||
}
|
||||
}
|
||||
fn int_divide(&self, arg: Entry) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.int_divide(arg),
|
||||
// Entry::Vector(vector) => vector.int_divide(),
|
||||
}
|
||||
}
|
||||
fn negate(&self) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.negate(),
|
||||
// Entry::Vector(vector) => vector.negate(),
|
||||
}
|
||||
}
|
||||
fn abs(&self) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.abs(),
|
||||
// Entry::Vector(vector) => vector.abs(),
|
||||
}
|
||||
}
|
||||
fn inverse(&self) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.inverse(),
|
||||
// Entry::Vector(vector) => vector.inverse(),
|
||||
}
|
||||
}
|
||||
fn modulo(&self, arg: Entry) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.modulo(arg),
|
||||
// Entry::Vector(vector) => vector.modulo(),
|
||||
}
|
||||
}
|
||||
fn sin(&self) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.sin(),
|
||||
// Entry::Vector(vector) => vector.sin(),
|
||||
}
|
||||
}
|
||||
fn cos(&self) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.cos(),
|
||||
// Entry::Vector(vector) => vector.cos(),
|
||||
}
|
||||
}
|
||||
fn tan(&self) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.tan(),
|
||||
// Entry::Vector(vector) => vector.tan(),
|
||||
}
|
||||
}
|
||||
fn asin(&self) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.asin(),
|
||||
// Entry::Vector(vector) => vector.asin(),
|
||||
}
|
||||
}
|
||||
fn acos(&self) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.acos(),
|
||||
// Entry::Vector(vector) => vector.acos(),
|
||||
}
|
||||
}
|
||||
fn atan(&self) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.atan(),
|
||||
// Entry::Vector(vector) => vector.atan(),
|
||||
}
|
||||
}
|
||||
fn sqrt(&self) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.sqrt(),
|
||||
// Entry::Vector(vector) => vector.sqrt(),
|
||||
}
|
||||
}
|
||||
fn log(&self) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.log(),
|
||||
// Entry::Vector(vector) => vector.log(),
|
||||
}
|
||||
}
|
||||
fn ln(&self) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.ln(),
|
||||
// Entry::Vector(vector) => vector.ln(),
|
||||
}
|
||||
}
|
||||
fn pow(&self, arg: Entry) -> CalculatorResult<Entry> {
|
||||
match self {
|
||||
Entry::Number(number) => number.pow(arg),
|
||||
// Entry::Vector(vector) => vector.pow(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CalculatorEntry for Number {
|
||||
fn is_valid(&self) -> bool {
|
||||
!self.value.is_nan() && !self.value.is_infinite()
|
||||
}
|
||||
fn add(&self, _arg: Entry) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
value: 1.0f64 + self.value,
|
||||
}))
|
||||
}
|
||||
fn sub(&self, _arg: Entry) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
value: 1.0f64 - self.value,
|
||||
}))
|
||||
}
|
||||
fn mul(&self, _arg: Entry) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
value: 1.0f64 * self.value,
|
||||
}))
|
||||
}
|
||||
fn div(&self, _arg: Entry) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
value: 1.0f64 / self.value,
|
||||
}))
|
||||
}
|
||||
fn int_divide(&self, _arg: Entry) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
value: 1.0f64.div_euclid(self.value),
|
||||
}))
|
||||
}
|
||||
fn negate(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number { value: -self.value }))
|
||||
}
|
||||
fn abs(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
value: self.value.abs(),
|
||||
}))
|
||||
}
|
||||
fn inverse(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
value: self.value.recip(),
|
||||
}))
|
||||
}
|
||||
fn modulo(&self, _arg: Entry) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
value: 1.0f64 % self.value,
|
||||
}))
|
||||
}
|
||||
fn sin(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number { value: self.value }))
|
||||
// match self.angle_mode {
|
||||
// CalculatorAngleMode::Degrees => {
|
||||
// |self.value: f64| a.to_radians().sin()
|
||||
// }
|
||||
// CalculatorAngleMode::Radians => |a: f64| a.sin(),
|
||||
// CalculatorAngleMode::Grads => |a: f64| {
|
||||
// (a * std::f64::consts::PI / 200.0).sin()
|
||||
// },
|
||||
// }
|
||||
}
|
||||
fn cos(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number { value: self.value }))
|
||||
// match self.angle_mode {
|
||||
// CalculatorAngleMode::Degrees => {
|
||||
// |self.value: f64| a.to_radians().cos()
|
||||
// }
|
||||
// CalculatorAngleMode::Radians => |a: f64| a.cos(),
|
||||
// CalculatorAngleMode::Grads => |a: f64| {
|
||||
// (a * std::f64::consts::PI / 200.0).cos()
|
||||
// },
|
||||
// }
|
||||
}
|
||||
fn tan(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number { value: self.value }))
|
||||
// match self.angle_mode {
|
||||
// CalculatorAngleMode::Degrees => {
|
||||
// |self.value: f64| a.to_radians().tan()
|
||||
// }
|
||||
// CalculatorAngleMode::Radians => |a: f64| a.tan(),
|
||||
// CalculatorAngleMode::Grads => |a: f64| {
|
||||
// (a * std::f64::consts::PI / 200.0).tan()
|
||||
// },
|
||||
// }
|
||||
}
|
||||
fn asin(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number { value: self.value }))
|
||||
// match self.angle_mode {
|
||||
// CalculatorAngleMode::Degrees => {
|
||||
// |self.value: f64| a.asin().to_degrees()
|
||||
// }
|
||||
// CalculatorAngleMode::Radians => |a: f64| a.asin(),
|
||||
// CalculatorAngleMode::Grads => |a: f64| {
|
||||
// a.asin() * std::f64::consts::PI / 200.0
|
||||
// },
|
||||
// }
|
||||
}
|
||||
fn acos(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number { value: self.value }))
|
||||
// match self.angle_mode {
|
||||
// CalculatorAngleMode::Degrees => {
|
||||
// |self.value: f64| a.acos().to_degrees()
|
||||
// }
|
||||
// CalculatorAngleMode::Radians => |a: f64| a.acos(),
|
||||
// CalculatorAngleMode::Grads => |a: f64| {
|
||||
// a.acos() * std::f64::consts::PI / 200.0
|
||||
// },
|
||||
// }
|
||||
}
|
||||
fn atan(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number { value: self.value }))
|
||||
// match self.angle_mode {
|
||||
// CalculatorAngleMode::Degrees => {
|
||||
// |self.value: f64| a.atan().to_degrees()
|
||||
// }
|
||||
// CalculatorAngleMode::Radians => |a: f64| a.atan(),
|
||||
// CalculatorAngleMode::Grads => |a: f64| {
|
||||
// a.atan() * std::f64::consts::PI / 200.0
|
||||
// },
|
||||
// }
|
||||
}
|
||||
fn sqrt(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
value: self.value.sqrt(),
|
||||
}))
|
||||
}
|
||||
fn log(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
value: self.value.log10(),
|
||||
}))
|
||||
}
|
||||
fn ln(&self) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
value: self.value.ln(),
|
||||
}))
|
||||
}
|
||||
fn pow(&self, _arg: Entry) -> CalculatorResult<Entry> {
|
||||
Ok(Entry::Number(Number {
|
||||
value: 1.0f64.powf(self.value),
|
||||
}))
|
||||
}
|
||||
// fn e(&self, _arg: Entry) -> CalculatorResult<Entry> {
|
||||
// Ok(Entry::Number(Number { value:1.0f64 * 10.0_f64.powf(self.value) }))
|
||||
// }
|
||||
}
|
||||
|
||||
pub trait CalculatorEntry {
|
||||
fn is_valid(&self) -> bool;
|
||||
fn add(&self, arg: Entry) -> CalculatorResult<Entry>;
|
||||
fn sub(&self, arg: Entry) -> CalculatorResult<Entry>;
|
||||
fn mul(&self, arg: Entry) -> CalculatorResult<Entry>;
|
||||
fn div(&self, arg: Entry) -> CalculatorResult<Entry>;
|
||||
fn int_divide(&self, arg: Entry) -> CalculatorResult<Entry>;
|
||||
fn negate(&self) -> CalculatorResult<Entry>;
|
||||
fn abs(&self) -> CalculatorResult<Entry>;
|
||||
fn inverse(&self) -> CalculatorResult<Entry>;
|
||||
fn modulo(&self, arg: Entry) -> CalculatorResult<Entry>;
|
||||
fn sin(&self) -> CalculatorResult<Entry>;
|
||||
fn cos(&self) -> CalculatorResult<Entry>;
|
||||
fn tan(&self) -> CalculatorResult<Entry>;
|
||||
fn asin(&self) -> CalculatorResult<Entry>;
|
||||
fn acos(&self) -> CalculatorResult<Entry>;
|
||||
fn atan(&self) -> CalculatorResult<Entry>;
|
||||
fn sqrt(&self) -> CalculatorResult<Entry>;
|
||||
fn log(&self) -> CalculatorResult<Entry>;
|
||||
fn ln(&self) -> CalculatorResult<Entry>;
|
||||
fn pow(&self, arg: Entry) -> CalculatorResult<Entry>;
|
||||
// fn e(&self, arg: Entry) -> CalculatorResult<Entry>;
|
||||
}
|
||||
|
||||
impl fmt::Display for Entry {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Number(Number { value }) => write!(f, "{}", value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Number {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self)
|
||||
}
|
||||
}
|
||||
|
||||
// impl fmt::Display for Vector {
|
||||
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
// match self {
|
||||
// Self::Degrees => write!(f, "DEG"),
|
||||
// Self::Radians => write!(f, "RAD"),
|
||||
// Self::Grads => write!(f, "GRD"),
|
||||
// }
|
||||
// }
|
||||
// }
|
@ -7,65 +7,68 @@ pub type CalculatorResult<T> = Result<T, CalculatorError>;
|
||||
/// All possible errors the calculator can throw
|
||||
#[derive(Debug)]
|
||||
pub enum CalculatorError {
|
||||
/// Divide by zero, log(-1), etc
|
||||
ArithmeticError,
|
||||
/// Not enough stck entries for operation
|
||||
NotEnoughStackEntries,
|
||||
/// Thrown when an undo or redo cannot be performed
|
||||
CorruptStateChange(String),
|
||||
/// Cannot undo or redo
|
||||
EmptyHistory(String),
|
||||
/// Constant undefined
|
||||
NoSuchConstant(char),
|
||||
/// Register undefined
|
||||
NoSuchRegister(char),
|
||||
/// Macro undefined
|
||||
NoSuchMacro(char),
|
||||
/// Operator undefined
|
||||
NoSuchOperator(char),
|
||||
/// Setting undefined
|
||||
NoSuchSetting(char),
|
||||
/// Macro calls itself
|
||||
RecursiveMacro(char),
|
||||
/// Could not convert l to number
|
||||
ParseError,
|
||||
/// Requested precision is too high
|
||||
PrecisionTooHigh,
|
||||
/// Config serialization error
|
||||
SaveError(Option<ConfyError>),
|
||||
/// Config deserialization error
|
||||
LoadError(Option<ConfyError>),
|
||||
/// Divide by zero, log(-1), etc
|
||||
ArithmeticError,
|
||||
/// Not enough stck entries for operation
|
||||
NotEnoughStackEntries,
|
||||
/// Requested type does not match target type
|
||||
TypeMismatch,
|
||||
/// Thrown when an undo or redo cannot be performed
|
||||
CorruptStateChange(String),
|
||||
/// Cannot undo or redo
|
||||
EmptyHistory(String),
|
||||
/// Constant undefined
|
||||
NoSuchConstant(char),
|
||||
/// Register undefined
|
||||
NoSuchRegister(char),
|
||||
/// Macro undefined
|
||||
NoSuchMacro(char),
|
||||
/// Operator undefined
|
||||
NoSuchOperator(char),
|
||||
/// Setting undefined
|
||||
NoSuchSetting(char),
|
||||
/// Macro calls itself
|
||||
RecursiveMacro(char),
|
||||
/// Could not convert l to number
|
||||
ParseError,
|
||||
/// Requested precision is too high
|
||||
PrecisionTooHigh,
|
||||
/// Config serialization error
|
||||
SaveError(Option<ConfyError>),
|
||||
/// Config deserialization error
|
||||
LoadError(Option<ConfyError>),
|
||||
}
|
||||
|
||||
impl error::Error for CalculatorError {}
|
||||
|
||||
impl fmt::Display for CalculatorError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::ArithmeticError => write!(f, "Arithmetic Error"),
|
||||
Self::NotEnoughStackEntries => write!(f, "Not enough items in the stack"),
|
||||
Self::CorruptStateChange(msg) => {
|
||||
write!(f, "Corrupt state change: {}", msg)
|
||||
}
|
||||
Self::EmptyHistory(msg) => write!(f, "No history to {}", msg),
|
||||
Self::NoSuchOperator(c) => write!(f, "No such operator '{}'", c),
|
||||
Self::NoSuchConstant(c) => write!(f, "No such constant '{}'", c),
|
||||
Self::NoSuchRegister(c) => write!(f, "No such register '{}'", c),
|
||||
Self::NoSuchMacro(c) => write!(f, "No such macro '{}'", c),
|
||||
Self::NoSuchSetting(c) => write!(f, "No such setting '{}'", c),
|
||||
Self::RecursiveMacro(c) => write!(f, "Recursive macro '{}'", c),
|
||||
Self::ParseError => write!(f, "Parse error"),
|
||||
Self::PrecisionTooHigh => write!(f, "Precision too high"),
|
||||
Self::SaveError(None) => write!(f, "Could not save"),
|
||||
Self::SaveError(Some(ConfyError::SerializeTomlError(e))) => {
|
||||
write!(f, "Save serialization error: {}", e)
|
||||
}
|
||||
Self::SaveError(Some(e)) => write!(f, "Could not save: {}", e),
|
||||
Self::LoadError(None) => write!(f, "Could not load"),
|
||||
Self::LoadError(Some(ConfyError::SerializeTomlError(e))) => {
|
||||
write!(f, "Load serialization error: {}", e)
|
||||
}
|
||||
Self::LoadError(Some(e)) => write!(f, "Could not load: {}", e),
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::ArithmeticError => write!(f, "Arithmetic Error"),
|
||||
Self::NotEnoughStackEntries => write!(f, "Not enough items in the stack"),
|
||||
Self::TypeMismatch => write!(f, "Type mismatch"),
|
||||
Self::CorruptStateChange(msg) => {
|
||||
write!(f, "Corrupt state change: {}", msg)
|
||||
}
|
||||
Self::EmptyHistory(msg) => write!(f, "No history to {}", msg),
|
||||
Self::NoSuchOperator(c) => write!(f, "No such operator '{}'", c),
|
||||
Self::NoSuchConstant(c) => write!(f, "No such constant '{}'", c),
|
||||
Self::NoSuchRegister(c) => write!(f, "No such register '{}'", c),
|
||||
Self::NoSuchMacro(c) => write!(f, "No such macro '{}'", c),
|
||||
Self::NoSuchSetting(c) => write!(f, "No such setting '{}'", c),
|
||||
Self::RecursiveMacro(c) => write!(f, "Recursive macro '{}'", c),
|
||||
Self::ParseError => write!(f, "Parse error"),
|
||||
Self::PrecisionTooHigh => write!(f, "Precision too high"),
|
||||
Self::SaveError(None) => write!(f, "Could not save"),
|
||||
Self::SaveError(Some(ConfyError::SerializeTomlError(e))) => {
|
||||
write!(f, "Save serialization error: {}", e)
|
||||
}
|
||||
Self::SaveError(Some(e)) => write!(f, "Could not save: {}", e),
|
||||
Self::LoadError(None) => write!(f, "Could not load"),
|
||||
Self::LoadError(Some(ConfyError::SerializeTomlError(e))) => {
|
||||
write!(f, "Load serialization error: {}", e)
|
||||
}
|
||||
Self::LoadError(Some(e)) => write!(f, "Could not load: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
use super::entries::Entry;
|
||||
use serde::{Deserialize, Serialize};
|
||||
/// Operations that can be sent to the calculator such as +, -, or undo
|
||||
#[derive(PartialEq, Debug, Serialize, Deserialize)]
|
||||
@ -12,9 +13,6 @@ pub enum CalculatorOperation {
|
||||
Modulo,
|
||||
IntegerDivide,
|
||||
//Remainder,
|
||||
Drop,
|
||||
Dup,
|
||||
Swap,
|
||||
Sin,
|
||||
Cos,
|
||||
Tan,
|
||||
@ -28,7 +26,9 @@ pub enum CalculatorOperation {
|
||||
// Factorial,
|
||||
Log,
|
||||
Ln,
|
||||
E,
|
||||
Drop,
|
||||
Dup,
|
||||
Swap,
|
||||
Macro(MacroState),
|
||||
}
|
||||
|
||||
@ -45,9 +45,9 @@ pub enum OpArgs {
|
||||
/// This is a macro start and end noop
|
||||
Macro(MacroState),
|
||||
/// Operation takes 1 argument, ex: sqrt or negate
|
||||
Unary(f64),
|
||||
Unary(Entry),
|
||||
/// Operation takes 2 arguments, ex: + or -
|
||||
Binary([f64; 2]),
|
||||
Binary([Entry; 2]),
|
||||
/// Operation takes no arguments, ex: push
|
||||
None,
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
use super::entries::Entry;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
@ -5,38 +6,38 @@ use std::fmt;
|
||||
/// The calculator state
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub enum CalculatorState {
|
||||
Normal,
|
||||
WaitingForConstant,
|
||||
WaitingForMacro,
|
||||
WaitingForRegister(RegisterState),
|
||||
WaitingForSetting,
|
||||
Normal,
|
||||
WaitingForConstant,
|
||||
WaitingForMacro,
|
||||
WaitingForRegister(RegisterState),
|
||||
WaitingForSetting,
|
||||
}
|
||||
|
||||
impl Default for CalculatorState {
|
||||
fn default() -> Self {
|
||||
Self::Normal
|
||||
}
|
||||
fn default() -> Self {
|
||||
Self::Normal
|
||||
}
|
||||
}
|
||||
|
||||
/// The state of the requested register operation
|
||||
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
||||
pub enum RegisterState {
|
||||
Save,
|
||||
Load,
|
||||
Save,
|
||||
Load,
|
||||
}
|
||||
|
||||
/// One calculator constant containing a message and value
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CalculatorConstant {
|
||||
pub help: String,
|
||||
pub value: f64,
|
||||
pub help: String,
|
||||
pub value: Entry,
|
||||
}
|
||||
|
||||
/// One calculator macro containing a messsage and value
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CalculatorMacro {
|
||||
pub help: String,
|
||||
pub value: String,
|
||||
pub help: String,
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
/// Map of chars to constants
|
||||
@ -46,264 +47,269 @@ pub type CalculatorConstants = HashMap<char, CalculatorConstant>;
|
||||
pub type CalculatorMacros = HashMap<char, CalculatorMacro>;
|
||||
|
||||
/// Map of chars to registers
|
||||
pub type CalculatorRegisters = HashMap<char, f64>;
|
||||
pub type CalculatorRegisters = HashMap<char, Entry>;
|
||||
|
||||
/// Possible calculator angle modes
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(tag = "angle_mode")]
|
||||
pub enum CalculatorAngleMode {
|
||||
Degrees,
|
||||
Radians,
|
||||
Grads,
|
||||
Degrees,
|
||||
Radians,
|
||||
Grads,
|
||||
}
|
||||
|
||||
impl Default for CalculatorAngleMode {
|
||||
fn default() -> Self {
|
||||
Self::Degrees
|
||||
}
|
||||
fn default() -> Self {
|
||||
Self::Degrees
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for CalculatorAngleMode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Degrees => write!(f, "DEG"),
|
||||
Self::Radians => write!(f, "RAD"),
|
||||
Self::Grads => write!(f, "GRD"),
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Degrees => write!(f, "DEG"),
|
||||
Self::Radians => write!(f, "RAD"),
|
||||
Self::Grads => write!(f, "GRD"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The calculator digit display mode
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(tag = "display_mode")]
|
||||
pub enum CalculatorDisplayMode {
|
||||
/// Rust's default f64 format
|
||||
Default,
|
||||
/// Thousands separator
|
||||
Separated { separator: char },
|
||||
/// Aligned scientific format
|
||||
Scientific { precision: usize },
|
||||
/// Scientific format, chunked by groups of 3
|
||||
///
|
||||
/// Example: 1 E+5 or 100E+5
|
||||
Engineering { precision: usize },
|
||||
/// Fixed precision
|
||||
Fixed { precision: usize },
|
||||
/// Rust's default Entry format
|
||||
Default,
|
||||
/// Thousands separator
|
||||
Separated { separator: char },
|
||||
/// Aligned scientific format
|
||||
Scientific { precision: usize },
|
||||
/// Scientific format, chunked by groups of 3
|
||||
///
|
||||
/// Example: 1 E+5 or 100E+5
|
||||
Engineering { precision: usize },
|
||||
/// Fixed precision
|
||||
Fixed { precision: usize },
|
||||
}
|
||||
|
||||
impl fmt::Display for CalculatorDisplayMode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Default => write!(f, "DEF"),
|
||||
Self::Separated { separator } => write!(f, "SEP({})", separator),
|
||||
Self::Scientific { precision } => write!(f, "SCI({})", precision),
|
||||
Self::Engineering { precision } => write!(f, "ENG({})", precision),
|
||||
Self::Fixed { precision } => write!(f, "FIX({})", precision),
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Default => write!(f, "DEF"),
|
||||
Self::Separated { separator } => write!(f, "SEP({})", separator),
|
||||
Self::Scientific { precision } => write!(f, "SCI({})", precision),
|
||||
Self::Engineering { precision } => write!(f, "ENG({})", precision),
|
||||
Self::Fixed { precision } => write!(f, "FIX({})", precision),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CalculatorDisplayMode {
|
||||
fn default() -> Self {
|
||||
Self::Default
|
||||
}
|
||||
fn default() -> Self {
|
||||
Self::Default
|
||||
}
|
||||
}
|
||||
|
||||
impl CalculatorDisplayMode {
|
||||
pub fn format_number(&self, number: f64) -> String {
|
||||
match self {
|
||||
Self::Default => format!("{}", number),
|
||||
Self::Separated { separator } => Self::separated(number, *separator),
|
||||
Self::Scientific { precision } => Self::scientific(number, *precision),
|
||||
Self::Engineering { precision } => Self::engineering(number, *precision),
|
||||
Self::Fixed { precision } => {
|
||||
format!("{:0>.precision$}", number, precision = precision)
|
||||
}
|
||||
pub fn format_number(&self, number: &Entry) -> String {
|
||||
match self {
|
||||
Self::Default => format!("{}", number),
|
||||
Self::Separated { separator } => Self::separated(number, *separator),
|
||||
Self::Scientific { precision } => Self::scientific(number, *precision),
|
||||
Self::Engineering { precision } => Self::engineering(number, *precision),
|
||||
Self::Fixed { precision } => {
|
||||
format!("{:0>.precision$}", number, precision = precision)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Based on https://stackoverflow.com/a/65266882
|
||||
fn scientific(f: f64, precision: usize) -> String {
|
||||
let mut ret = format!("{:.precision$E}", f, precision = precision);
|
||||
let exp = ret.split_off(ret.find('E').unwrap_or(0));
|
||||
let (exp_sign, exp) = exp
|
||||
.strip_prefix("E-")
|
||||
.map_or_else(|| ('+', &exp[1..]), |stripped| ('-', stripped));
|
||||
// Based on https://stackoverflow.com/a/65266882
|
||||
fn scientific(_f: &Entry, _precision: usize) -> String {
|
||||
// TODO
|
||||
String::from("TODO")
|
||||
// let mut ret = format!("{:.precision$E}", f, precision = precision);
|
||||
// let exp = ret.split_off(ret.find('E').unwrap_or(0));
|
||||
// let (exp_sign, exp) = exp
|
||||
// .strip_prefix("E-")
|
||||
// .map_or_else(|| ('+', &exp[1..]), |stripped| ('-', stripped));
|
||||
|
||||
let sign = if ret.starts_with('-') { "" } else { " " };
|
||||
format!("{}{} E{}{:0>pad$}", sign, ret, exp_sign, exp, pad = 2)
|
||||
}
|
||||
|
||||
fn engineering(f: f64, precision: usize) -> String {
|
||||
// Format the string so the first digit is always in the first column, and remove '.'. Requested precision + 2 to account for using 1, 2, or 3 digits for the whole portion of the string
|
||||
// 1,000 => 1000E3
|
||||
let all = format!(" {:.precision$E}", f, precision = precision)
|
||||
// Remove . since it can be moved
|
||||
.replacen(".", "", 1)
|
||||
// Add 00E before E here so the length is enough for slicing below
|
||||
.replacen("E", "00E", 1);
|
||||
// Extract mantissa and the string representation of the exponent. Unwrap should be safe as formatter will insert E
|
||||
// 1000E3 => (1000, E3)
|
||||
let (num_str, exp_str) = all.split_at(all.find('E').unwrap());
|
||||
// Extract the exponent as an isize. This should always be true because f64 max will be ~400
|
||||
// E3 => 3 as isize
|
||||
let exp = exp_str[1..].parse::<isize>().unwrap();
|
||||
// Sign of the exponent. If string representation starts with E-, then negative
|
||||
let display_exp_sign = if exp_str.strip_prefix("E-").is_some() {
|
||||
'-'
|
||||
} else {
|
||||
'+'
|
||||
};
|
||||
|
||||
// The exponent to display. Always a multiple of 3 in engineering mode. Always positive because sign is added with display_exp_sign above
|
||||
// 100 => 0, 1000 => 3, .1 => 3 (but will show as -3)
|
||||
let display_exp = (exp.div_euclid(3) * 3).abs();
|
||||
// Number of whole digits. Always 1, 2, or 3 depending on exponent divisibility
|
||||
let num_whole_digits = exp.rem_euclid(3) as usize + 1;
|
||||
|
||||
// If this is a negative number, strip off the added space, otherwise keep the space (and next digit)
|
||||
let num_str = if num_str.strip_prefix(" -").is_some() {
|
||||
&num_str[1..]
|
||||
} else {
|
||||
num_str
|
||||
};
|
||||
|
||||
// Whole portion of number. Slice is safe because the num_whole_digits is always 3 and the num_str will always have length >= 3 since precision in all=2 (+original whole digit)
|
||||
// Original number is 1,000 => whole will be 1, if original is 0.01, whole will be 10
|
||||
let whole = &num_str[0..=num_whole_digits];
|
||||
// Decimal portion of the number. Sliced from the number of whole digits to the *requested* precision. Precision generated in all will be requested precision + 2
|
||||
let decimal = &num_str[(num_whole_digits + 1)..=(precision + num_whole_digits)];
|
||||
// Right align whole portion, always have decimal point
|
||||
format!(
|
||||
"{: >4}.{} E{}{:0>pad$}",
|
||||
// display_sign,
|
||||
whole,
|
||||
decimal,
|
||||
display_exp_sign,
|
||||
display_exp,
|
||||
pad = 2
|
||||
)
|
||||
}
|
||||
|
||||
fn separated(f: f64, sep: char) -> String {
|
||||
let mut ret = f.to_string();
|
||||
let start = if ret.starts_with('-') { 1 } else { 0 };
|
||||
let end = ret.find('.').unwrap_or_else(|| ret.len());
|
||||
for i in 0..((end - start - 1).div_euclid(3)) {
|
||||
ret.insert(end - (i + 1) * 3, sep);
|
||||
// let sign = if ret.starts_with('-') { "" } else { " " };
|
||||
// format!("{}{} E{}{:0>pad$}", sign, ret, exp_sign, exp, pad = 2)
|
||||
}
|
||||
|
||||
fn engineering(_f: &Entry, _precision: usize) -> String {
|
||||
// TODO
|
||||
String::from("TODO")
|
||||
/*// Format the string so the first digit is always in the first column, and remove '.'. Requested precision + 2 to account for using 1, 2, or 3 digits for the whole portion of the string
|
||||
// 1,000 => 1000E3
|
||||
let all = format!(" {:.precision$E}", f, precision = precision)
|
||||
// Remove . since it can be moved
|
||||
.replacen(".", "", 1)
|
||||
// Add 00E before E here so the length is enough for slicing below
|
||||
.replacen("E", "00E", 1);
|
||||
// Extract mantissa and the string representation of the exponent. Unwrap should be safe as formatter will insert E
|
||||
// 1000E3 => (1000, E3)
|
||||
let (num_str, exp_str) = all.split_at(all.find('E').unwrap());
|
||||
// Extract the exponent as an isize. This should always be true because Entry max will be ~400
|
||||
// E3 => 3 as isize
|
||||
let exp = exp_str[1..].parse::<isize>().unwrap();
|
||||
// Sign of the exponent. If string representation starts with E-, then negative
|
||||
let display_exp_sign = if exp_str.strip_prefix("E-").is_some() {
|
||||
'-'
|
||||
} else {
|
||||
'+'
|
||||
};
|
||||
|
||||
// The exponent to display. Always a multiple of 3 in engineering mode. Always positive because sign is added with display_exp_sign above
|
||||
// 100 => 0, 1000 => 3, .1 => 3 (but will show as -3)
|
||||
let display_exp = (exp.div_euclid(3) * 3).abs();
|
||||
// Number of whole digits. Always 1, 2, or 3 depending on exponent divisibility
|
||||
let num_whole_digits = exp.rem_euclid(3) as usize + 1;
|
||||
|
||||
// If this is a negative number, strip off the added space, otherwise keep the space (and next digit)
|
||||
let num_str = if num_str.strip_prefix(" -").is_some() {
|
||||
&num_str[1..]
|
||||
} else {
|
||||
num_str
|
||||
};
|
||||
|
||||
// Whole portion of number. Slice is safe because the num_whole_digits is always 3 and the num_str will always have length >= 3 since precision in all=2 (+original whole digit)
|
||||
// Original number is 1,000 => whole will be 1, if original is 0.01, whole will be 10
|
||||
let whole = &num_str[0..=num_whole_digits];
|
||||
// Decimal portion of the number. Sliced from the number of whole digits to the *requested* precision. Precision generated in all will be requested precision + 2
|
||||
let decimal = &num_str[(num_whole_digits + 1)..=(precision + num_whole_digits)];
|
||||
// Right align whole portion, always have decimal point
|
||||
format!(
|
||||
"{: >4}.{} E{}{:0>pad$}",
|
||||
// display_sign,
|
||||
whole,
|
||||
decimal,
|
||||
display_exp_sign,
|
||||
display_exp,
|
||||
pad = 2
|
||||
)
|
||||
*/
|
||||
}
|
||||
|
||||
fn separated(f: &Entry, sep: char) -> String {
|
||||
let mut ret = f.to_string();
|
||||
let start = if ret.starts_with('-') { 1 } else { 0 };
|
||||
let end = ret.find('.').unwrap_or_else(|| ret.len());
|
||||
for i in 0..((end - start - 1).div_euclid(3)) {
|
||||
ret.insert(end - (i + 1) * 3, sep);
|
||||
}
|
||||
ret
|
||||
}
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
/// Left or right calculator alignment
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub enum CalculatorAlignment {
|
||||
Right,
|
||||
Left,
|
||||
Right,
|
||||
Left,
|
||||
}
|
||||
|
||||
impl Default for CalculatorAlignment {
|
||||
fn default() -> Self {
|
||||
Self::Left
|
||||
}
|
||||
fn default() -> Self {
|
||||
Self::Left
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for CalculatorAlignment {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Left => write!(f, "L"),
|
||||
Self::Right => write!(f, "R"),
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Left => write!(f, "L"),
|
||||
Self::Right => write!(f, "R"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_scientific() {
|
||||
for (f, precision, s) in vec![
|
||||
// Basic
|
||||
(1.0, 0, " 1 E+00"),
|
||||
(-1.0, 0, "-1 E+00"),
|
||||
(100.0, 0, " 1 E+02"),
|
||||
(0.1, 0, " 1 E-01"),
|
||||
(0.01, 0, " 1 E-02"),
|
||||
(-0.1, 0, "-1 E-01"),
|
||||
// i
|
||||
(1.0, 0, " 1 E+00"),
|
||||
// Precision
|
||||
(-0.123456789, 3, "-1.235 E-01"),
|
||||
(-0.123456789, 2, "-1.23 E-01"),
|
||||
(-0.123456789, 2, "-1.23 E-01"),
|
||||
(-1e99, 2, "-1.00 E+99"),
|
||||
(-1e100, 2, "-1.00 E+100"),
|
||||
// Rounding
|
||||
(0.5, 2, " 5.00 E-01"),
|
||||
(0.5, 1, " 5.0 E-01"),
|
||||
(0.5, 0, " 5 E-01"),
|
||||
(1.5, 2, " 1.50 E+00"),
|
||||
(1.5, 1, " 1.5 E+00"),
|
||||
(1.5, 0, " 2 E+00"),
|
||||
] {
|
||||
assert_eq!(
|
||||
CalculatorDisplayMode::Scientific { precision }.format_number(f),
|
||||
s
|
||||
);
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_scientific() {
|
||||
for (f, precision, s) in vec![
|
||||
// Basic
|
||||
(1.0, 0, " 1 E+00"),
|
||||
(-1.0, 0, "-1 E+00"),
|
||||
(100.0, 0, " 1 E+02"),
|
||||
(0.1, 0, " 1 E-01"),
|
||||
(0.01, 0, " 1 E-02"),
|
||||
(-0.1, 0, "-1 E-01"),
|
||||
// i
|
||||
(1.0, 0, " 1 E+00"),
|
||||
// Precision
|
||||
(-0.123_456_789, 3, "-1.235 E-01"),
|
||||
(-0.123_456_789, 2, "-1.23 E-01"),
|
||||
(-0.123_456_789, 2, "-1.23 E-01"),
|
||||
(-1e99, 2, "-1.00 E+99"),
|
||||
(-1e100, 2, "-1.00 E+100"),
|
||||
// Rounding
|
||||
(0.5, 2, " 5.00 E-01"),
|
||||
(0.5, 1, " 5.0 E-01"),
|
||||
(0.5, 0, " 5 E-01"),
|
||||
(1.5, 2, " 1.50 E+00"),
|
||||
(1.5, 1, " 1.5 E+00"),
|
||||
(1.5, 0, " 2 E+00"),
|
||||
] {
|
||||
assert_eq!(
|
||||
CalculatorDisplayMode::Scientific { precision }.format_number(f),
|
||||
s
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_separated() {
|
||||
for (f, separator, s) in vec![
|
||||
(100.0, ',', "100"),
|
||||
(100.0, ',', "100"),
|
||||
(-100.0, ',', "-100"),
|
||||
(1_000.0, ',', "1,000"),
|
||||
(-1_000.0, ',', "-1,000"),
|
||||
(10_000.0, ',', "10,000"),
|
||||
(-10_000.0, ',', "-10,000"),
|
||||
(100_000.0, ',', "100,000"),
|
||||
(-100_000.0, ',', "-100,000"),
|
||||
(1_000_000.0, ',', "1,000,000"),
|
||||
(-1_000_000.0, ',', "-1,000,000"),
|
||||
(1_000_000.123456789, ',', "1,000,000.123456789"),
|
||||
(-1_000_000.123456789, ',', "-1,000,000.123456789"),
|
||||
(1_000_000.123456789, ' ', "1 000 000.123456789"),
|
||||
(1_000_000.123456789, ' ', "1 000 000.123456789"),
|
||||
] {
|
||||
assert_eq!(
|
||||
CalculatorDisplayMode::Separated { separator }.format_number(f),
|
||||
s
|
||||
);
|
||||
#[test]
|
||||
fn test_separated() {
|
||||
for (f, separator, s) in vec![
|
||||
(100.0, ',', "100"),
|
||||
(100.0, ',', "100"),
|
||||
(-100.0, ',', "-100"),
|
||||
(1_000.0, ',', "1,000"),
|
||||
(-1_000.0, ',', "-1,000"),
|
||||
(10_000.0, ',', "10,000"),
|
||||
(-10_000.0, ',', "-10,000"),
|
||||
(100_000.0, ',', "100,000"),
|
||||
(-100_000.0, ',', "-100,000"),
|
||||
(1_000_000.0, ',', "1,000,000"),
|
||||
(-1_000_000.0, ',', "-1,000,000"),
|
||||
(1_000_000.123_456_789, ',', "1,000,000.123456789"),
|
||||
(-1_000_000.123_456_789, ',', "-1,000,000.123456789"),
|
||||
(1_000_000.123_456_789, ' ', "1 000 000.123456789"),
|
||||
(1_000_000.123_456_789, ' ', "1 000 000.123456789"),
|
||||
] {
|
||||
assert_eq!(
|
||||
CalculatorDisplayMode::Separated { separator }.format_number(f),
|
||||
s
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_engineering() {
|
||||
for (f, precision, s) in vec![
|
||||
(100.0, 3, " 100.000 E+00"),
|
||||
(100.0, 3, " 100.000 E+00"),
|
||||
(-100.0, 3, "-100.000 E+00"),
|
||||
(100.0, 0, " 100. E+00"),
|
||||
(-100.0, 0, "-100. E+00"),
|
||||
(0.1, 2, " 100.00 E-03"),
|
||||
(0.01, 2, " 10.00 E-03"),
|
||||
(0.001, 2, " 1.00 E-03"),
|
||||
(0.0001, 2, " 100.00 E-06"),
|
||||
// Rounding
|
||||
(0.5, 2, " 500.00 E-03"),
|
||||
(0.5, 1, " 500.0 E-03"),
|
||||
(0.5, 0, " 500. E-03"),
|
||||
(1.5, 2, " 1.50 E+00"),
|
||||
(1.5, 1, " 1.5 E+00"),
|
||||
(1.5, 0, " 2. E+00"),
|
||||
] {
|
||||
assert_eq!(
|
||||
CalculatorDisplayMode::Engineering { precision }.format_number(f),
|
||||
s
|
||||
);
|
||||
#[test]
|
||||
fn test_engineering() {
|
||||
for (f, precision, s) in vec![
|
||||
(100.0, 3, " 100.000 E+00"),
|
||||
(100.0, 3, " 100.000 E+00"),
|
||||
(-100.0, 3, "-100.000 E+00"),
|
||||
(100.0, 0, " 100. E+00"),
|
||||
(-100.0, 0, "-100. E+00"),
|
||||
(0.1, 2, " 100.00 E-03"),
|
||||
(0.01, 2, " 10.00 E-03"),
|
||||
(0.001, 2, " 1.00 E-03"),
|
||||
(0.0001, 2, " 100.00 E-06"),
|
||||
// Rounding
|
||||
(0.5, 2, " 500.00 E-03"),
|
||||
(0.5, 1, " 500.0 E-03"),
|
||||
(0.5, 0, " 500. E-03"),
|
||||
(1.5, 2, " 1.50 E+00"),
|
||||
(1.5, 1, " 1.5 E+00"),
|
||||
(1.5, 0, " 2. E+00"),
|
||||
] {
|
||||
assert_eq!(
|
||||
CalculatorDisplayMode::Engineering { precision }.format_number(f),
|
||||
s
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ impl App {
|
||||
"{}: {} ({})",
|
||||
key,
|
||||
constant.help,
|
||||
self.calculator.display_mode.format_number(constant.value)
|
||||
self.calculator.display_mode.format_number(&constant.value)
|
||||
)
|
||||
})
|
||||
.fold(String::new(), |acc, s| acc + &s + "\n")
|
||||
@ -233,9 +233,11 @@ impl App {
|
||||
.enumerate()
|
||||
.rev()
|
||||
.map(|(i, m)| {
|
||||
let number = self.calculator.display_mode.format_number(*m);
|
||||
let number = self.calculator.display_mode.format_number(&*m);
|
||||
let content = match self.calculator.calculator_alignment {
|
||||
CalculatorAlignment::Left => format!("{:>2}: {}", i, number),
|
||||
CalculatorAlignment::Left => {
|
||||
format!("{:>2}: {}", i, number)
|
||||
}
|
||||
CalculatorAlignment::Right => {
|
||||
let ret = format!("{} :{:>2}", number, i);
|
||||
if ret.len() < chunk.width.saturating_sub(BORDER_SIZE) as usize {
|
||||
|
Loading…
Reference in New Issue
Block a user