From a8f574bd80d0dca36ea90df29fe2314435c51479 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Wed, 28 Apr 2021 21:07:45 -0400 Subject: [PATCH] Continue work --- src/calc.rs | 72 ++++++++++++++++++++++------------------------- src/main.rs | 4 +-- src/util/event.rs | 11 +------- 3 files changed, 37 insertions(+), 50 deletions(-) diff --git a/src/calc.rs b/src/calc.rs index d6d24d6..570dc49 100644 --- a/src/calc.rs +++ b/src/calc.rs @@ -146,9 +146,7 @@ impl<'a> Calculator<'a> { self.state = CalculatorState::WaitingForRegister(RegisterState::Save); Ok(()) } - _ => { - return Err(CalculatorError::NoSuchOperator(c)); - } + _ => Err(CalculatorError::NoSuchOperator(c)), }, CalculatorState::WaitingForConstant => { let f = self @@ -176,9 +174,9 @@ impl<'a> Calculator<'a> { // Record that we are running macro c self.active_macros.insert(c); for c in value.chars() { - self.take_input(c).or_else(|e| { + self.take_input(c).map_err(|e| { self.cancel(); - Err(e) + e })?; } // Macro c should be over now @@ -207,8 +205,6 @@ impl<'a> Calculator<'a> { Ok(()) } } - //} - //Ok(()) } pub fn cancel(&mut self) { @@ -220,7 +216,7 @@ impl<'a> Calculator<'a> { Ok(()) } pub fn edit(&mut self) -> CalculatorResult<()> { - if self.l.len() > 0 { + if !self.l.is_empty() { return Ok(()); } @@ -275,15 +271,15 @@ impl<'a> Calculator<'a> { self.registers.iter() } - pub fn push_constant(&mut self, key: char) -> CalculatorResult<()> { - match self.constants.get(&key) { - Some(CalculatorConstant { value, .. }) => { - let value = *value; - self.push(value) - } - None => Err(CalculatorError::NoSuchConstant(key)), - } - } + // pub fn push_constant(&mut self, key: char) -> CalculatorResult<()> { + // match self.constants.get(&key) { + // Some(CalculatorConstant { value, .. }) => { + // let value = *value; + // self.push(value) + // } + // None => Err(CalculatorError::NoSuchConstant(key)), + // } + // } // pub fn push_register(&mut self, key: char) -> CalculatorResult<()> { // match self.registers.get(&key) { // Some(f) => { @@ -298,12 +294,12 @@ impl<'a> Calculator<'a> { // self.registers.insert(key, f); // Ok(()) // } - pub fn get_macro(&mut self, key: char) -> Result<&CalculatorMacro<'a>, CalculatorError> { - match self.macros.get(&key) { - Some(m) => Ok(m), - None => Err(CalculatorError::NoSuchMacro(key)), - } - } + // pub fn get_macro(&mut self, key: char) -> Result<&CalculatorMacro<'a>, CalculatorError> { + // match self.macros.get(&key) { + // Some(m) => Ok(m), + // None => Err(CalculatorError::NoSuchMacro(key)), + // } + // } pub fn flush_l(&mut self) -> CalculatorResult { if self.l.is_empty() { @@ -315,13 +311,13 @@ impl<'a> Calculator<'a> { Ok(true) } } - pub fn push(&mut self, f: f64) -> CalculatorResult<()> { + fn push(&mut self, f: f64) -> CalculatorResult<()> { self.direct_state_change(CalculatorStateChange { pop: OpArgs::None, push: OpArgs::Unary(f), }) } - pub fn pop(&mut self) -> CalculatorResult { + fn pop(&mut self) -> CalculatorResult { let f = self.checked_get(0)?; self.direct_state_change(CalculatorStateChange { pop: OpArgs::Unary(f), @@ -332,7 +328,6 @@ impl<'a> Calculator<'a> { pub fn get_stack(&self) -> &VecDeque { &self.stack } - //TODO: VecDeque could have other types pub fn op(&mut self, op: CalculatorOperation) -> CalculatorResult<()> { // Dup is special -- don't actually run it if l needs to be flushed if self.flush_l()? { @@ -384,7 +379,7 @@ impl<'a> Calculator<'a> { .ok_or_else(|| CalculatorError::EmptyHistory(String::from("redo")))?; return self.apply_state_change(s, true); } - // TODO: This should not happen -- need to pull all macro/register accesses into their own enum + // Macros are a no-op as an operator CalculatorOperation::Macro(_) => { return Ok(()); } @@ -466,13 +461,15 @@ impl<'a> Calculator<'a> { self.stack.pop_front(); } OpArgs::Binary([a, b]) => { - if forward { - self.stack_eq(0, *a)?; - self.stack_eq(1, *b)?; - } else { - self.stack_eq(0, *a)?; - self.stack_eq(1, *b)?; - } + self.stack_eq(0, *a)?; + self.stack_eq(1, *b)?; + // if forward { + // self.stack_eq(0, *a)?; + // self.stack_eq(1, *b)?; + // } else { + // self.stack_eq(0, *a)?; + // self.stack_eq(1, *b)?; + // } self.stack.pop_front(); self.stack.pop_front(); } @@ -513,9 +510,8 @@ impl<'a> Calculator<'a> { } fn checked_get(&self, idx: usize) -> CalculatorResult { - match self.stack.get(idx) { - None => Err(CalculatorError::NotEnoughStackEntries), - Some(r) => Ok(*r), - } + self.stack + .get(idx) + .ok_or(Err(CalculatorError::NotEnoughStackEntries)) } } diff --git a/src/main.rs b/src/main.rs index ea1a717..6e55f9f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -182,7 +182,7 @@ fn main() -> Result<(), Box> { }; draw_clippy_rect( ClippyRectangle { - title: title, + title, msg: app .calculator .get_registers_iter() @@ -285,7 +285,7 @@ fn handle_key(app: &mut App, events: &Events, key: Key) -> CalculatorResult {} }, } - return Ok(false); + Ok(false) } struct ClippyRectangle<'a> { diff --git a/src/util/event.rs b/src/util/event.rs index d032ae3..5d284cf 100644 --- a/src/util/event.rs +++ b/src/util/event.rs @@ -87,16 +87,6 @@ impl Events { } } - pub fn fill_event_buf(&self, mac: &str) { - for c in mac.chars() { - // TODO: Catch errors - if let Err(_) = self.tx.send(Event::Input(Key::Char(c))) { - //return; - } - } - //self.tx.send(Event::MacroEnd); - } - pub fn next(&self) -> Result, mpsc::RecvError> { self.rx.recv() } @@ -104,6 +94,7 @@ impl Events { pub fn try_next(&self) -> Result, mpsc::TryRecvError> { self.rx.try_recv() } + pub fn try_iter(&self) -> TryIter> { self.rx.try_iter() }