7WMS7RLPXYF47WXLZXY25VCUJOGL7XIDVQK6FGT27EHPG2JICDRQC
macro_rules! commit { () => {{
if sections.contains_key(§ion.0) {
return Err(Error::SectionOverwritten(section.0));
}
sections.insert(std::mem::take(&mut section.0), std::mem::take(&mut section.1));
}} }
macro_rules! commit {
() => {{
if sections.contains_key(§ion.0) {
return Err(Error::SectionOverwritten(section.0));
}
sections.insert(
std::mem::take(&mut section.0),
std::mem::take(&mut section.1),
);
}};
}
return Err(Error::InvalidItem(line.to_string()));
return Err(mkerr());
}
let mut it = line.chars().peekable();
// parse premarkers
let mut premarkers = Vec::new();
while it.next_if(|i| *i == '[').is_some() {
let premarker = it.next().ok_or_else(mkerr)?;
let _ = it.next().ok_or_else(mkerr)?;
let c2 = it.next().ok_or_else(mkerr)?;
if c2 != ']' {
return Err(mkerr());
}
premarkers.push(premarker);
while it.next_if(|i| i.is_whitespace()).is_some() {}
let mut it = line.chars();
let c1 = it.next().unwrap();
let premarker = it.next().unwrap();
let _ = it.next().unwrap();
let c2 = it.next().unwrap();
if c1 != '[' || c2 != ']' {
return Err(Error::InvalidItem(line.to_string()));
if premarkers.is_empty() {
return Err(mkerr());
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,
sections.insert(
String::new(),
Section {
summary: ItemData::default(),
items: vec![
(
ItemData {
premarkers: vec![' '],
multiplier: 1,
},
"15 Ruß".to_string(),
),
(
ItemData {
premarkers: vec![' '],
multiplier: 132,
},
"Wattestäbe".to_string(),
),
(
ItemData {
premarkers: vec!['#'],
multiplier: 0,
},
"Handtuch".to_string(),
),
],
items: vec![
(ItemData { premarker: ' ', multiplier: 1 }, "Ranke".to_string()),
(ItemData { premarker: '*', multiplier: 1 }, "5 x Metrik + BNaumbnd".to_string()),
],
});
);
sections.insert(
"Wachsbaum ...".to_string(),
Section {
summary: ItemData {
premarkers: vec!['-'],
multiplier: 5000,
},
items: vec![
(
ItemData {
premarkers: vec![' '],
multiplier: 1,
},
"Ranke".to_string(),
),
(
ItemData {
premarkers: vec!['*'],
multiplier: 1,
},
"5 x Metrik + BNaumbnd".to_string(),
),
],
},
);
assert_eq!("Packl (xyz) ...\n".parse::<PackList>(), Err(Error::DataAfterRangeSpec));
assert_eq!("P\n[ ] # 1\n[ ] # 1\n".parse::<PackList>(), Err(Error::SectionOverwritten("1".to_string())));
assert_eq!("P\n[ ]".parse::<PackList>(), Err(Error::InvalidItem("[ ]".to_string())));
assert_eq!(
"Packl (xyz) ...\n".parse::<PackList>(),
Err(Error::DataAfterRangeSpec)
);
assert_eq!(
"P\n[ ] # 1\n[ ] # 1\n".parse::<PackList>(),
Err(Error::SectionOverwritten("1".to_string()))
);
assert_eq!(
"P\n[ ]".parse::<PackList>(),
Err(Error::InvalidItem("[ ]".to_string()))
);
}
#[test]
fn ex1() {
let inp1 = r#"Packliste Ölland:
[* ] [ _] [. ] 15 132x Ruß
[* ] [ _] [. ] 132x Ruß
"#;
let inp2 = r#"Packliste Ölland
[* ] [ ] [. ] 15 132x Ruß
[* ] [ ] [. ] 132x Ruß
"#;
let mut sections = BTreeMap::new();
sections.insert(
String::new(),
Section {
summary: ItemData::default(),
items: vec![
(
ItemData {
premarkers: vec!['*', ' ', '.'],
multiplier: 1,
},
"15 132x Ruß".to_string(),
),
(
ItemData {
premarkers: vec!['*', ' ', '.'],
multiplier: 132,
},
"Ruß".to_string(),
),
],
},
);
let res = PackList {
name: "Packliste Ölland".to_string(),
range: String::new(),
sections,
};
assert_eq!(inp1.parse::<PackList>().unwrap(), res);
assert_eq!(inp2.parse::<PackList>().unwrap(), res);
assert_eq!(res.to_string(), inp2);
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,
sections.insert(
String::new(),
Section {
summary: ItemData::default(),
items: vec![
(
ItemData {
premarkers: vec![' '],
multiplier: 1,
},
"15 Ruß".to_string(),
),
(
ItemData {
premarkers: vec![' '],
multiplier: 132,
},
"Wattestäbe".to_string(),
),
(
ItemData {
premarkers: vec!['#'],
multiplier: 0,
},
"Handtuch".to_string(),
),
],
items: vec![
(ItemData { premarker: ' ', multiplier: 1 }, "Ranke".to_string()),
(ItemData { premarker: '*', multiplier: 1 }, "5 x Metrik + BNaumbnd".to_string()),
],
});
);
sections.insert(
"Wachsbaum ...".to_string(),
Section {
summary: ItemData {
premarkers: vec!['-'],
multiplier: 5000,
},
items: vec![
(
ItemData {
premarkers: vec![' '],
multiplier: 1,
},
"Ranke".to_string(),
),
(
ItemData {
premarkers: vec!['*'],
multiplier: 1,
},
"5 x Metrik + BNaumbnd".to_string(),
),
],
},
);
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,
sections2.insert(
String::new(),
Section {
summary: ItemData::default(),
items: vec![
(
ItemData {
premarkers: vec![' '],
multiplier: 1,
},
"15 Ruß".to_string(),
),
(
ItemData {
premarkers: vec![' '],
multiplier: 132,
},
"Wattestäbe".to_string(),
),
(
ItemData {
premarkers: vec![' '],
multiplier: 0,
},
"Handtuch".to_string(),
),
],
},
);
sections2.insert(
"Wachsbaum ...".to_string(),
Section {
summary: ItemData {
premarkers: vec![' '],
multiplier: 5000,
},
items: vec![
(
ItemData {
premarkers: vec![' '],
multiplier: 1,
},
"Ranke".to_string(),
),
(
ItemData {
premarkers: vec![' '],
multiplier: 1,
},
"5 x Metrik + BNaumbnd".to_string(),
),
],