]> git.scottworley.com Git - tablify/blame - src/lib.rs
Start connecting stuff
[tablify] / src / lib.rs
CommitLineData
397ef957 1use std::collections::{HashMap, HashSet};
9dfa98b7 2use std::io::BufRead;
75bb888a
SW
3use std::iter::Iterator;
4
5#[derive(Debug, PartialEq, Eq)]
6struct RowInput {
7 label: String,
8 entries: Vec<String>,
9}
10
201b9ef3 11struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
8110b492 12 input: std::iter::Enumerate<Input>,
201b9ef3
SW
13 row: Option<RowInput>,
14}
15impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
201b9ef3 16 fn new(input: Input) -> Self {
8110b492
SW
17 Self {
18 input: input.enumerate(),
19 row: None,
20 }
201b9ef3
SW
21 }
22}
23impl<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 {
1f6bd845
SW
27 match self
28 .input
29 .next()
8110b492 30 .map(|(n, r)| (n, r.map(|line| String::from(line.trim_end()))))
1f6bd845 31 {
201b9ef3 32 None => return Ok(std::mem::take(&mut self.row)).transpose(),
8110b492
SW
33 Some((_, Err(e))) => return Some(Err(e)),
34 Some((_, Ok(line))) if line.is_empty() && self.row.is_some() => {
201b9ef3
SW
35 return Ok(std::mem::take(&mut self.row)).transpose()
36 }
8110b492
SW
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 }
201b9ef3
SW
45 Some(ref mut row) => row.entries.push(String::from(line.trim())),
46 },
8110b492 47 Some((_, Ok(line))) => {
201b9ef3
SW
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
201b9ef3
SW
62fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<RowInput, std::io::Error>> {
63 Reader::new(std::io::BufReader::new(input).lines())
75bb888a
SW
64}
65
58b5f36d
SW
66fn column_counts(rows: &[RowInput]) -> Vec<(usize, String)> {
67 let mut counts: Vec<_> = rows
68 .iter()
397ef957 69 .flat_map(|r| r.entries.iter().collect::<HashSet<_>>().into_iter())
58b5f36d
SW
70 .fold(HashMap::new(), |mut cs, e| {
71 cs.entry(String::from(e))
72 .and_modify(|n| *n += 1)
f272e502 73 .or_insert(1);
58b5f36d 74 cs
f272e502 75 })
58b5f36d
SW
76 .into_iter()
77 .map(|(col, n)| (n, col))
78 .collect();
79 counts.sort();
80 counts
f272e502
SW
81}
82
4b99fb70
SW
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
89pub 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!"))
ece97615 93}
75bb888a
SW
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn test_read_rows() {
101 assert_eq!(
201b9ef3 102 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
75bb888a
SW
103 vec![RowInput {
104 label: String::from("foo"),
105 entries: vec![]
106 }]
107 );
9dfa98b7 108 assert_eq!(
201b9ef3 109 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
9dfa98b7
SW
110 vec![RowInput {
111 label: String::from("bar"),
112 entries: vec![]
113 }]
114 );
2aa9ef94 115 assert_eq!(
201b9ef3 116 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
2aa9ef94
SW
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 );
201b9ef3
SW
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 );
1f6bd845
SW
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 );
201b9ef3
SW
183
184 let bad = read_rows(&b" foo"[..]).next().unwrap();
185 assert!(bad.is_err());
8110b492 186 assert!(format!("{bad:?}").contains("1: Entry with no header"));
201b9ef3
SW
187
188 let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
189 assert!(bad2.is_err());
8110b492 190 assert!(format!("{bad2:?}").contains("3: Entry with no header"));
75bb888a 191 }
f272e502
SW
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 ),
58b5f36d 201 vec![(1, String::from("bar")), (1, String::from("baz"))]
f272e502
SW
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 ),
58b5f36d 209 vec![(1, String::from("bar")), (2, String::from("baz"))]
f272e502 210 );
397ef957
SW
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 ),
58b5f36d 217 vec![(1, String::from("bar")), (2, String::from("baz"))]
397ef957 218 );
f272e502 219 }
75bb888a 220}