diff --git a/README.md b/README.md index 8dd6b9f..fbd3f81 100644 --- a/README.md +++ b/README.md @@ -51,8 +51,9 @@ cargo install dioxus-cli ## Project Structure - `src/main.rs`: Entry point. -- `src/ui.rs`: Main calculator component and logic. +- `src/ui.rs`: Main calculator UI and logic calls. - `assets/main.css`: Styling and themes. +- `src/logic.rs`: Calculator logic. ## License diff --git a/src/logic.rs b/src/logic.rs new file mode 100644 index 0000000..c898d9e --- /dev/null +++ b/src/logic.rs @@ -0,0 +1,85 @@ +use dioxus::prelude::*; + +/// Helper function to add a digit to the current number +pub fn input_digit( + mut first_num: Signal, + mut second_num: Signal, + operator: Signal, + digit: &str, +) { + if operator().is_empty() { + first_num.write().push_str(digit); + } else { + second_num.write().push_str(digit); + } +} + +/// Helper logic for operators +pub fn handle_operator( + first_num: Signal, + second_num: Signal, + mut operator: Signal, + op: &str, +) { + if !first_num().is_empty() && second_num().is_empty() { + operator.set(op.to_string()); + } else if !second_num().is_empty() { + // If we already have two numbers, calculate first, then set new operator + calculate_result(first_num, second_num, operator); + operator.set(op.to_string()); + } +} + +/// Performs the calculation based on current state +pub fn calculate_result( + mut first_num: Signal, + mut second_num: Signal, + mut operator: Signal, +) { + // Parse numbers + let num1_str = first_num(); + let num2_str = second_num(); + let op = operator(); + + // If we don't have a second number, we can't calculate + if num2_str.is_empty() { + return; + } + + // Try to parse strings to floats + let n1 = match num1_str.parse::() { + Ok(n) => n, + Err(_) => return, // Invalid number + }; + + let n2 = match num2_str.parse::() { + Ok(n) => n, + Err(_) => return, // Invalid number + }; + + // Calculate result + let result = match op.as_str() { + "+" => (n1 + n2).to_string(), + "-" => (n1 - n2).to_string(), + "*" => (n1 * n2).to_string(), + "/" => { + if n2 == 0.0 { + "Error".to_string() + } else { + (n1 / n2).to_string() + } + } + _ => return, // Unknown operator + }; + + // Update state with result + if result == "Error" { + first_num.set(String::new()); + } else { + first_num.set(result); + } + + // Reset others + second_num.set(String::new()); + operator.set(String::new()); +} diff --git a/src/main.rs b/src/main.rs index 2be9323..a39020e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use dioxus::prelude::*; +mod logic; mod ui; const MAIN_CSS: Asset = asset!("/assets/main.css"); diff --git a/src/ui.rs b/src/ui.rs index 002e076..eb067a3 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,3 +1,4 @@ +use crate::logic::{calculate_result, handle_operator, input_digit}; use dioxus::prelude::*; /// The main calculator component. @@ -133,87 +134,3 @@ pub fn Calculator() -> Element { } } } - -/// Helper function to add a digit to the current number -fn input_digit( - mut first_num: Signal, - mut second_num: Signal, - operator: Signal, - digit: &str, -) { - if operator().is_empty() { - first_num.write().push_str(digit); - } else { - second_num.write().push_str(digit); - } -} - -/// Helper logic for operators -fn handle_operator( - first_num: Signal, - second_num: Signal, - mut operator: Signal, - op: &str, -) { - if !first_num().is_empty() && second_num().is_empty() { - operator.set(op.to_string()); - } else if !second_num().is_empty() { - // If we already have two numbers, calculate first, then set new operator - calculate_result(first_num, second_num, operator); - operator.set(op.to_string()); - } -} - -/// Performs the calculation based on current state -fn calculate_result( - mut first_num: Signal, - mut second_num: Signal, - mut operator: Signal, -) { - // Parse numbers - let num1_str = first_num(); - let num2_str = second_num(); - let op = operator(); - - // If we don't have a second number, we can't calculate - if num2_str.is_empty() { - return; - } - - // Try to parse strings to floats - let n1 = match num1_str.parse::() { - Ok(n) => n, - Err(_) => return, // Invalid number - }; - - let n2 = match num2_str.parse::() { - Ok(n) => n, - Err(_) => return, // Invalid number - }; - - // Calculate result - let result = match op.as_str() { - "+" => (n1 + n2).to_string(), - "-" => (n1 - n2).to_string(), - "*" => (n1 * n2).to_string(), - "/" => { - if n2 == 0.0 { - "Error".to_string() - } else { - (n1 / n2).to_string() - } - } - _ => return, // Unknown operator - }; - - // Update state with result - if result == "Error" { - first_num.set(String::new()); - } else { - first_num.set(result); - } - - // Reset others - second_num.set(String::new()); - operator.set(String::new()); -}