Skip to content

Commit

Permalink
Start implementing operators
Browse files Browse the repository at this point in the history
  • Loading branch information
c-git committed Jan 23, 2024
1 parent 48ee524 commit 3f28925
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// We derive Deserialize/Serialize so we can persist app state on shutdown.
#[derive(serde::Deserialize, serde::Serialize)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
#[derive(Default)]
#[derive(Debug, Default)]
pub struct CalculatorApp {
value: Option<f64>,
answer: Option<f64>,
last_operation: Option<Operator>,
error_message: Option<String>,
}

#[derive(serde::Deserialize, serde::Serialize)]
#[derive(serde::Deserialize, serde::Serialize, Clone, Copy, Debug)]
enum Operator {
Add,
Subtract,
Expand Down Expand Up @@ -43,18 +43,41 @@ impl CalculatorApp {
}

fn click_operator(&mut self, operator: Operator) {
match operator {
Operator::Add => todo!(),
Operator::Subtract => todo!(),
Operator::Multiply => todo!(),
Operator::Divide => todo!(),
Operator::Equal => todo!(),
type OP = Operator;
match (self.answer, self.value, self.last_operation) {
(None, None, _) => (),
(None, Some(_), None) => {
self.last_operation = Some(operator);
self.answer = self.value;
self.value = None;
}
(None, Some(_), Some(_)) => {
dbg!(&self, operator);
self.error_message = Some("Err: Unreachable".to_owned());
}
(Some(_), None, None) => {
dbg!(&self, operator);
self.error_message = Some("Err: Unreachable".to_owned());
}
(Some(_), None, Some(_)) => self.last_operation = Some(operator),
(Some(_), Some(_), None) => {
dbg!(&self, operator);
self.error_message = Some("Err: Unreachable".to_owned());
}
(Some(_), Some(_), Some(_)) => match operator {
OP::Add => todo!(),
OP::Subtract => todo!(),
OP::Multiply => todo!(),
OP::Divide => todo!(),
OP::Equal => todo!(),
},
}
}
}

impl eframe::App for CalculatorApp {
/// Called by the frame work to save state before shutdown.
fn save(&mut self, storage: &mut dyn eframe::Storage) {
eframe::set_value(storage, eframe::APP_KEY, self);
}

Expand Down

0 comments on commit 3f28925

Please sign in to comment.