1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
#[derive(Debug)] struct ScaledValue { value: f32, suffix: String, } #[derive(Debug)] pub struct Formatter { decimals: usize, separator: String, scales: Scales, forced_units: String, forced_suffix: String, } #[derive(Debug)] pub struct Scales { base: u32, suffixes: Vec<String>, } impl Formatter { pub fn new() -> Self { Formatter { decimals: 2, separator: " ".to_owned(), scales: Scales::SI(), forced_units: "".to_owned(), forced_suffix: "".to_owned(), } } pub fn with_decimals(&mut self, decimals: usize) -> &mut Self { self.decimals = decimals; self } pub fn with_separator(&mut self, separator: &str) -> &mut Self { self.separator = separator.to_owned(); self } pub fn with_scales(&mut self, scales: Scales) -> &mut Self { self.scales = scales; self } pub fn with_units(&mut self, units: &str) -> &mut Self { self.forced_units = units.to_owned(); self } pub fn with_suffix(&mut self, suffix: &str) -> &mut Self { self.forced_suffix = suffix.to_owned(); self } pub fn format(&self, value: f64) -> String { if value < 0.0 { return format!("-{}", self.format(value * -1.0)); } let scaled_value = self.scales.to_scaled_value(value); format!( "{:.width$}{}{}{}", scaled_value.value, self.separator, scaled_value.suffix, self.forced_units, width = self.decimals ) } pub fn parse(&self, value: &str) -> f64 { let v: Vec<&str> = value.split(&self.separator).collect(); let result = v.get(0).unwrap().parse::<f64>().unwrap(); let mut suffix = v.get(1).unwrap().to_string(); let new_len = suffix.len() - self.forced_units.len(); suffix.truncate(new_len); let magnitude_multiplier = self.scales.get_magnitude_multipler(&suffix); (result * magnitude_multiplier) } } impl Scales { pub fn new() -> Self { Scales::SI() } pub fn SI() -> Self { Scales { base: 1000, suffixes: [ "".to_owned(), "k".to_owned(), "M".to_owned(), "B".to_owned(), "T".to_owned(), "P".to_owned(), "E".to_owned(), "Z".to_owned(), "Y".to_owned(), ].to_vec(), } } pub fn Binary() -> Self { Scales { base: 1000, suffixes: [ "".to_owned(), "ki".to_owned(), "Mi".to_owned(), "Gi".to_owned(), "Ti".to_owned(), "Pi".to_owned(), "Ei".to_owned(), "Zi".to_owned(), "Yi".to_owned(), ].to_vec(), } } pub fn with_base(&mut self, base: u32) -> &mut Self { self.base = base; self } pub fn with_suffixes(&mut self, suffixes: Vec<String>) -> &mut Self { self.suffixes = Vec::new(); for suffix in suffixes { self.suffixes.push(suffix.to_owned()); } self } pub fn get_magnitude_multipler(&self, value: &str) -> f64 { let ndx = 0; for ndx in 0..self.suffixes.len() { println!("{}", self.suffixes[ndx]); if value == self.suffixes[ndx] { return self.base.pow(ndx as u32) as f64; } } return 0.0; } fn to_scaled_value(&self, value: f64) -> ScaledValue { let mut index: usize = 0; let mut _value: f64 = value; loop { if _value < (self.base as f64) { break; } _value /= self.base as f64; index += 1; } println!( "\t\t{}: {} {} --- {}", value, index, self.base.pow(index as u32), value / (self.base.pow(index as u32) as f64) ); ScaledValue { value: (value / self.base.pow((index) as u32) as f64) as f32, suffix: self.suffixes[index].to_owned(), } } }