mirror of
https://github.com/rm-dr/daisy
synced 2025-10-20 00:54:28 -07:00
Added build script to generate units
This commit is contained in:
@ -4,8 +4,6 @@ use crate::quantity::Scalar;
|
||||
use crate::quantity::Quantity;
|
||||
use super::WholeUnit;
|
||||
use super::Prefix;
|
||||
use super::Unit;
|
||||
use super::unit_db;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -18,88 +16,16 @@ pub struct FreeUnit {
|
||||
}
|
||||
|
||||
|
||||
macro_rules! unpack_string {
|
||||
(
|
||||
$u:expr, $s:expr,
|
||||
$( $_:expr ),*
|
||||
) => { $s };
|
||||
}
|
||||
|
||||
impl ToString for FreeUnit {
|
||||
fn to_string(&self) -> String {
|
||||
|
||||
let s = unit_db!(self.whole, unpack_string);
|
||||
let s = self.whole.to_string();
|
||||
let p = self.prefix.to_string();
|
||||
|
||||
format!("{p}{s}")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
macro_rules! unpack_base_factor {
|
||||
(
|
||||
$unit:expr,
|
||||
$display_string:expr,
|
||||
base
|
||||
) => { None };
|
||||
|
||||
(
|
||||
$unit:expr,
|
||||
$display_string:expr,
|
||||
float,
|
||||
$value:expr,
|
||||
$( ($u:expr, $p:expr) ),*
|
||||
) => {
|
||||
Some(Quantity {
|
||||
scalar: Scalar::new_float_from_string($value).unwrap(),
|
||||
unit: Unit::from_array(&[
|
||||
$(
|
||||
(FreeUnit::from_whole($u), Scalar::new_rational($p).unwrap()),
|
||||
)*
|
||||
(FreeUnit::from_whole($unit), Scalar::new_rational(-1f64).unwrap())
|
||||
])
|
||||
})
|
||||
};
|
||||
|
||||
(
|
||||
$unit:expr,
|
||||
$display_string:expr,
|
||||
rational,
|
||||
$value:expr,
|
||||
$( ($u:expr, $p:expr) ),*
|
||||
) => {
|
||||
Some(Quantity {
|
||||
scalar: Scalar::new_rational_from_string($value).unwrap(),
|
||||
unit: Unit::from_array(&[
|
||||
$(
|
||||
(FreeUnit::from_whole($u), Scalar::new_rational($p).unwrap()),
|
||||
)*
|
||||
(FreeUnit::from_whole($unit), Scalar::new_rational(-1f64).unwrap())
|
||||
])
|
||||
})
|
||||
};
|
||||
|
||||
(
|
||||
$unit:expr,
|
||||
$display_string:expr,
|
||||
rational_frac,
|
||||
($t:expr, $b:expr),
|
||||
$( ($u:expr, $p:expr) ),*
|
||||
) => {
|
||||
Some(Quantity {
|
||||
scalar: Scalar::new_rational_from_frac($t, $b).unwrap(),
|
||||
unit: Unit::from_array(&[
|
||||
$(
|
||||
(FreeUnit::from_whole($u), Scalar::new_rational($p).unwrap()),
|
||||
)*
|
||||
(FreeUnit::from_whole($unit), Scalar::new_rational(-1f64).unwrap())
|
||||
])
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
impl FreeUnit {
|
||||
pub fn from_whole(whole: WholeUnit) -> FreeUnit {
|
||||
return FreeUnit { whole, prefix: Prefix::None }
|
||||
@ -113,7 +39,7 @@ impl FreeUnit {
|
||||
/// gives a quantity in base units.
|
||||
pub fn to_base_factor(&self) -> Quantity {
|
||||
|
||||
let q = unit_db!(self.whole, unpack_base_factor);
|
||||
let q = self.whole.base_factor();
|
||||
let mut q = q.unwrap_or(Quantity::new_rational_from_string("1").unwrap());
|
||||
|
||||
let mut p = self.prefix.to_ratio();
|
||||
@ -126,7 +52,7 @@ impl FreeUnit {
|
||||
|
||||
// Get this unit in terms of base units
|
||||
pub fn get_base(&self) -> Quantity {
|
||||
let q = unit_db!(self.whole, unpack_base_factor);
|
||||
let q = self.whole.base_factor();
|
||||
let mut q = q.unwrap_or(Quantity::new_rational_from_string("1").unwrap());
|
||||
|
||||
// Don't divide by self
|
||||
|
Reference in New Issue
Block a user