From 5371f8d5ac440d7d423de0bf44eead94d0e177b6 Mon Sep 17 00:00:00 2001 From: Kasper Date: Fri, 20 Nov 2020 20:19:31 +0100 Subject: [PATCH] Added units of electric current --- README.md | 2 +- src/lexer.rs | 5 +++++ src/units.rs | 12 ++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1a54d1e..7e2ec0b 100644 --- a/README.md +++ b/README.md @@ -142,7 +142,7 @@ create_units!( ) ``` -The number associated with a unit is it's "weight". For example, if a second's weight is `1`, then a minute's weight is `1000`. +The number associated with a unit is it's "weight". For example, if a second's weight is `1`, then a minute's weight is `60`. I have found [translatorscafe.com](https://www.translatorscafe.com/unit-converter) and [calculateme.com](https://www.calculateme.com/) to be good websites for unit conversion. Wikipedia is worth looking at as well. diff --git a/src/lexer.rs b/src/lexer.rs index ebee167..5e34cd9 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -318,6 +318,11 @@ pub fn lex(input: &str, allow_trailing_operators: bool, default_degree: Unit) -> "hp" | "hps" | "horsepower" | "horsepowers" => tokens.push(Token::Unit(Horsepower)), "mhp" | "hpm" | "metric hp" | "metric hps" | "metric horsepower" | "metric horsepowers" => tokens.push(Token::Unit(MetricHorsepower)), + "ma" | "milliamp" | "milliampere" => tokens.push(Token::Unit(Milliampere)), + "a" | "amp" | "ampere" => tokens.push(Token::Unit(Ampere)), + "ka" | "kiloamp" | "Kiloampere" => tokens.push(Token::Unit(Kiloampere)), + "bi" | "biot" | "biots" | "aba" | "abampere" => tokens.push(Token::Unit(Abampere)), + // for pound-force per square inch "lbf" => tokens.push(Token::LexerKeyword(PoundForce)), "force" => tokens.push(Token::LexerKeyword(Force)), diff --git a/src/units.rs b/src/units.rs index 1de9f29..d3c70c6 100644 --- a/src/units.rs +++ b/src/units.rs @@ -23,6 +23,8 @@ pub enum UnitType { Energy, /// A unit of power, for example `Watt` Power, + /// A unit of electrical current, for example `Ampere` + ElectricCurrent, /// A unit of pressure, for example `Bar` Pressure, /// A unit of frequency, for example `Hertz` @@ -209,6 +211,11 @@ create_units!( Horsepower: (Power, d128!(745.69987158227022)), // exact according to wikipedia MetricHorsepower: (Power, d128!(735.49875)), + Milliampere: (ElectricCurrent, d128!(1)), + Ampere: (ElectricCurrent, d128!(1000)), + Kiloampere: (ElectricCurrent, d128!(1000000)), + Abampere: (ElectricCurrent, d128!(10000)), + Pascal: (Pressure, d128!(1)), Kilopascal: (Pressure, d128!(1000)), Atmosphere: (Pressure, d128!(101325)), @@ -653,6 +660,7 @@ mod tests { assert_eq!(convert_test(1000.0, GigawattHour, TerawattHour), 1.0); assert_eq!(convert_test(1000.0, TerawattHour, PetawattHour), 1.0); + assert_eq!(convert_test(1000.0, Milliwatt, Watt), 1.0); assert_eq!(convert_test(1000.0, Watt, Kilowatt), 1.0); assert_eq!(convert_test(1000.0, Kilowatt, Megawatt), 1.0); assert_eq!(convert_test(1000.0, Megawatt, Gigawatt), 1.0); @@ -663,6 +671,10 @@ mod tests { assert_eq!(convert_test(745.6998715822702, Watt, Horsepower), 1.0); assert_eq!(convert_test(735.49875, Watt, MetricHorsepower), 1.0); + assert_eq!(convert_test(1000.0, Milliampere, Ampere), 1.0); + assert_eq!(convert_test(1000.0, Ampere, Kiloampere), 1.0); + assert_eq!(convert_test(10.0, Ampere, Biot), 1.0); + assert_eq!(convert_test(1000.0, Pascal, Kilopascal), 1.0); assert_eq!(convert_test(101325.0, Pascal, Atmosphere), 1.0); assert_eq!(convert_test(100.0, Pascal, Millibar), 1.0);