oxedyne/fe2o3/fe2o3_units/src/si.rs
2.5 KiB, 36 runs
created by r1870400018:1237, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use crate::system::System; |
| 2 | |
| 3 | use oxedyne_fe2o3_core::prelude::*; |
| 4 | |
| 5 | #[derive(Clone, Debug, Default, PartialEq)] |
| 6 | pub struct SI { |
| 7 | // dim fields |
| 8 | time: i8, |
| 9 | length: i8, |
| 10 | mass: i8, |
| 11 | current: i8, |
| 12 | temp: i8, |
| 13 | amount: i8, |
| 14 | luminosity: i8, |
| 15 | data: i8, |
| 16 | base: bool, // is this a base unit? (i.e. only one dim field has a value of 1, the rest 0) |
| 17 | base_sym: &'static str, |
| 18 | } |
| 19 | |
| 20 | impl SI { |
| 21 | |
| 22 | const TIME_UNIT: &'static str = "s"; |
| 23 | const LENGTH_UNIT: &'static str = "m"; |
| 24 | const MASS_UNIT: &'static str = "kg"; |
| 25 | const CURRENT_UNIT: &'static str = "A"; |
| 26 | const TEMP_UNIT: &'static str = "K"; |
| 27 | const AMOUNT_UNIT: &'static str = "mol"; |
| 28 | const LUMINOSITY_UNIT: &'static str = "cd"; |
| 29 | const DATA_UNIT: &'static str = "B"; |
| 30 | |
| 31 | pub fn meters() -> Self { |
| 32 | Self { |
| 33 | length: 1, |
| 34 | base: true, |
| 35 | base_sym: Self::LENGTH_UNIT, |
| 36 | ..Default::default() |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | pub fn bytes() -> Self { |
| 41 | Self { |
| 42 | data: 1, |
| 43 | base: true, |
| 44 | base_sym: Self::DATA_UNIT, |
| 45 | ..Default::default() |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | pub fn secs() -> Self { |
| 50 | Self { |
| 51 | time: 1, |
| 52 | base: true, |
| 53 | base_sym: Self::TIME_UNIT, |
| 54 | ..Default::default() |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | } |
| 59 | |
| 60 | impl System for SI { |
| 61 | fn base_symbol(&self) -> &'static str { |
| 62 | self.base_sym |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | impl std::fmt::Display for SI { |
| 67 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 68 | if self.time != 0 { |
| 69 | ok!(write!(f, "[{}]^{}", Self::TIME_UNIT, self.time)); |
| 70 | } |
| 71 | if self.length != 0 { |
| 72 | ok!(write!(f, "[{}]^{}", Self::LENGTH_UNIT, self.length)); |
| 73 | } |
| 74 | if self.mass != 0 { |
| 75 | ok!(write!(f, "[{}]^{}", Self::MASS_UNIT, self.mass)); |
| 76 | } |
| 77 | if self.current != 0 { |
| 78 | ok!(write!(f, "[{}]^{}", Self::CURRENT_UNIT, self.current)); |
| 79 | } |
| 80 | if self.temp != 0 { |
| 81 | ok!(write!(f, "[{}]^{}", Self::TEMP_UNIT, self.temp)); |
| 82 | } |
| 83 | if self.amount != 0 { |
| 84 | ok!(write!(f, "[{}]^{}", Self::AMOUNT_UNIT, self.amount)); |
| 85 | } |
| 86 | if self.luminosity != 0 { |
| 87 | ok!(write!(f, "[{}]^{}", Self::LUMINOSITY_UNIT, self.luminosity)); |
| 88 | } |
| 89 | if self.data != 0 { |
| 90 | ok!(write!(f, "[{}]^{}", Self::DATA_UNIT, self.data)); |
| 91 | } |
| 92 | Ok(()) |
| 93 | } |
| 94 | } |