updated code structure
This commit is contained in:
@@ -51,8 +51,9 @@ cargo install dioxus-cli
|
|||||||
## Project Structure
|
## Project Structure
|
||||||
|
|
||||||
- `src/main.rs`: Entry point.
|
- `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.
|
- `assets/main.css`: Styling and themes.
|
||||||
|
- `src/logic.rs`: Calculator logic.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
85
src/logic.rs
Normal file
85
src/logic.rs
Normal file
@@ -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<String>,
|
||||||
|
mut second_num: Signal<String>,
|
||||||
|
operator: Signal<String>,
|
||||||
|
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<String>,
|
||||||
|
second_num: Signal<String>,
|
||||||
|
mut operator: Signal<String>,
|
||||||
|
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<String>,
|
||||||
|
mut second_num: Signal<String>,
|
||||||
|
mut operator: Signal<String>,
|
||||||
|
) {
|
||||||
|
// 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::<f64>() {
|
||||||
|
Ok(n) => n,
|
||||||
|
Err(_) => return, // Invalid number
|
||||||
|
};
|
||||||
|
|
||||||
|
let n2 = match num2_str.parse::<f64>() {
|
||||||
|
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());
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
|
mod logic;
|
||||||
mod ui;
|
mod ui;
|
||||||
|
|
||||||
const MAIN_CSS: Asset = asset!("/assets/main.css");
|
const MAIN_CSS: Asset = asset!("/assets/main.css");
|
||||||
|
|||||||
85
src/ui.rs
85
src/ui.rs
@@ -1,3 +1,4 @@
|
|||||||
|
use crate::logic::{calculate_result, handle_operator, input_digit};
|
||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
/// The main calculator component.
|
/// 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<String>,
|
|
||||||
mut second_num: Signal<String>,
|
|
||||||
operator: Signal<String>,
|
|
||||||
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<String>,
|
|
||||||
second_num: Signal<String>,
|
|
||||||
mut operator: Signal<String>,
|
|
||||||
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<String>,
|
|
||||||
mut second_num: Signal<String>,
|
|
||||||
mut operator: Signal<String>,
|
|
||||||
) {
|
|
||||||
// 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::<f64>() {
|
|
||||||
Ok(n) => n,
|
|
||||||
Err(_) => return, // Invalid number
|
|
||||||
};
|
|
||||||
|
|
||||||
let n2 = match num2_str.parse::<f64>() {
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user