mirror of https://github.com/rm-dr/daisy
Added unit fromstring
parent
e9645208d9
commit
a38293c247
|
@ -12,7 +12,8 @@ use crate::parser::groupify::groupify;
|
|||
use crate::parser::treeify::treeify;
|
||||
use crate::parser::find_subs::find_subs;
|
||||
|
||||
use crate::quantity::{Quantity, BaseUnit};
|
||||
use crate::quantity::Quantity;
|
||||
use crate::quantity::Unit;
|
||||
|
||||
use crate::tokens::Token;
|
||||
|
||||
|
@ -89,28 +90,35 @@ impl PreToken {
|
|||
},
|
||||
|
||||
PreToken::PreWord(l, s) => {
|
||||
return Ok(match &s[..] {
|
||||
let c = match &s[..] {
|
||||
// Mathematical constants
|
||||
// 100 digits of each.
|
||||
"π"|"pi" => { Token::Constant(Quantity::new_float_from_string("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067").unwrap(), String::from("π")) },
|
||||
"e" => { Token::Constant(Quantity::new_float_from_string("2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427").unwrap(), String::from("e")) },
|
||||
"phi"|"φ" => { Token::Constant(Quantity::new_float_from_string("1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137").unwrap(), String::from("φ")) },
|
||||
"π"|"pi" => { Some(Token::Constant(Quantity::new_float_from_string(
|
||||
"3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067"
|
||||
).unwrap(), String::from("π")))},
|
||||
|
||||
// Units
|
||||
"m" => {
|
||||
let mut u = Quantity::new_rational(1f64).unwrap();
|
||||
u.add_unit(BaseUnit::Meter, 1f64);
|
||||
Token::Number(u)
|
||||
},
|
||||
"e" => { Some(Token::Constant(Quantity::new_float_from_string(
|
||||
"2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427"
|
||||
).unwrap(), String::from("e"))) },
|
||||
|
||||
"s" => {
|
||||
let mut u = Quantity::new_rational(1f64).unwrap();
|
||||
u.add_unit(BaseUnit::Second, 1f64);
|
||||
Token::Number(u)
|
||||
"phi"|"φ" => { Some(Token::Constant(Quantity::new_float_from_string(
|
||||
"1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137"
|
||||
).unwrap(), String::from("φ"))) },
|
||||
|
||||
_ => { None }
|
||||
};
|
||||
|
||||
if c.is_some() { return Ok(c.unwrap()); }
|
||||
|
||||
let c = Unit::from_string(&s);
|
||||
|
||||
if c.is_some() {
|
||||
let mut q = Quantity::new_rational(1f64).unwrap();
|
||||
q.set_unit(c.unwrap());
|
||||
return Ok(Token::Number(q));
|
||||
}
|
||||
|
||||
_ => { return Err((l, ParserError::Undefined(s))); }
|
||||
});
|
||||
return Err((l, ParserError::Undefined(s)));
|
||||
}
|
||||
|
||||
PreToken::Container(v) => { return Ok(v); }
|
||||
|
|
|
@ -95,6 +95,22 @@ impl Unit {
|
|||
return u;
|
||||
}
|
||||
|
||||
pub fn from_string(s: &str) -> Option<Unit> {
|
||||
let b = match s {
|
||||
"m" => BaseUnit::Meter,
|
||||
"s" => BaseUnit::Second,
|
||||
"kg" => BaseUnit::Kilogram,
|
||||
"a" => BaseUnit::Ampere,
|
||||
"k" => BaseUnit::Kelvin,
|
||||
"mol" => BaseUnit::Mole,
|
||||
"c" => BaseUnit::Candela,
|
||||
_ => { return None; }
|
||||
};
|
||||
|
||||
let mut u = Unit::new();
|
||||
u.insert(b, 1f64);
|
||||
return Some(u);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue