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

View File

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

View File

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