]> git.scottworley.com Git - tablify/blob - src/lib.rs
Don't count multiple entries in a single row in column counts
[tablify] / src / lib.rs
1 #[cfg(test)]
2 use std::collections::{HashMap, HashSet};
3 #[cfg(test)]
4 use std::io::BufRead;
5 #[cfg(test)]
6 use std::iter::Iterator;
7
8 #[derive(Debug, PartialEq, Eq)]
9 struct RowInput {
10 label: String,
11 entries: Vec<String>,
12 }
13
14 struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
15 input: std::iter::Enumerate<Input>,
16 row: Option<RowInput>,
17 }
18 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
19 #[cfg(test)]
20 fn new(input: Input) -> Self {
21 Self {
22 input: input.enumerate(),
23 row: None,
24 }
25 }
26 }
27 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> {
28 type Item = Result<RowInput, std::io::Error>;
29 fn next(&mut self) -> Option<Self::Item> {
30 loop {
31 match self
32 .input
33 .next()
34 .map(|(n, r)| (n, r.map(|line| String::from(line.trim_end()))))
35 {
36 None => return Ok(std::mem::take(&mut self.row)).transpose(),
37 Some((_, Err(e))) => return Some(Err(e)),
38 Some((_, Ok(line))) if line.is_empty() && self.row.is_some() => {
39 return Ok(std::mem::take(&mut self.row)).transpose()
40 }
41 Some((_, Ok(line))) if line.is_empty() => {}
42 Some((n, Ok(line))) if line.starts_with(' ') => match &mut self.row {
43 None => {
44 return Some(Err(std::io::Error::other(format!(
45 "{}: Entry with no header",
46 n + 1
47 ))))
48 }
49 Some(ref mut row) => row.entries.push(String::from(line.trim())),
50 },
51 Some((_, Ok(line))) => {
52 let prev = std::mem::take(&mut self.row);
53 self.row = Some(RowInput {
54 label: line,
55 entries: vec![],
56 });
57 if prev.is_some() {
58 return Ok(prev).transpose();
59 }
60 }
61 }
62 }
63 }
64 }
65
66 #[cfg(test)]
67 fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<RowInput, std::io::Error>> {
68 Reader::new(std::io::BufReader::new(input).lines())
69 }
70
71 #[cfg(test)]
72 fn column_counts(rows: &[RowInput]) -> HashMap<String, usize> {
73 rows.iter()
74 .flat_map(|r| r.entries.iter().collect::<HashSet<_>>().into_iter())
75 .fold(HashMap::new(), |mut counts, e| {
76 counts
77 .entry(String::from(e))
78 .and_modify(|c| *c += 1)
79 .or_insert(1);
80 counts
81 })
82 }
83
84 pub fn tablify(_input: &impl std::io::Read) -> String {
85 String::from("Hello, world!")
86 }
87
88 #[cfg(test)]
89 mod tests {
90 use super::*;
91
92 #[test]
93 fn test_read_rows() {
94 assert_eq!(
95 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
96 vec![RowInput {
97 label: String::from("foo"),
98 entries: vec![]
99 }]
100 );
101 assert_eq!(
102 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
103 vec![RowInput {
104 label: String::from("bar"),
105 entries: vec![]
106 }]
107 );
108 assert_eq!(
109 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
110 vec![
111 RowInput {
112 label: String::from("foo"),
113 entries: vec![]
114 },
115 RowInput {
116 label: String::from("bar"),
117 entries: vec![]
118 }
119 ]
120 );
121 assert_eq!(
122 read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(),
123 vec![RowInput {
124 label: String::from("foo"),
125 entries: vec![String::from("bar")]
126 }]
127 );
128 assert_eq!(
129 read_rows(&b"foo\n bar\n baz\n"[..])
130 .flatten()
131 .collect::<Vec<_>>(),
132 vec![RowInput {
133 label: String::from("foo"),
134 entries: vec![String::from("bar"), String::from("baz")]
135 }]
136 );
137 assert_eq!(
138 read_rows(&b"foo\n\nbar\n"[..])
139 .flatten()
140 .collect::<Vec<_>>(),
141 vec![
142 RowInput {
143 label: String::from("foo"),
144 entries: vec![]
145 },
146 RowInput {
147 label: String::from("bar"),
148 entries: vec![]
149 }
150 ]
151 );
152 assert_eq!(
153 read_rows(&b"foo\n \nbar\n"[..])
154 .flatten()
155 .collect::<Vec<_>>(),
156 vec![
157 RowInput {
158 label: String::from("foo"),
159 entries: vec![]
160 },
161 RowInput {
162 label: String::from("bar"),
163 entries: vec![]
164 }
165 ]
166 );
167 assert_eq!(
168 read_rows(&b"foo \n bar \n"[..])
169 .flatten()
170 .collect::<Vec<_>>(),
171 vec![RowInput {
172 label: String::from("foo"),
173 entries: vec![String::from("bar")]
174 }]
175 );
176
177 let bad = read_rows(&b" foo"[..]).next().unwrap();
178 assert!(bad.is_err());
179 assert!(format!("{bad:?}").contains("1: Entry with no header"));
180
181 let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
182 assert!(bad2.is_err());
183 assert!(format!("{bad2:?}").contains("3: Entry with no header"));
184 }
185
186 #[test]
187 fn test_column_counts() {
188 assert_eq!(
189 column_counts(
190 &read_rows(&b"foo\n bar\n baz\n"[..])
191 .collect::<Result<Vec<_>, _>>()
192 .unwrap()
193 ),
194 HashMap::from([(String::from("bar"), 1), (String::from("baz"), 1)])
195 );
196 assert_eq!(
197 column_counts(
198 &read_rows(&b"foo\n bar\n baz\nquux\n baz"[..])
199 .collect::<Result<Vec<_>, _>>()
200 .unwrap()
201 ),
202 HashMap::from([(String::from("bar"), 1), (String::from("baz"), 2)])
203 );
204 assert_eq!(
205 column_counts(
206 &read_rows(&b"foo\n bar\n bar\n baz\n bar\nquux\n baz"[..])
207 .collect::<Result<Vec<_>, _>>()
208 .unwrap()
209 ),
210 HashMap::from([(String::from("bar"), 1), (String::from("baz"), 2)])
211 );
212 }
213 }