From f07ba9ed3881f9cd44a919ee2e6478f2a705d920 Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 1 Apr 2023 18:27:47 -0700 Subject: [PATCH] Minor fixes --- src/quantity/quantity.rs | 8 ++++---- src/quantity/rationalq.rs | 8 ++++++-- src/tokens.rs | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/quantity/quantity.rs b/src/quantity/quantity.rs index c56be24..cb48335 100644 --- a/src/quantity/quantity.rs +++ b/src/quantity/quantity.rs @@ -116,10 +116,10 @@ impl Quantity { } } - pub fn new_rational_from_string(s: &str) -> Quantity { - return Quantity::Rational { - v: RationalQ::from_string(s) - } + pub fn new_rational_from_string(s: &str) -> Option { + let r = RationalQ::from_string(s); + if r.is_none() { return None; } + return Some(Quantity::Rational { v: r.unwrap() }); } pub fn new_rational_from_f64(f: f64) -> diff --git a/src/quantity/rationalq.rs b/src/quantity/rationalq.rs index 01d789a..5916224 100644 --- a/src/quantity/rationalq.rs +++ b/src/quantity/rationalq.rs @@ -63,9 +63,13 @@ impl RationalQ { return Some(RationalQ{ val: v.unwrap() }); } - pub fn from_string(s: &str) -> RationalQ { + pub fn from_string(s: &str) -> Option { let v = Rational::from_str_radix(s, 10); - return RationalQ{ val: v.unwrap() }; + let v = match v { + Ok(x) => x, + Err(_) => return None + }; + return Some(RationalQ{ val: v }); } pub fn to_float(&self) -> Float { diff --git a/src/tokens.rs b/src/tokens.rs index b024c6e..f97a482 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -572,7 +572,7 @@ impl Operator{ if let Token::Number(v) = args { if !v.fract().is_zero() { return Err(()); } - if v >= Quantity::new_rational(100, 1) { return Err(()); } + if v > Quantity::new_rational(50_000, 1) { return Err(()); } let mut prod = Quantity::new_rational(1, 1); let mut u = v.clone();