Reworked unit prefixes

pull/2/head
Mark 2023-06-13 21:18:00 -07:00
parent 078fc12c5e
commit 3acfc7ebfd
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 358 additions and 263 deletions

View File

@ -41,7 +41,7 @@ fn write_wholeunit_main(mut file: &File, units: &Vec<Value>) {
for u in units { for u in units {
writeln!(file, writeln!(file,
"\t\t\tWholeUnit::{e} => \"{s}\",", "\t\t\tWholeUnit::{e} => \"{s}\",",
s = u["print"].as_str().unwrap(), s = u["strings"].as_array().unwrap()[0][0].as_str().unwrap(),
e = u["enum_name"].as_str().unwrap() e = u["enum_name"].as_str().unwrap()
).unwrap(); ).unwrap();
} }
@ -158,43 +158,6 @@ fn write_wholeunit_base_factor(mut file: &File, units: &Vec<Value>) {
} }
/// Write all SI prefixes.
/// Used inside freeunit_from_string().
fn prefix_si(mut file: &File, enum_name: &str, s: &str) {
writeln!(file,
concat!(
"\t\t", "\"{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::None}}),\n",
"\t\t", "\"Q{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Quetta}}),\n",
"\t\t", "\"R{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Ronna}}),\n",
"\t\t", "\"Y{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Yotta}}),\n",
"\t\t", "\"Z{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Zetta}}),\n",
"\t\t", "\"E{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Exa}}),\n",
"\t\t", "\"P{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Peta}}),\n",
"\t\t", "\"T{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Tera}}),\n",
"\t\t", "\"G{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Giga}}),\n",
"\t\t", "\"M{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Mega}}),\n",
"\t\t", "\"k{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Kilo}}),\n",
"\t\t", "\"h{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Hecto}}),\n",
"\t\t", "\"da{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Deka}}),\n",
"\t\t", "\"d{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Deci}}),\n",
"\t\t", "\"c{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Centi}}),\n",
"\t\t", "\"m{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Milli}}),\n",
"\t\t", "\"u{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Micro}}),\n",
"\t\t", "\"n{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Nano}}),\n",
"\t\t", "\"p{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Pico}}),\n",
"\t\t", "\"f{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Femto}}),\n",
"\t\t", "\"a{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Atto}}),\n",
"\t\t", "\"z{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Zepto}}),\n",
"\t\t", "\"y{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Yocto}}),\n",
"\t\t", "\"r{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Ronto}}),\n",
"\t\t", "\"q{s}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: Prefix::Quecto}}),",
),
e = enum_name,
s = s
).unwrap();
}
/// Create freeunit_from_string(). /// Create freeunit_from_string().
/// Should only be run once. /// Should only be run once.
fn write_freeunit_from_string(mut file: &File, units: &Vec<Value>) { fn write_freeunit_from_string(mut file: &File, units: &Vec<Value>) {
@ -207,33 +170,27 @@ fn write_freeunit_from_string(mut file: &File, units: &Vec<Value>) {
).unwrap(); ).unwrap();
for u in units { for u in units {
if u.as_table().unwrap().contains_key("parse") {
for s in u["parse"].as_array().unwrap() { for s in u["strings"].as_array().unwrap() {
if s.as_array().unwrap().len() == 1 {
writeln!(file, writeln!(file,
"\t\t\"{}\" => Some(FreeUnit{{whole: WholeUnit::{}, prefix: Prefix::None}}),", "\t\t\"{}\" => Some(FreeUnit{{whole: WholeUnit::{}, prefix: Prefix::None}}),",
s.as_str().unwrap(), s.as_array().unwrap()[0].as_str().unwrap(),
u["enum_name"].as_str().unwrap() u["enum_name"].as_str().unwrap()
).unwrap(); ).unwrap();
} else {
for p in &s.as_array().unwrap()[1..] {
writeln!(file,
"\t\t\"{p}{u}\" => Some(FreeUnit{{whole: WholeUnit::{e}, prefix: str_to_prefix!(\"{p}\")}}),",
u = s.as_array().unwrap()[0].as_str().unwrap(),
p = p.as_str().unwrap(),
e = u["enum_name"].as_str().unwrap()
).unwrap();
} }
} }
if u.as_table().unwrap().contains_key("parse_with_prefix") {
if u.as_table().unwrap()["parse_with_prefix"].is_array() {
for p in u["parse_with_prefix"].as_array().unwrap() {
prefix_si(
&file,
u["enum_name"].as_str().unwrap(),
p.as_str().unwrap()
);
}
} else {
prefix_si(
&file,
u["enum_name"].as_str().unwrap(),
u["parse_with_prefix"].as_str().unwrap()
);
}
} }
writeln!(file, "").unwrap(); writeln!(file, "").unwrap();

View File

@ -4,9 +4,13 @@
# #
# Basic properties: # Basic properties:
# enum_name: unique capitalized string. The name of this unit's enum element. # enum_name: unique capitalized string. The name of this unit's enum element.
# print: string. What this unit will print as. This string will NOT be parsed into this unit.
# base: if true, this is a base unit. Optional, false if omitted. # base: if true, this is a base unit. Optional, false if omitted.
# no_space: if true, don't put a space between this unit and its number. Optional, false if omitted. # no_space: if true, don't put a space between this unit and its number. Optional, false if omitted.
# strings: array of arrays of strings. Specifies what strings are interpreted as this unit.
# Each entry looks like ["s", ...], where the first item is a string and ... is an optional list of valid prefixes. See `prefix.rs`.
# Note that the empty string must be in the list of prefixes if you want the string to be available without a prefix.
# Units with no prefix must be a one-element array.
# The first string in this array will be the string we print when displaying this unit.
# #
# #
# Base units (only apply if base = false): # Base units (only apply if base = false):
@ -25,80 +29,74 @@
# u = base unit enum name # u = base unit enum name
# p = base unit power # p = base unit power
# Any unit included here MUST have `base = true`. THIS IS NOT CHECKED, THINGS WILL BREAK! # Any unit included here MUST have `base = true`. THIS IS NOT CHECKED, THINGS WILL BREAK!
#
#
# Parsing:
# parse: array of strings. These strings will be parsed into this unit.
# parse_with_prefix: string or string array. Prefixes will be generated for this string, including the null prefix.
# Strings in `parse_with_prefix` MUST NOT be in `parse`.
[[unit]] [[unit]]
enum_name = "Second" enum_name = "Second"
print = "s" strings = [
["s", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "s" ["sec"], ["second"], ["seconds"]
parse = ["sec", "second", "seconds"] ]
base = true base = true
[[unit]] [[unit]]
enum_name = "Gram" enum_name = "Gram"
print = "g" strings = [
["g", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "g" ["gram"], ["grams"], ["gramme"], ["grammes"]
parse = ["gram", "grams", "gramme", "grammes"] ]
base = true base = true
[[unit]] [[unit]]
enum_name = "Meter" enum_name = "Meter"
print = "m" strings = [
["m", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "m" ["meter"], ["meters"]
parse = ["meter", "meters"] ]
base = true base = true
[[unit]] [[unit]]
enum_name = "Ampere" enum_name = "Ampere"
print = "A" strings = [
["A", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "A" ["ampere"], ["amperes"], ["amp"]
parse = ["ampere", "amperes"] ]
base = true base = true
[[unit]] [[unit]]
enum_name = "Kelvin" enum_name = "Kelvin"
print = "K" strings = [
["K", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "K" ["Kelvin"], ["kelvin"]
parse = ["kelvin"] ]
base = true base = true
[[unit]] [[unit]]
enum_name = "Mole" enum_name = "Mole"
print = "mol" strings = [
["mol", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "mol" ["mole"]
parse = ["mole"] ]
base = true base = true
[[unit]] [[unit]]
enum_name = "Candela" enum_name = "Candela"
print = "cd" strings = [
["cd", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "cd" ["candela"]
parse = ["candela"] ]
base = true base = true
@ -109,8 +107,9 @@ base = true
[[unit]] [[unit]]
enum_name = "Minute" enum_name = "Minute"
print = "min" strings = [
parse = ["min", "minute", "minutes"] ["min"], ["minute"], ["minutes"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "60" base_value = "60"
@ -120,8 +119,9 @@ base_units = [ { u = "Second", p = 1} ]
[[unit]] [[unit]]
enum_name = "Hour" enum_name = "Hour"
print = "h" strings = [
parse = ["h", "hour", "hours"] ["h"], ["hour"], ["hours"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "3600" base_value = "3600"
@ -131,8 +131,9 @@ base_units = [ { u = "Second", p = 1} ]
[[unit]] [[unit]]
enum_name = "Day" enum_name = "Day"
print = "d" strings = [
parse = ["d", "day", "days"] ["d"], ["day"], ["days"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "86400" base_value = "86400"
@ -142,8 +143,9 @@ base_units = [ { u = "Second", p = 1} ]
[[unit]] [[unit]]
enum_name = "Week" enum_name = "Week"
print = "w" strings = [
parse = ["w", "week", "weeks"] ["w"], ["week"], ["weeks"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "604800" base_value = "604800"
@ -153,8 +155,9 @@ base_units = [ { u = "Second", p = 1} ]
[[unit]] [[unit]]
enum_name = "Month" enum_name = "Month"
print = "month" strings = [
parse = ["month", "months"] ["month"], ["months"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "2629746" base_value = "2629746"
@ -164,8 +167,9 @@ base_units = [ { u = "Second", p = 1} ]
[[unit]] [[unit]]
enum_name = "Fortnight" enum_name = "Fortnight"
print = "fortnight" strings = [
parse = ["fortnight", "fortnights"] ["fortnight"], ["fortnights"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1209600" base_value = "1209600"
@ -175,8 +179,9 @@ base_units = [ { u = "Second", p = 1} ]
[[unit]] [[unit]]
enum_name = "GregorianYear" enum_name = "GregorianYear"
print = "year" strings = [
parse = ["year", "years"] ["year"], ["years"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "31557000" base_value = "31557000"
@ -186,8 +191,10 @@ base_units = [ { u = "Second", p = 1} ]
[[unit]] [[unit]]
enum_name = "JulianYear" enum_name = "JulianYear"
print = "julianYear" strings = [
parse = ["julianYear", "julianYears"] ["julianYear"], ["julianYears"],
["julianyear"], ["julianyears"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "31557600" base_value = "31557600"
@ -201,8 +208,9 @@ base_units = [ { u = "Second", p = 1} ]
[[unit]] [[unit]]
enum_name = "Angstrom" enum_name = "Angstrom"
print = "Å" strings = [
parse = ["Å", "angstrom"] ["Å"], ["angstrom"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1e-10" base_value = "1e-10"
@ -212,8 +220,9 @@ base_units = [ { u = "Meter", p = 1} ]
[[unit]] [[unit]]
enum_name = "Thou" enum_name = "Thou"
print = "thou" strings = [
parse = ["thou"] ["thou"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.0000254" base_value = "0.0000254"
@ -223,8 +232,9 @@ base_units = [ { u = "Meter", p = 1} ]
[[unit]] [[unit]]
enum_name = "Point" enum_name = "Point"
print = "pt" strings = [
parse = ["pt", "point"] ["pt"], ["point"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.0003514598" base_value = "0.0003514598"
@ -234,8 +244,9 @@ base_units = [ { u = "Meter", p = 1} ]
[[unit]] [[unit]]
enum_name = "Inch" enum_name = "Inch"
print = "in" strings = [
parse = ["in", "inch", "inches"] ["in"], ["inch"], ["inches"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.0254" base_value = "0.0254"
@ -245,8 +256,9 @@ base_units = [ { u = "Meter", p = 1} ]
[[unit]] [[unit]]
enum_name = "Foot" enum_name = "Foot"
print = "ft" strings = [
parse = ["ft", "foot", "feet"] ["ft"], ["foot"], ["feet"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.3048" base_value = "0.3048"
@ -256,8 +268,9 @@ base_units = [ { u = "Meter", p = 1} ]
[[unit]] [[unit]]
enum_name = "Yard" enum_name = "Yard"
print = "yd" strings = [
parse = ["yd", "yard", "yards"] ["yd"], ["yard"], ["yards"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.9144" base_value = "0.9144"
@ -267,8 +280,9 @@ base_units = [ { u = "Meter", p = 1} ]
[[unit]] [[unit]]
enum_name = "Furlong" enum_name = "Furlong"
print = "furlong" strings = [
parse = ["furlong", "furlongs"] ["furlong"], ["furlongs"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "201.17" base_value = "201.17"
@ -277,8 +291,9 @@ base_units = [ { u = "Meter", p = 1} ]
[[unit]] [[unit]]
enum_name = "Rod" enum_name = "Rod"
print = "rod" strings = [
parse = ["rod", "rods"] ["rod"], ["rods"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "5.0292" base_value = "5.0292"
@ -288,8 +303,9 @@ base_units = [ { u = "Meter", p = 1} ]
[[unit]] [[unit]]
enum_name = "Mile" enum_name = "Mile"
print = "mi" strings = [
parse = ["mi", "mile", "miles"] ["mi"], ["mile"], ["miles"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1609.344" base_value = "1609.344"
@ -299,8 +315,10 @@ base_units = [ { u = "Meter", p = 1} ]
[[unit]] [[unit]]
enum_name = "AstronomicalUnit" enum_name = "AstronomicalUnit"
print = "au" strings = [
parse = ["au", "AU", "astronomicalUnit", "astronomicalUnits"] ["au"], ["AU"], ["astronomicalUnit"], ["astronomicalUnits"],
["astronomicalunit"], ["astronomicalunits"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "149597870700" base_value = "149597870700"
@ -310,8 +328,9 @@ base_units = [ { u = "Meter", p = 1} ]
[[unit]] [[unit]]
enum_name = "Lightyear" enum_name = "Lightyear"
print = "ly" strings = [
parse = ["ly", "lightyear", "lightyears"] ["ly"], ["lightyear"], ["lightyears"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "9460730472580800" base_value = "9460730472580800"
@ -321,8 +340,9 @@ base_units = [ { u = "Meter", p = 1} ]
[[unit]] [[unit]]
enum_name = "Parsec" enum_name = "Parsec"
print = "pc" strings = [
parse = ["pc", "parsec", "parsecs"] ["pc"], ["parsec"], ["parsecs"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "3.085677581e16" base_value = "3.085677581e16"
@ -335,8 +355,9 @@ base_units = [ { u = "Meter", p = 1} ]
[[unit]] [[unit]]
enum_name = "Barn" enum_name = "Barn"
print = "b" strings = [
parse = ["b", "barn"] ["barn"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1e-28" base_value = "1e-28"
@ -346,8 +367,9 @@ base_units = [ { u = "Meter", p = 2} ]
[[unit]] [[unit]]
enum_name = "Hectare" enum_name = "Hectare"
print = "ha" strings = [
parse = ["hectare", "hectares"] ["ha"], ["hectare"], ["hectares"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "10000" base_value = "10000"
@ -357,8 +379,9 @@ base_units = [ { u = "Meter", p = 2} ]
[[unit]] [[unit]]
enum_name = "Acre" enum_name = "Acre"
print = "acre" strings = [
parse = ["acres"] ["acre"], ["acres"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "4046.8564224" base_value = "4046.8564224"
@ -371,9 +394,11 @@ base_units = [ { u = "Meter", p = 2} ]
[[unit]] [[unit]]
enum_name = "Liter" enum_name = "Liter"
print = "l" strings = [
parse_with_prefix = ["l", "L"] ["l", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse = ["liter", "liters", "litre", "litres"] ["L", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
["liter"], ["liters"], ["litre"], ["litres"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.001" base_value = "0.001"
@ -383,8 +408,9 @@ base_units = [ { u = "Meter", p = 3} ]
[[unit]] [[unit]]
enum_name = "USGallon" enum_name = "USGallon"
print = "gal" strings = [
parse = ["gal", "gals", "usgal", "gallon", "gallons", "Gallon", "Gallons"] ["gal"], ["gals"], ["usgal"], ["gallon"], ["gallons"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.003785411784" base_value = "0.003785411784"
@ -394,8 +420,9 @@ base_units = [ { u = "Meter", p = 3} ]
[[unit]] [[unit]]
enum_name = "Quart" enum_name = "Quart"
print = "qt" strings = [
parse = ["quart", "quarts"] ["qt"], ["quart"], ["quarts"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.000946352946" base_value = "0.000946352946"
@ -405,8 +432,9 @@ base_units = [ { u = "Meter", p = 3} ]
[[unit]] [[unit]]
enum_name = "ImperialGallon" enum_name = "ImperialGallon"
print = "impgal" strings = [
parse = ["imperialGallon", "imperialGallons"] ["impgal"], ["imperialGallon"], ["imperialGallons"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.00454609" base_value = "0.00454609"
@ -416,8 +444,9 @@ base_units = [ { u = "Meter", p = 3} ]
[[unit]] [[unit]]
enum_name = "Hogshead" enum_name = "Hogshead"
print = "hogshead" strings = [
parse = ["hogshead", "hogsheads"] ["hogshead"], ["hogsheads"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.2385" # 63 gallons base_value = "0.2385" # 63 gallons
@ -427,8 +456,9 @@ base_units = [ { u = "Meter", p = 3} ]
[[unit]] [[unit]]
enum_name = "Cup" enum_name = "Cup"
print = "cup" strings = [
parse = ["cup"] ["cup"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.0002365882365" base_value = "0.0002365882365"
@ -438,8 +468,9 @@ base_units = [ { u = "Meter", p = 3} ]
[[unit]] [[unit]]
enum_name = "Floz" enum_name = "Floz"
print = "floz" strings = [
parse = ["floz"] ["floz"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.0000295735295625" base_value = "0.0000295735295625"
@ -449,8 +480,9 @@ base_units = [ { u = "Meter", p = 3} ]
[[unit]] [[unit]]
enum_name = "Pint" enum_name = "Pint"
print = "pint" strings = [
parse = ["pint", "pints"] ["pint"], ["pints"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.00056826125" base_value = "0.00056826125"
@ -460,8 +492,9 @@ base_units = [ { u = "Meter", p = 3} ]
[[unit]] [[unit]]
enum_name = "Tablespoon" enum_name = "Tablespoon"
print = "tbsp" strings = [
parse = ["tbsp", "Tbsp", "tablespoon", "Tablespoon"] ["tbsp"], ["Tbsp"], ["tablespoon"], ["Tablespoon"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.00001478676478125" base_value = "0.00001478676478125"
@ -471,8 +504,9 @@ base_units = [ { u = "Meter", p = 3} ]
[[unit]] [[unit]]
enum_name = "Teaspoon" enum_name = "Teaspoon"
print = "tsp" strings = [
parse = ["tsp", "Tsp", "teaspoon", "teaspoons"] ["tsp"], ["Tsp"], ["teaspoon"], ["teaspoons"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.000005" base_value = "0.000005"
@ -485,9 +519,10 @@ base_units = [ { u = "Meter", p = 3} ]
[[unit]] [[unit]]
enum_name = "Pascal" enum_name = "Pascal"
print = "Pa" strings = [
parse_with_prefix = "Pa" ["Pa", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse = ["pascal"] ["pascal"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1000" base_value = "1000"
@ -496,8 +531,9 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = -1}, { u = "Second", p =
[[unit]] [[unit]]
enum_name = "Atmosphere" enum_name = "Atmosphere"
print = "atm" strings = [
parse = ["atm", "atmosphere", "atmospheres"] ["atm"], ["atmosphere"], ["atmospheres"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "101325000" base_value = "101325000"
@ -506,8 +542,9 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = -1}, { u = "Second", p =
[[unit]] [[unit]]
enum_name = "Bar" enum_name = "Bar"
print = "bar" strings = [
parse_with_prefix = "bar" ["bar", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
]
base_value_type = "exact" base_value_type = "exact"
base_value = "100000000" base_value = "100000000"
@ -516,8 +553,9 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = -1}, { u = "Second", p =
[[unit]] [[unit]]
enum_name = "Barye" enum_name = "Barye"
print = "Ba" strings = [
parse = ["Ba", "Barye"] ["Ba"], ["Barye"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "100" base_value = "100"
@ -526,8 +564,9 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = -1}, { u = "Second", p =
[[unit]] [[unit]]
enum_name = "Psi" enum_name = "Psi"
print = "psi" strings = [
parse = ["psi"] ["psi"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "6894757.2931783" base_value = "6894757.2931783"
@ -536,8 +575,9 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = -1}, { u = "Second", p =
[[unit]] [[unit]]
enum_name = "MillimeterMercury" enum_name = "MillimeterMercury"
print = "mmHg" strings = [
parse = ["mmhg", "mmHg"] ["mmhg"], ["mmHg"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "133322.387415" base_value = "133322.387415"
@ -546,8 +586,9 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = -1}, { u = "Second", p =
[[unit]] [[unit]]
enum_name = "Torr" enum_name = "Torr"
print = "torr" strings = [
parse = ["torr", "Torr"] ["torr"], ["Torr"]
]
base_value_type = "fract" base_value_type = "fract"
base_value = [101325000, 760] base_value = [101325000, 760]
@ -556,8 +597,9 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = -1}, { u = "Second", p =
[[unit]] [[unit]]
enum_name = "MeterSeaWater" enum_name = "MeterSeaWater"
print = "MSW" strings = [
parse = ["MSW", "msw"] ["MSW"], ["msw"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "10000000" base_value = "10000000"
@ -566,8 +608,9 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = -1}, { u = "Second", p =
[[unit]] [[unit]]
enum_name = "FootSeaWater" enum_name = "FootSeaWater"
print = "FSW" strings = [
parse = ["FSW", "fsw"] ["FSW"], ["fsw"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "3064330" base_value = "3064330"
@ -579,8 +622,9 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = -1}, { u = "Second", p =
[[unit]] [[unit]]
enum_name = "Gauss" enum_name = "Gauss"
print = "G" strings = [
parse = ["G", "gauss"] ["G"], ["gauss"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.1" base_value = "0.1"
@ -590,9 +634,10 @@ base_units = [ { u = "Gram", p = 1}, { u = "Second", p = -2}, { u = "Ampere", p
[[unit]] [[unit]]
enum_name = "Tesla" enum_name = "Tesla"
print = "T" strings = [
parse = ["tesla"] ["T", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "T" ["tesla"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1000" base_value = "1000"
@ -602,9 +647,10 @@ base_units = [ { u = "Gram", p = 1}, { u = "Second", p = -2}, { u = "Ampere", p
[[unit]] [[unit]]
enum_name = "Joule" enum_name = "Joule"
print = "J" strings = [
parse = ["joule", "joules"] ["J", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "J" ["joule"], ["joules"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1000" base_value = "1000"
@ -614,9 +660,9 @@ base_units = [ { u = "Gram", p = 1}, { u = "Second", p = -2}, { u = "Meter", p =
[[unit]] [[unit]]
enum_name = "Calorie" enum_name = "Calorie"
print = "cal" strings = [
parse = ["calorie", "calories"] ["cal"], ["calorie"], ["calories"]
parse_with_prefix = "cal" ]
base_value_type = "exact" base_value_type = "exact"
base_value = "4184" base_value = "4184"
@ -626,8 +672,9 @@ base_units = [ { u = "Gram", p = 1}, { u = "Second", p = -2}, { u = "Meter", p =
[[unit]] [[unit]]
enum_name = "BTU" enum_name = "BTU"
print = "BTU" strings = [
parse = ["btu", "BTU"] ["btu"], ["BTU"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1055100" base_value = "1055100"
@ -637,9 +684,10 @@ base_units = [ { u = "Gram", p = 1}, { u = "Second", p = -2}, { u = "Meter", p =
[[unit]] [[unit]]
enum_name = "Hertz" enum_name = "Hertz"
print = "Hz" strings = [
parse = ["hertz"] ["Hz", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "Hz" ["hertz"], ["Hertz"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1" base_value = "1"
@ -649,9 +697,11 @@ base_units = [ { u = "Second", p = -1} ]
[[unit]] [[unit]]
enum_name = "Ohm" enum_name = "Ohm"
print = "Ω" strings = [
parse = ["ohms"] ["Ω", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = ["ohm", "Ω"] ["ohm", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
["ohms", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1000" base_value = "1000"
@ -661,9 +711,10 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = 2}, { u = "Ampere", p =
[[unit]] [[unit]]
enum_name = "Siemens" enum_name = "Siemens"
print = "S" strings = [
parse = ["℧", "mho", "mhos", "siemens"] ["S", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "S" ["℧"], ["mho"], ["mhos"], ["siemens"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.001" base_value = "0.001"
@ -673,9 +724,10 @@ base_units = [ { u = "Gram", p = -1}, { u = "Meter", p = -2}, { u = "Ampere", p
[[unit]] [[unit]]
enum_name = "Coulomb" enum_name = "Coulomb"
print = "C" strings = [
parse = ["coulomb", "coulombs"] ["C", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "C" ["coulomb"], ["coulombs"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.001" base_value = "0.001"
@ -685,9 +737,10 @@ base_units = [ { u = "Ampere", p = 1}, { u = "Second", p = 1} ]
[[unit]] [[unit]]
enum_name = "Watt" enum_name = "Watt"
print = "W" strings = [
parse = ["watt", "Watt", "Watts", "watts"] ["W", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "W" ["watt"], ["Watt"], ["Watts"], ["watts"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1000" base_value = "1000"
@ -697,9 +750,10 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = 2}, { u = "Second", p =
[[unit]] [[unit]]
enum_name = "Volt" enum_name = "Volt"
print = "V" strings = [
parse = ["volt", "volts"] ["V", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "V" ["volt"], ["volts"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1000" base_value = "1000"
@ -709,9 +763,10 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = 2}, { u = "Second", p =
[[unit]] [[unit]]
enum_name = "Henry" enum_name = "Henry"
print = "H" strings = [
parse = ["henry", "henries", "henrys", "Henry", "Henries", "Henrys"] ["H", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "H" ["henry"], ["henries"], ["henrys"], ["Henry"], ["Henries"], ["Henrys"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1000" base_value = "1000"
@ -721,9 +776,10 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = 2}, { u = "Second", p =
[[unit]] [[unit]]
enum_name = "Farad" enum_name = "Farad"
print = "F" strings = [
parse = ["farad", "Farad", "farads", "Farads"] ["F", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "F" ["farad"], ["Farad"], ["farads"], ["Farads"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.001" base_value = "0.001"
@ -733,9 +789,10 @@ base_units = [ { u = "Gram", p = -1}, { u = "Meter", p = -2}, { u = "Second", p
[[unit]] [[unit]]
enum_name = "ElectronVolt" enum_name = "ElectronVolt"
print = "eV" strings = [
parse = ["electronvolt"] ["eV", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "eV" ["electronvolt"], ["electronVolt"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1.602176634e-16" base_value = "1.602176634e-16"
@ -745,9 +802,10 @@ base_units = [ { u = "Gram", p = 1}, { u = "Second", p = -2}, { u = "Meter", p =
[[unit]] [[unit]]
enum_name = "Weber" enum_name = "Weber"
print = "Wb" strings = [
parse = ["weber"] ["Wb", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "Wb" ["weber"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1000" base_value = "1000"
@ -761,8 +819,9 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = 2}, { u = "Second", p =
[[unit]] [[unit]]
enum_name = "Tonne" enum_name = "Tonne"
print = "t" strings = [
parse = ["t", "tonne", "tonnes", "ton", "tons"] ["t"], ["tonne"], ["tonnes"], ["ton"], ["tons"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1000000" base_value = "1000000"
@ -772,8 +831,9 @@ base_units = [ { u = "Gram", p = 1} ]
[[unit]] [[unit]]
enum_name = "Ounce" enum_name = "Ounce"
print = "oz" strings = [
parse = ["oz", "ounce", "ounces"] ["oz"], ["ounce"], ["ounces"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "28.349523125" base_value = "28.349523125"
@ -783,8 +843,9 @@ base_units = [ { u = "Gram", p = 1} ]
[[unit]] [[unit]]
enum_name = "Pound" enum_name = "Pound"
print = "lb" strings = [
parse = ["lb", "lbs", "pound", "pounds"] ["lb"], ["lbs"], ["pound"], ["pounds"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "453.59237" base_value = "453.59237"
@ -794,8 +855,9 @@ base_units = [ { u = "Gram", p = 1} ]
[[unit]] [[unit]]
enum_name = "PoundForce" enum_name = "PoundForce"
print = "lbf" strings = [
parse = ["lbf", "poundforce"] ["lbf"], ["poundforce"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "4448.2216152605" base_value = "4448.2216152605"
@ -805,9 +867,10 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = 1}, { u = "Second", p =
[[unit]] [[unit]]
enum_name = "Newton" enum_name = "Newton"
print = "N" strings = [
parse = ["newton", "newtons"] ["N", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "N" ["newton"], ["newtons"], ["Newton"], ["Newtons"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1000" base_value = "1000"
@ -820,9 +883,10 @@ base_units = [ { u = "Gram", p = 1}, { u = "Meter", p = 1}, { u = "Second", p =
[[unit]] [[unit]]
enum_name = "Katal" enum_name = "Katal"
print = "kat" strings = [
parse = ["katal"] ["kat", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "kat" ["katal"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1" base_value = "1"
@ -832,8 +896,9 @@ base_units = [ { u = "Mole", p = 1}, { u = "Second", p = -1} ]
[[unit]] [[unit]]
enum_name = "Degree" enum_name = "Degree"
print = "°" strings = [
parse = ["°", "deg", "degree", "degrees"] ["°"], ["deg"], ["degree"], ["degrees"]
]
base = true base = true
no_space = true no_space = true
@ -841,8 +906,9 @@ no_space = true
[[unit]] [[unit]]
enum_name = "Radian" enum_name = "Radian"
print = "r" strings = [
parse = ["r", "radian", "radians"] ["r"], ["radian"], ["radians"]
]
base_value_type = "approx" base_value_type = "approx"
base_value = "57.295779513" base_value = "57.295779513"
@ -852,8 +918,9 @@ base_units = [ { u = "Degree", p = 1} ]
[[unit]] [[unit]]
enum_name = "RPM" enum_name = "RPM"
print = "rpm" strings = [
parse = ["rpm"] ["rpm"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "360" base_value = "360"
@ -865,9 +932,10 @@ base_units = [ { u = "Degree", p = 1}, { u = "Second", p = -1} ]
[[unit]] [[unit]]
enum_name = "Becquerel" enum_name = "Becquerel"
print = "Bq" strings = [
parse = ["becquerel"] ["Bq", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "Bq" ["becquerel"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1" base_value = "1"
@ -877,9 +945,10 @@ base_units = [ { u = "Second", p = -1} ]
[[unit]] [[unit]]
enum_name = "Gray" enum_name = "Gray"
print = "Gy" strings = [
parse = ["gray"] ["Gy", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "Gy" ["gray"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "1" base_value = "1"
@ -889,8 +958,9 @@ base_units = [ { u = "Meter", p = 2}, { u = "Second", p = -2} ]
[[unit]] # Not radian, radioactivity unit [[unit]] # Not radian, radioactivity unit
enum_name = "Rad" enum_name = "Rad"
print = "rad" strings = [
parse_with_prefix = "rad" ["rad", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
]
base_value_type = "exact" base_value_type = "exact"
base_value = "0.01" base_value = "0.01"
@ -904,41 +974,49 @@ base_units = [ { u = "Meter", p = 2}, { u = "Second", p = -2} ]
[[unit]] [[unit]]
enum_name = "Bit" enum_name = "Bit"
print = "bit" strings = [
parse = ["bit", "bits"] ["bit"], ["bits"]
]
base = true base = true
[[unit]] [[unit]]
enum_name = "Frame" enum_name = "Frame"
print = "frame" strings = [
parse = ["frame", "frames"] ["frame"], ["frames"]
]
base = true base = true
[[unit]] [[unit]]
enum_name = "Pixel" enum_name = "Pixel"
print = "px" strings = [
parse = ["px", "pixel", "pixels", "Pixel", "Pixels"] ["px"], ["pixel"], ["pixels"], ["Pixel"], ["Pixels"]
]
base = true base = true
[[unit]] [[unit]]
enum_name = "Dot" enum_name = "Dot"
print = "dot" strings = [
parse = ["dot", "dots"] ["dot"], ["dots"]
]
base = true base = true
[[unit]] [[unit]]
enum_name = "Byte" enum_name = "Byte"
print = "B" strings = [
parse = ["Bytes", "bytes", "Byte", "byte", "Octet", "Octets", "octets", "octet"] ["B", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
parse_with_prefix = "B" ["b", "","Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q"],
["B", "Ei","Pi","Ti","Gi","Mi","Ki"],
["b", "Ei","Pi","Ti","Gi","Mi","Ki"],
["Bytes"], ["bytes"], ["Byte"], ["byte"], ["Octet"], ["Octets"], ["octets"], ["octet"]
]
base_value_type = "exact" base_value_type = "exact"
base_value = "8" base_value = "8"

View File

@ -11,4 +11,5 @@ pub use freeunit::FreeUnit;
use crate::quantity::Quantity; use crate::quantity::Quantity;
use crate::quantity::Scalar; use crate::quantity::Scalar;
use prefix::str_to_prefix;
include!(concat!(env!("OUT_DIR"), "/units.rs")); include!(concat!(env!("OUT_DIR"), "/units.rs"));

View File

@ -32,7 +32,15 @@ pub enum Prefix {
Zepto, Zepto,
Yocto, Yocto,
Ronto, Ronto,
Quecto Quecto,
BinExa,
BinPeta,
BinTera,
BinGiga,
BinMega,
BinKilo
} }
@ -65,6 +73,14 @@ impl Prefix {
Prefix::Ronto => "1e-27", Prefix::Ronto => "1e-27",
Prefix::Quecto => "1e-30", Prefix::Quecto => "1e-30",
Prefix::BinExa => "1152921504606846976", // 2^60
Prefix::BinPeta => "1125899906842624", // 2^50
Prefix::BinTera => "1099511627776", // 2^40
Prefix::BinGiga => "1073741824", // 2^30
Prefix::BinMega => "1048576", // 2^20
Prefix::BinKilo => "1024", // 2^10
Prefix::None => { "1" } Prefix::None => { "1" }
}).unwrap(); }).unwrap();
@ -73,6 +89,41 @@ impl Prefix {
} }
} }
macro_rules! str_to_prefix {
("") => {Prefix::None};
("Q") => {Prefix::Quetta};
("R") => {Prefix::Ronna};
("Y") => {Prefix::Yotta};
("Z") => {Prefix::Zetta};
("E") => {Prefix::Exa};
("P") => {Prefix::Peta};
("T") => {Prefix::Tera};
("G") => {Prefix::Giga};
("M") => {Prefix::Mega};
("k") => {Prefix::Kilo};
("h") => {Prefix::Hecto};
("da") => {Prefix::Deka};
("d") => {Prefix::Deci};
("c") => {Prefix::Centi};
("m") => {Prefix::Milli};
("u") => {Prefix::Micro};
("n") => {Prefix::Nano};
("p") => {Prefix::Pico};
("f") => {Prefix::Femto};
("a") => {Prefix::Atto};
("z") => {Prefix::Zepto};
("y") => {Prefix::Yocto};
("r") => {Prefix::Ronto};
("q") => {Prefix::Quecto};
("Ei") => {Prefix::BinExa};
("Pi") => {Prefix::BinPeta};
("Ti") => {Prefix::BinTera};
("Gi") => {Prefix::BinGiga};
("Mi") => {Prefix::BinMega};
("Ki") => {Prefix::BinKilo};
}
pub (super) use str_to_prefix;
impl ToString for Prefix { impl ToString for Prefix {
fn to_string(&self) -> String { fn to_string(&self) -> String {
@ -103,6 +154,14 @@ impl ToString for Prefix {
Prefix::Ronto => "r", Prefix::Ronto => "r",
Prefix::Quecto => "q", Prefix::Quecto => "q",
Prefix::BinExa => "Ei",
Prefix::BinPeta => "Pi",
Prefix::BinTera => "Ti",
Prefix::BinGiga => "Gi",
Prefix::BinMega => "Mi",
Prefix::BinKilo => "Ki",
Prefix::None => "" Prefix::None => ""
}) })
} }