use std::collections::BTreeMap;
mod serial;
pub use serial::Error as ParseError;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PackList {
pub name: String,
pub range: String,
pub sections: BTreeMap<String, Section>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Section {
pub summary: ItemData,
pub items: Vec<(ItemData, String)>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ItemData {
pub premarker: char,
pub multiplier: u32,
}
impl Default for ItemData {
fn default() -> Self {
Self {
premarker: ' ',
multiplier: 1,
}
}
}
impl PackList {
pub fn reset_premarkers(&mut self) {
self.sections.values_mut().for_each(|sd| sd.reset_premarkers());
}
}
impl Section {
pub fn reset_premarkers(&mut self) {
self.summary.premarker = ' ';
self.items.iter_mut().for_each(|item| item.0.premarker = ' ');
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ex0rs() {
let mut sections = BTreeMap::new();
sections.insert(String::new(), Section {
summary: ItemData::default(),
items: vec![
(ItemData { premarker: ' ', multiplier: 1 }, "15 Ruß".to_string()),
(ItemData { premarker: ' ', multiplier: 132 }, "Wattestäbe".to_string()),
(ItemData { premarker: '#', multiplier: 0 }, "Handtuch".to_string()),
],
});
sections.insert("Wachsbaum ...".to_string(), Section {
summary: ItemData {
premarker: '-',
multiplier: 5000,
},
items: vec![
(ItemData { premarker: ' ', multiplier: 1 }, "Ranke".to_string()),
(ItemData { premarker: '*', multiplier: 1 }, "5 x Metrik + BNaumbnd".to_string()),
],
});
let mut res = PackList {
name: "Packliste Ölland".to_string(),
range: "80 Tage, 50 Nächtsle".to_string(),
sections,
};
let mut sections2 = BTreeMap::new();
sections2.insert(String::new(), Section {
summary: ItemData::default(),
items: vec![
(ItemData { premarker: ' ', multiplier: 1 }, "15 Ruß".to_string()),
(ItemData { premarker: ' ', multiplier: 132 }, "Wattestäbe".to_string()),
(ItemData { premarker: ' ', multiplier: 0 }, "Handtuch".to_string()),
],
});
sections2.insert("Wachsbaum ...".to_string(), Section {
summary: ItemData {
premarker: ' ',
multiplier: 5000,
},
items: vec![
(ItemData { premarker: ' ', multiplier: 1 }, "Ranke".to_string()),
(ItemData { premarker: ' ', multiplier: 1 }, "5 x Metrik + BNaumbnd".to_string()),
],
});
let res2 = PackList {
name: "Packliste Ölland".to_string(),
range: "80 Tage, 50 Nächtsle".to_string(),
sections: sections2,
};
res.reset_premarkers();
assert_eq!(res, res2);
}
}