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