use std:: collections::HashMap;
#[derive(Clone,Debug,PartialEq, Eq, PartialOrd, Ord)]
pub enum Unit {
ElementGroup {
count: u32,
units: Vec<Unit>
},
Element {
count: u32,
symbol: String
}
}
impl Unit {
pub fn get_composition(&self) -> HashMap<&String, u32> {
let mut map = HashMap::new();
match self {
Self::Element {count,symbol} => {
let v = map.entry(symbol).or_insert(0);
*v += count;
},
Self::ElementGroup {count,units} => {
for unit in units {
let new_map = unit.get_composition();
for (k,_v) in new_map {
let v = map.entry(k).or_insert(0);
*v += _v * count
}
}
}
}
map
}
}