Remove getters and setters

This commit is contained in:
Austen Adler 2021-05-19 18:17:44 -04:00
parent 6fe3f27c85
commit 4f4af9c5ef
3 changed files with 26 additions and 53 deletions

View File

@ -4,9 +4,8 @@ pub mod operations;
use confy::{load, store}; use confy::{load, store};
use constants::{ use constants::{
CalculatorAngleMode, CalculatorConstant, CalculatorConstants, CalculatorConstantsIter, CalculatorAngleMode, CalculatorConstant, CalculatorConstants, CalculatorDisplayMode,
CalculatorDisplayMode, CalculatorMacro, CalculatorMacros, CalculatorMacrosIter, CalculatorMacro, CalculatorMacros, CalculatorRegisters, CalculatorState, RegisterState,
CalculatorRegisters, CalculatorRegistersIter, CalculatorState, RegisterState,
}; };
use errors::{CalculatorError, CalculatorResult}; use errors::{CalculatorError, CalculatorResult};
use operations::{CalculatorOperation, CalculatorStateChange, MacroState, OpArgs}; use operations::{CalculatorOperation, CalculatorStateChange, MacroState, OpArgs};
@ -37,23 +36,23 @@ pub struct Calculator {
#[serde(skip)] #[serde(skip)]
l: String, l: String,
save_on_close: bool, save_on_close: bool,
stack: VecDeque<f64>, pub stack: VecDeque<f64>,
#[serde(serialize_with = "ordered_char_map")] #[serde(serialize_with = "ordered_char_map")]
macros: CalculatorMacros, pub macros: CalculatorMacros,
#[serde(skip)] #[serde(skip)]
active_macros: HashSet<char>, active_macros: HashSet<char>,
#[serde(serialize_with = "ordered_char_map")] #[serde(serialize_with = "ordered_char_map")]
constants: CalculatorConstants, pub constants: CalculatorConstants,
#[serde(skip)] #[serde(skip)]
registers: CalculatorRegisters, pub registers: CalculatorRegisters,
#[serde(skip)] #[serde(skip)]
undo_buf: Vec<CalculatorStateChange>, undo_buf: Vec<CalculatorStateChange>,
#[serde(skip)] #[serde(skip)]
redo_buf: Vec<CalculatorStateChange>, redo_buf: Vec<CalculatorStateChange>,
#[serde(skip)] #[serde(skip)]
state: CalculatorState, pub state: CalculatorState,
angle_mode: CalculatorAngleMode, pub angle_mode: CalculatorAngleMode,
display_mode: CalculatorDisplayMode, pub display_mode: CalculatorDisplayMode,
// calculator_alignment: CalculatorAlignment, // calculator_alignment: CalculatorAlignment,
} }
@ -382,37 +381,12 @@ impl Calculator {
pub fn get_status_line(&self) -> String { pub fn get_status_line(&self) -> String {
format!( format!(
"[{}] [{}] [{}]", "[{}] [{}] [{}]",
self.get_display_mode(), self.display_mode,
self.get_angle_mode(), self.angle_mode,
if self.get_save_on_close() { "W" } else { "w" } if self.save_on_close { "W" } else { "w" }
) )
} }
pub fn get_state(&self) -> &CalculatorState {
&self.state
}
pub fn get_constants_iter(&self) -> CalculatorConstantsIter {
self.constants.iter()
}
pub fn get_macros_iter(&self) -> CalculatorMacrosIter {
self.macros.iter()
}
pub fn get_registers_iter(&self) -> CalculatorRegistersIter {
self.registers.iter()
}
pub fn get_stack(&self) -> &VecDeque<f64> {
&self.stack
}
pub fn get_angle_mode(&self) -> &CalculatorAngleMode {
&self.angle_mode
}
pub fn get_display_mode(&self) -> &CalculatorDisplayMode {
&self.display_mode
}
pub fn get_save_on_close(&self) -> bool {
self.save_on_close
}
pub fn flush_l(&mut self) -> CalculatorResult<bool> { pub fn flush_l(&mut self) -> CalculatorResult<bool> {
if self.l.is_empty() { if self.l.is_empty() {
Ok(false) Ok(false)

View File

@ -1,5 +1,4 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::hash_map::Iter;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
@ -39,13 +38,10 @@ pub struct CalculatorMacro {
} }
pub type CalculatorConstants = HashMap<char, CalculatorConstant>; pub type CalculatorConstants = HashMap<char, CalculatorConstant>;
pub type CalculatorConstantsIter<'a> = Iter<'a, char, CalculatorConstant>;
pub type CalculatorMacros = HashMap<char, CalculatorMacro>; pub type CalculatorMacros = HashMap<char, CalculatorMacro>;
pub type CalculatorMacrosIter<'a> = Iter<'a, char, CalculatorMacro>;
pub type CalculatorRegisters = HashMap<char, f64>; pub type CalculatorRegisters = HashMap<char, f64>;
pub type CalculatorRegistersIter<'a> = Iter<'a, char, f64>;
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "angle_mode")] #[serde(tag = "angle_mode")]

View File

@ -65,7 +65,7 @@ impl Default for App {
} }
impl App { impl App {
fn draw_clippy_dialogs<T: std::io::Write>(&mut self, f: &mut Frame<CrosstermBackend<T>>) { fn draw_clippy_dialogs<T: std::io::Write>(&mut self, f: &mut Frame<CrosstermBackend<T>>) {
match (&self.state, self.calculator.get_state()) { match (&self.state, &self.calculator.state) {
(AppState::Help, _) => { (AppState::Help, _) => {
draw_clippy_rect( draw_clippy_rect(
ClippyRectangle { ClippyRectangle {
@ -99,7 +99,8 @@ impl App {
title: "Constants", title: "Constants",
msg: self msg: self
.calculator .calculator
.get_constants_iter() .constants
.iter()
.map(|(key, constant)| { .map(|(key, constant)| {
format!("{}: {} ({})", key, constant.help, constant.value) format!("{}: {} ({})", key, constant.help, constant.value)
}) })
@ -119,7 +120,8 @@ impl App {
title, title,
msg: self msg: self
.calculator .calculator
.get_registers_iter() .registers
.iter()
.map(|(key, value)| format!("{}: {}", key, value)) .map(|(key, value)| format!("{}: {}", key, value))
.fold(String::new(), |acc, s| acc + &s + "\n") .fold(String::new(), |acc, s| acc + &s + "\n")
.trim_end(), .trim_end(),
@ -133,7 +135,8 @@ impl App {
title: "Macros", title: "Macros",
msg: self msg: self
.calculator .calculator
.get_macros_iter() .macros
.iter()
.map(|(key, mac)| format!("{}: {}", key, mac.help)) .map(|(key, mac)| format!("{}: {}", key, mac.help))
.fold(String::new(), |acc, s| acc + &s + "\n") .fold(String::new(), |acc, s| acc + &s + "\n")
.trim_end(), .trim_end(),
@ -194,7 +197,7 @@ impl App {
Span::styled("q", Style::default().add_modifier(Modifier::BOLD)), Span::styled("q", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to exit, "), Span::raw(" to exit, "),
Span::styled("h", Style::default().add_modifier(Modifier::BOLD)), Span::styled("h", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(format!(" for help - {}", self.calculator.get_status_line(),)), Span::raw(format!(" for help - {}", self.calculator.get_status_line())),
] ]
} }
(None, _) => vec![ (None, _) => vec![
@ -210,22 +213,22 @@ impl App {
// Stack // Stack
let mut stack: Vec<ListItem> = self let mut stack: Vec<ListItem> = self
.calculator .calculator
.get_stack() .stack
.iter() .iter()
.take(chunks[1].height as usize - 2) .take(chunks[1].height as usize - 2)
.enumerate() .enumerate()
.rev() .rev()
.map(|(i, m)| { .map(|(i, m)| {
let number = match self.calculator.get_display_mode() { let number = match self.calculator.display_mode {
CalculatorDisplayMode::Default => format!("{}", m), CalculatorDisplayMode::Default => format!("{}", m),
CalculatorDisplayMode::Separated { separator } => { CalculatorDisplayMode::Separated { separator } => {
format::separated(*m, *separator) format::separated(*m, separator)
} }
CalculatorDisplayMode::Scientific { precision } => { CalculatorDisplayMode::Scientific { precision } => {
format::scientific(*m, *precision) format::scientific(*m, precision)
} }
CalculatorDisplayMode::Engineering { precision } => { CalculatorDisplayMode::Engineering { precision } => {
format::engineering(*m, *precision) format::engineering(*m, precision)
} }
CalculatorDisplayMode::Fixed { precision } => { CalculatorDisplayMode::Fixed { precision } => {
format!("{:0>.precision$}", m, precision = precision) format!("{:0>.precision$}", m, precision = precision)
@ -303,7 +306,7 @@ fn main() -> Result<(), Box<dyn Error>> {
} }
fn handle_key(app: &mut App, key: KeyEvent) -> CalculatorResult<CalculatorResponse> { fn handle_key(app: &mut App, key: KeyEvent) -> CalculatorResult<CalculatorResponse> {
match (&app.state, app.calculator.get_state()) { match (&app.state, &app.calculator.state) {
(AppState::Calculator, CalculatorState::Normal) => match key { (AppState::Calculator, CalculatorState::Normal) => match key {
KeyEvent { KeyEvent {
code: KeyCode::Char('q'), code: KeyCode::Char('q'),