oxedyne/fe2o3/fe2o3_units/src/system.rs
1.8 KiB, 7 runs
created by r1870400018:1239, 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 oxedyne_fe2o3_core::prelude::*; |
| 2 | |
| 3 | use crate::{ |
| 4 | scale::{ |
| 5 | Mag, |
| 6 | Scale, |
| 7 | ScaleBasis, |
| 8 | }, |
| 9 | si::SI, |
| 10 | }; |
| 11 | |
| 12 | pub trait System: Clone + std::fmt::Display + PartialEq { |
| 13 | fn base_symbol(&self) -> &'static str { |
| 14 | "" |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | #[derive(Clone, Debug)] |
| 19 | pub struct Units<D: System> { |
| 20 | pub mag: Mag, |
| 21 | pub dim: D, |
| 22 | } |
| 23 | |
| 24 | //impl<D> fmt::Display for Units<D> |
| 25 | // where D: Clone + fmt::Display + PartialEq |
| 26 | //{ |
| 27 | // fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 28 | // match self.system { |
| 29 | // System::SI => { |
| 30 | // match write!(f, "f"), |
| 31 | // }, |
| 32 | // |
| 33 | // } |
| 34 | // } |
| 35 | //} |
| 36 | |
| 37 | impl<D: System> Units<D> { |
| 38 | |
| 39 | pub fn new(mag: Mag, dim: D) -> Self { |
| 40 | Self{ |
| 41 | mag: mag, |
| 42 | dim: dim, |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | pub fn bytes(val: f64, sf: u8) -> Outcome<Units::<SI>> { |
| 47 | Ok(Units::new( |
| 48 | ok!(Mag::new(val, Scale::One(ScaleBasis::Binary), sf)), |
| 49 | SI::bytes(), |
| 50 | )) |
| 51 | } |
| 52 | |
| 53 | pub fn mag(&self) -> &Mag { |
| 54 | &self.mag |
| 55 | } |
| 56 | |
| 57 | pub fn dim(&self) -> &D { |
| 58 | &self.dim |
| 59 | } |
| 60 | |
| 61 | pub fn val(&self) -> f64 { |
| 62 | self.mag.val |
| 63 | } |
| 64 | |
| 65 | pub fn prefix(&self) -> &'static str { |
| 66 | self.mag.prefix() |
| 67 | } |
| 68 | |
| 69 | /// Returns "" if the dimension does not represent a fundamental unit. |
| 70 | pub fn symbol(&self) -> &'static str { |
| 71 | self.dim.base_symbol() |
| 72 | } |
| 73 | |
| 74 | pub fn unitise(&self) -> Self { |
| 75 | Self::new( |
| 76 | self.mag.unitise(), |
| 77 | self.dim.clone(), |
| 78 | ) |
| 79 | } |
| 80 | |
| 81 | pub fn humanise(&self) -> Self { |
| 82 | Self::new( |
| 83 | self.mag.humanise(), |
| 84 | self.dim.clone(), |
| 85 | ) |
| 86 | } |
| 87 | |
| 88 | } |
| 89 | |
| 90 | impl<D: System> PartialEq for Units<D> { |
| 91 | |
| 92 | fn eq(&self, other: &Self) -> bool { |
| 93 | if self.dim.eq(&other.dim) { |
| 94 | return self.mag.eq(&other.mag); |
| 95 | } |
| 96 | false |
| 97 | } |
| 98 | } |