Added no_space parameter for units

pull/2/head
Mark 2023-06-13 09:13:24 -07:00
parent 088853014e
commit a6bcbd5f66
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 53 additions and 10 deletions

View File

@ -31,6 +31,7 @@ fn write_wholeunit_main(mut file: &File, units: &Vec<Value>) {
writeln!(file, "}}\n").unwrap();
// ToString
writeln!(file,
concat!(
"impl ToString for WholeUnit {{\n",
@ -47,7 +48,30 @@ fn write_wholeunit_main(mut file: &File, units: &Vec<Value>) {
).unwrap();
}
writeln!(file, "\t\t}})\n\t}}\n}}").unwrap();
writeln!(file, "\t\t}})\n\t}}\n}}\n").unwrap();
// Properties
writeln!(file,
concat!(
"impl WholeUnit {{\n",
"\tfn no_space(&self) -> bool {{\n",
"\t\tmatch self {{"
)
).unwrap();
for u in units {
if u.as_table().unwrap().contains_key("no_space") {
if u.as_table().unwrap()["no_space"].as_bool().unwrap() {
writeln!(file,
"\t\t\tWholeUnit::{} => true,",
u["enum_name"].as_str().unwrap()
).unwrap();
}
}
}
writeln!(file, "\t\t\t_ => false\n\t\t}}\n\t}}\n}}").unwrap();
}
@ -223,9 +247,6 @@ fn write_freeunit_from_string(mut file: &File, units: &Vec<Value>) {
fn main() -> Result<(), ()>{
let out_dir = env::var_os("OUT_DIR").unwrap();

View File

@ -318,10 +318,18 @@ impl Operator {
};
if no_times {
return format!("{} {}",
self.add_parens_to_arg_strict(a),
self.add_parens_to_arg_strict(b)
);
let Token::Quantity(u) = b else {panic!()};
if u.unit.no_space() {
return format!("{}{}",
self.add_parens_to_arg_strict(a),
self.add_parens_to_arg_strict(b)
);
} else {
return format!("{} {}",
self.add_parens_to_arg_strict(a),
self.add_parens_to_arg_strict(b)
);
}
} else {
return format!("{} × {}",
self.add_parens_to_arg_strict(a),

View File

@ -29,7 +29,11 @@ impl ToString for Quantity {
let u = self.unit.to_string();
if self.is_one() { return u; };
return format!("{n} {u}");
if self.unit.no_space() {
return format!("{n}{u}");
} else {
return format!("{n} {u}");
}
}
}
@ -39,7 +43,11 @@ impl Quantity {
if self.unitless() { return n; }
let u = self.unit.to_string();
return format!("{n} {u}");
if self.unit.no_space() {
return format!("{n}{u}");
} else {
return format!("{n} {u}");
}
}
pub fn new_float(f: f64) -> Option<Quantity> {

View File

@ -86,6 +86,12 @@ impl Unit {
pub fn get_val_mut(&mut self) -> &mut HashMap<FreeUnit, Scalar> { &mut self.val }
pub fn unitless(&self) -> bool { self.get_val().len() == 0 }
pub fn no_space(&self) -> bool {
if self.get_val().len() == 1 {
return self.get_val().keys().next().unwrap().whole.no_space();
} else { return false; }
}
pub fn from_array(a: &[(FreeUnit, Scalar)]) -> Unit {
let mut n = Unit::new();
for (u, p) in a.iter() {