smartcalc/
tools.rs

1/*
2 * smartcalc v1.0.8
3 * Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
4 * Licensed under the GNU General Public License v2.0.
5 */
6
7use alloc::string::{ToString, String};
8use crate::config::SmartCalcConfig;
9 
10pub fn do_divition(left: f64, right: f64) -> f64 {
11    let mut calculation = left / right;
12    if calculation.is_infinite() || calculation.is_nan() {
13        calculation = 0.0;
14    }
15    calculation
16}
17
18pub fn parse_timezone<'t>(config: &SmartCalcConfig, capture: &regex::Captures<'t>) -> Option<(String, i32)> {
19    match capture.name("timezone_1") {
20        Some(tz) => {
21            let timezone = tz.as_str().to_uppercase();
22            config.timezones.get(&timezone).map(|offset| (timezone, *offset))
23        },
24        None => match capture.name("timezone_2") {
25            Some(tz) => {
26                let hour = match capture.name("timezone_hour")?.as_str().parse::<i32>() {
27                    Ok(hour) => hour,
28                    Err(_) => return None
29                };
30                let minute   = match capture.name("timezone_minute") {
31                    Some(minute) => {
32                        match minute.as_str().parse::<i32>() {
33                            Ok(minute) => minute,
34                            Err(_) => return None
35                        }
36                    },
37                    _ => 0
38                };
39
40                let timezone_type = match capture.name("timezone_type") {
41                    Some(timezone_type) => match timezone_type.as_str() {
42                        "-" => -1,
43                        _ => 1
44                    },
45                    None => 1
46                };
47
48                Some((tz.as_str().to_string(), (hour * 60 + minute) * timezone_type))
49            },
50            None => None
51        }
52    }
53}