]> git.scottworley.com Git - tablify/blob - src/lib.rs
72a3add0ef2f9edccc34cc6f39b9ce8df9f79b56
[tablify] / src / lib.rs
1 #[cfg(test)]
2 use std::io::BufRead;
3 #[cfg(test)]
4 use std::iter::Iterator;
5
6 #[derive(Debug, PartialEq, Eq)]
7 struct RowInput {
8 label: String,
9 entries: Vec<String>,
10 }
11
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 {
26 match self
27 .input
28 .next()
29 .map(|r| r.map(|line| String::from(line.trim_end())))
30 {
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
56 #[cfg(test)]
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())
59 }
60
61 pub fn tablify(_input: &impl std::io::Read) -> String {
62 String::from("Hello, world!")
63 }
64
65 #[cfg(test)]
66 mod tests {
67 use super::*;
68
69 #[test]
70 fn test_read_rows() {
71 assert_eq!(
72 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
73 vec![RowInput {
74 label: String::from("foo"),
75 entries: vec![]
76 }]
77 );
78 assert_eq!(
79 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
80 vec![RowInput {
81 label: String::from("bar"),
82 entries: vec![]
83 }]
84 );
85 assert_eq!(
86 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
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 );
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 );
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 );
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"));
161 }
162 }