+struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
+ input: Input,
+ row: Option<RowInput>,
+}
+impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
+ #[cfg(test)]
+ fn new(input: Input) -> Self {
+ Self { input, row: None }
+ }
+}
+impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> {
+ type Item = Result<RowInput, std::io::Error>;
+ fn next(&mut self) -> Option<Self::Item> {
+ loop {
+ match self.input.next() {
+ None => return Ok(std::mem::take(&mut self.row)).transpose(),
+ Some(Err(e)) => return Some(Err(e)),
+ Some(Ok(line)) if line.is_empty() && self.row.is_some() => {
+ return Ok(std::mem::take(&mut self.row)).transpose()
+ }
+ Some(Ok(line)) if line.is_empty() => {}
+ Some(Ok(line)) if line.starts_with(' ') => match &mut self.row {
+ None => return Some(Err(std::io::Error::other("Entry with no header"))),
+ Some(ref mut row) => row.entries.push(String::from(line.trim())),
+ },
+ Some(Ok(line)) => {
+ let prev = std::mem::take(&mut self.row);
+ self.row = Some(RowInput {
+ label: line,
+ entries: vec![],
+ });
+ if prev.is_some() {
+ return Ok(prev).transpose();
+ }
+ }
+ }
+ }
+ }
+}
+