Skip to content

Commit

Permalink
Clean up some unwrap() code.
Browse files Browse the repository at this point in the history
  • Loading branch information
shartrec committed Jul 2, 2024
1 parent 4d4c8c5 commit 82a9b48
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 39 deletions.
64 changes: 36 additions & 28 deletions src/conversions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ use strum_macros::{Display, EnumIter};

use crate::conversions::System::Metric;

pub(crate) mod mass;
pub(crate) mod length;
pub(crate) mod area;
pub(crate) mod temperature;
pub(crate) mod volume;
mod mass;
mod length;
mod area;
mod temperature;
mod volume;
mod power;
mod torque;
mod force;
Expand Down Expand Up @@ -152,32 +152,40 @@ impl PartialEq for Unit {
}
}

pub(crate) fn convert(value: &f64, from: &Unit, to: &Unit) -> f64 {
if from == to {
return value.clone()
pub(crate) fn try_convert(value: &f64, from: &Option<&Unit>, to: &Option<&Unit>) -> f64 {
if let (Some(from), Some(to)) = (from, to) {
convert(value, from, to)
} else {
// Doing a conversion with no unit set
warn!("Doing a conversion, but units not set");
0.0
}

// Let's get the functions we need to convert the value to and from the base unit
// Funtions may be None if the unit is the base
let (to_base, from_base) =

// We need to see if these are of the same system and have a system base unit.
if from.system == to.system && from.dimension == to.dimension && !from.system.is_default() {
(&from.to_system_base, &to.from_system_base)
} else {
(&from.to_base, &to.from_base)
}
pub(crate) fn convert(value: &f64, from: &Unit, to: &Unit) -> f64 {
if from == to {
return value.clone()
}
;

let mut result = *value;
if let Some(f) = to_base {
result = f(result);
}
if let Some(f) = from_base {
result = f(result);
}
result

// Let's get the functions we need to convert the value to and from the base unit
// Funtions may be None if the unit is the base
let (to_base, from_base) =

// We need to see if these are of the same system and have a system base unit.
if from.system == to.system && from.dimension == to.dimension && !from.system.is_default() {
(&from.to_system_base, &to.from_system_base)
} else {
(&from.to_base, &to.from_base)
}
;

let mut result = *value;
if let Some(f) = to_base {
result = f(result);
}
if let Some(f) = from_base {
result = f(result);
}
result
}

pub(crate) fn get_units(dimension: &Option<Dimension>) -> Vec<&'static Unit> {
Expand Down
6 changes: 5 additions & 1 deletion src/evaluator/tokeniser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*
*/

use log::warn;
use crate::evaluator::{Evaluator, Token};

pub(crate) fn tokenize(expression: &str, evaluator: &Evaluator) -> Result<Vec<Token>, String> {
Expand All @@ -35,7 +36,10 @@ pub(crate) fn tokenize(expression: &str, evaluator: &Evaluator) -> Result<Vec<To
num_str.push(chars[i]);
i += 1;
}
let number = num_str.parse::<f64>().unwrap();
let number = num_str.parse::<f64>().unwrap_or_else(|e| {
warn!("{}", e);
0.0
});
tokens.push(Token::Number(number));
continue; // Skip the increment below because it's already done
}
Expand Down
31 changes: 21 additions & 10 deletions src/ui/calc_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ use iced::widget::{Button, button, Column, container, Container, horizontal_rule
use iced::widget::button::Status;
use iced::widget::text_editor::{Action, Content, Edit, Motion};
use iced::window::Id;
use log::warn;
use palette::{convert::FromColor, Hsl};
use palette::rgb::Rgb;

use crate::conversions::{convert, length::METRE, Unit};
use crate::conversions::{try_convert, Unit};
use crate::evaluator::AngleMode;
use crate::ui::calculator::Calc;
use crate::ui::messages::Message;
Expand Down Expand Up @@ -271,18 +272,28 @@ impl CalcWindow {
if !self.is_converting {
Column::with_children([con_mode, lcd, result]).spacing(2)
} else {
let conv_from = text(self.convert_from.as_ref().unwrap_or(&METRE).name)
.horizontal_alignment(Horizontal::Left)
.into();
let conv_to = text(self.convert_to.as_ref().unwrap_or(&METRE).name)
.horizontal_alignment(Horizontal::Left)
.into();

let converted_result = text(match &self.result {

let conv_from = if let Some(unit_from) = &self.convert_from {
text(unit_from.name)

} else {
warn!("Converting units, but no 'from' unit set");
text("")
}.horizontal_alignment(Horizontal::Left).into();

let conv_to = if let Some(unit_to) = &self.convert_to {
text(unit_to.name)

} else {
warn!("Converting units, but no 'to' unit set");
text("")
}.horizontal_alignment(Horizontal::Left)
.into();
let converted_result = text(match &self.result {
Some(r) => {
match r {
Ok(v) => {
let cv = convert(v, &self.convert_from.as_ref().unwrap_or(&METRE), &self.convert_to.as_ref().unwrap_or(&METRE));
let cv = try_convert(v, &self.convert_from.as_ref(), &self.convert_to.as_ref());
Self::format_result(&cv)
}
Err(e) => e.clone()
Expand Down

0 comments on commit 82a9b48

Please sign in to comment.