]> git.scottworley.com Git - tablify/blame_incremental - src/lib.rs
Proper log parsing
[tablify] / src / lib.rs
... / ...
CommitLineData
1#[cfg(test)]
2use std::io::BufRead;
3#[cfg(test)]
4use std::iter::Iterator;
5
6#[derive(Debug, PartialEq, Eq)]
7struct RowInput {
8 label: String,
9 entries: Vec<String>,
10}
11
12struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
13 input: Input,
14 row: Option<RowInput>,
15}
16impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
17 #[cfg(test)]
18 fn new(input: Input) -> Self {
19 Self { input, row: None }
20 }
21}
22impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> {
23 type Item = Result<RowInput, std::io::Error>;
24 fn next(&mut self) -> Option<Self::Item> {
25 loop {
26 match self.input.next() {
27 None => return Ok(std::mem::take(&mut self.row)).transpose(),
28 Some(Err(e)) => return Some(Err(e)),
29 Some(Ok(line)) if line.is_empty() && self.row.is_some() => {
30 return Ok(std::mem::take(&mut self.row)).transpose()
31 }
32 Some(Ok(line)) if line.is_empty() => {}
33 Some(Ok(line)) if line.starts_with(' ') => match &mut self.row {
34 None => return Some(Err(std::io::Error::other("Entry with no header"))),
35 Some(ref mut row) => row.entries.push(String::from(line.trim())),
36 },
37 Some(Ok(line)) => {
38 let prev = std::mem::take(&mut self.row);
39 self.row = Some(RowInput {
40 label: line,
41 entries: vec![],
42 });
43 if prev.is_some() {
44 return Ok(prev).transpose();
45 }
46 }
47 }
48 }
49 }
50}
51
52#[cfg(test)]
53fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<RowInput, std::io::Error>> {
54 Reader::new(std::io::BufReader::new(input).lines())
55}
56
57pub fn tablify(_input: &impl std::io::Read) -> String {
58 String::from("Hello, world!")
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_read_rows() {
67 assert_eq!(
68 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
69 vec![RowInput {
70 label: String::from("foo"),
71 entries: vec![]
72 }]
73 );
74 assert_eq!(
75 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
76 vec![RowInput {
77 label: String::from("bar"),
78 entries: vec![]
79 }]
80 );
81 assert_eq!(
82 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
83 vec![
84 RowInput {
85 label: String::from("foo"),
86 entries: vec![]
87 },
88 RowInput {
89 label: String::from("bar"),
90 entries: vec![]
91 }
92 ]
93 );
94 assert_eq!(
95 read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(),
96 vec![RowInput {
97 label: String::from("foo"),
98 entries: vec![String::from("bar")]
99 }]
100 );
101 assert_eq!(
102 read_rows(&b"foo\n bar\n baz\n"[..])
103 .flatten()
104 .collect::<Vec<_>>(),
105 vec![RowInput {
106 label: String::from("foo"),
107 entries: vec![String::from("bar"), String::from("baz")]
108 }]
109 );
110 assert_eq!(
111 read_rows(&b"foo\n\nbar\n"[..])
112 .flatten()
113 .collect::<Vec<_>>(),
114 vec![
115 RowInput {
116 label: String::from("foo"),
117 entries: vec![]
118 },
119 RowInput {
120 label: String::from("bar"),
121 entries: vec![]
122 }
123 ]
124 );
125
126 let bad = read_rows(&b" foo"[..]).next().unwrap();
127 assert!(bad.is_err());
128 assert!(format!("{bad:?}").contains("Entry with no header"));
129
130 let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
131 assert!(bad2.is_err());
132 assert!(format!("{bad2:?}").contains("Entry with no header"));
133 }
134}