]>
Commit | Line | Data |
---|---|---|
75bb888a | 1 | #[cfg(test)] |
9dfa98b7 SW |
2 | use std::io::BufRead; |
3 | #[cfg(test)] | |
75bb888a SW |
4 | use std::iter::Iterator; |
5 | ||
6 | #[derive(Debug, PartialEq, Eq)] | |
7 | struct RowInput { | |
8 | label: String, | |
9 | entries: Vec<String>, | |
10 | } | |
11 | ||
201b9ef3 SW |
12 | struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> { |
13 | input: Input, | |
14 | row: Option<RowInput>, | |
15 | } | |
16 | impl<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 | } | |
22 | impl<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 { | |
1f6bd845 SW |
26 | match self |
27 | .input | |
28 | .next() | |
29 | .map(|r| r.map(|line| String::from(line.trim_end()))) | |
30 | { | |
201b9ef3 SW |
31 | None => return Ok(std::mem::take(&mut self.row)).transpose(), |
32 | Some(Err(e)) => return Some(Err(e)), | |
33 | Some(Ok(line)) if line.is_empty() && self.row.is_some() => { | |
34 | return Ok(std::mem::take(&mut self.row)).transpose() | |
35 | } | |
36 | Some(Ok(line)) if line.is_empty() => {} | |
37 | Some(Ok(line)) if line.starts_with(' ') => match &mut self.row { | |
38 | None => return Some(Err(std::io::Error::other("Entry with no header"))), | |
39 | Some(ref mut row) => row.entries.push(String::from(line.trim())), | |
40 | }, | |
41 | Some(Ok(line)) => { | |
42 | let prev = std::mem::take(&mut self.row); | |
43 | self.row = Some(RowInput { | |
44 | label: line, | |
45 | entries: vec![], | |
46 | }); | |
47 | if prev.is_some() { | |
48 | return Ok(prev).transpose(); | |
49 | } | |
50 | } | |
51 | } | |
52 | } | |
53 | } | |
54 | } | |
55 | ||
75bb888a | 56 | #[cfg(test)] |
201b9ef3 SW |
57 | fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<RowInput, std::io::Error>> { |
58 | Reader::new(std::io::BufReader::new(input).lines()) | |
75bb888a SW |
59 | } |
60 | ||
ece97615 SW |
61 | pub fn tablify(_input: &impl std::io::Read) -> String { |
62 | String::from("Hello, world!") | |
63 | } | |
75bb888a SW |
64 | |
65 | #[cfg(test)] | |
66 | mod tests { | |
67 | use super::*; | |
68 | ||
69 | #[test] | |
70 | fn test_read_rows() { | |
71 | assert_eq!( | |
201b9ef3 | 72 | read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(), |
75bb888a SW |
73 | vec![RowInput { |
74 | label: String::from("foo"), | |
75 | entries: vec![] | |
76 | }] | |
77 | ); | |
9dfa98b7 | 78 | assert_eq!( |
201b9ef3 | 79 | read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(), |
9dfa98b7 SW |
80 | vec![RowInput { |
81 | label: String::from("bar"), | |
82 | entries: vec![] | |
83 | }] | |
84 | ); | |
2aa9ef94 | 85 | assert_eq!( |
201b9ef3 | 86 | read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(), |
2aa9ef94 SW |
87 | vec![ |
88 | RowInput { | |
89 | label: String::from("foo"), | |
90 | entries: vec![] | |
91 | }, | |
92 | RowInput { | |
93 | label: String::from("bar"), | |
94 | entries: vec![] | |
95 | } | |
96 | ] | |
97 | ); | |
201b9ef3 SW |
98 | assert_eq!( |
99 | read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(), | |
100 | vec![RowInput { | |
101 | label: String::from("foo"), | |
102 | entries: vec![String::from("bar")] | |
103 | }] | |
104 | ); | |
105 | assert_eq!( | |
106 | read_rows(&b"foo\n bar\n baz\n"[..]) | |
107 | .flatten() | |
108 | .collect::<Vec<_>>(), | |
109 | vec![RowInput { | |
110 | label: String::from("foo"), | |
111 | entries: vec![String::from("bar"), String::from("baz")] | |
112 | }] | |
113 | ); | |
114 | assert_eq!( | |
115 | read_rows(&b"foo\n\nbar\n"[..]) | |
116 | .flatten() | |
117 | .collect::<Vec<_>>(), | |
118 | vec![ | |
119 | RowInput { | |
120 | label: String::from("foo"), | |
121 | entries: vec![] | |
122 | }, | |
123 | RowInput { | |
124 | label: String::from("bar"), | |
125 | entries: vec![] | |
126 | } | |
127 | ] | |
128 | ); | |
1f6bd845 SW |
129 | assert_eq!( |
130 | read_rows(&b"foo\n \nbar\n"[..]) | |
131 | .flatten() | |
132 | .collect::<Vec<_>>(), | |
133 | vec![ | |
134 | RowInput { | |
135 | label: String::from("foo"), | |
136 | entries: vec![] | |
137 | }, | |
138 | RowInput { | |
139 | label: String::from("bar"), | |
140 | entries: vec![] | |
141 | } | |
142 | ] | |
143 | ); | |
144 | assert_eq!( | |
145 | read_rows(&b"foo \n bar \n"[..]) | |
146 | .flatten() | |
147 | .collect::<Vec<_>>(), | |
148 | vec![RowInput { | |
149 | label: String::from("foo"), | |
150 | entries: vec![String::from("bar")] | |
151 | }] | |
152 | ); | |
201b9ef3 SW |
153 | |
154 | let bad = read_rows(&b" foo"[..]).next().unwrap(); | |
155 | assert!(bad.is_err()); | |
156 | assert!(format!("{bad:?}").contains("Entry with no header")); | |
157 | ||
158 | let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap(); | |
159 | assert!(bad2.is_err()); | |
160 | assert!(format!("{bad2:?}").contains("Entry with no header")); | |
75bb888a SW |
161 | } |
162 | } |