]> git.scottworley.com Git - tablify/blame - src/lib.rs
Proper log parsing
[tablify] / src / lib.rs
CommitLineData
75bb888a 1#[cfg(test)]
9dfa98b7
SW
2use std::io::BufRead;
3#[cfg(test)]
75bb888a
SW
4use std::iter::Iterator;
5
6#[derive(Debug, PartialEq, Eq)]
7struct RowInput {
8 label: String,
9 entries: Vec<String>,
10}
11
201b9ef3
SW
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
75bb888a 52#[cfg(test)]
201b9ef3
SW
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())
75bb888a
SW
55}
56
ece97615
SW
57pub fn tablify(_input: &impl std::io::Read) -> String {
58 String::from("Hello, world!")
59}
75bb888a
SW
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_read_rows() {
67 assert_eq!(
201b9ef3 68 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
75bb888a
SW
69 vec![RowInput {
70 label: String::from("foo"),
71 entries: vec![]
72 }]
73 );
9dfa98b7 74 assert_eq!(
201b9ef3 75 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
9dfa98b7
SW
76 vec![RowInput {
77 label: String::from("bar"),
78 entries: vec![]
79 }]
80 );
2aa9ef94 81 assert_eq!(
201b9ef3 82 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
2aa9ef94
SW
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 );
201b9ef3
SW
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"));
75bb888a
SW
133 }
134}