]> git.scottworley.com Git - tablify/blob - src/lib.rs
Line numbers in error messages
[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: std::iter::Enumerate<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 {
20 input: input.enumerate(),
21 row: None,
22 }
23 }
24 }
25 impl<Input: Iterator<Item = Result<String, std::io::Error>>> Iterator for Reader<Input> {
26 type Item = Result<RowInput, std::io::Error>;
27 fn next(&mut self) -> Option<Self::Item> {
28 loop {
29 match self
30 .input
31 .next()
32 .map(|(n, r)| (n, r.map(|line| String::from(line.trim_end()))))
33 {
34 None => return Ok(std::mem::take(&mut self.row)).transpose(),
35 Some((_, Err(e))) => return Some(Err(e)),
36 Some((_, Ok(line))) if line.is_empty() && self.row.is_some() => {
37 return Ok(std::mem::take(&mut self.row)).transpose()
38 }
39 Some((_, Ok(line))) if line.is_empty() => {}
40 Some((n, Ok(line))) if line.starts_with(' ') => match &mut self.row {
41 None => {
42 return Some(Err(std::io::Error::other(format!(
43 "{}: Entry with no header",
44 n + 1
45 ))))
46 }
47 Some(ref mut row) => row.entries.push(String::from(line.trim())),
48 },
49 Some((_, Ok(line))) => {
50 let prev = std::mem::take(&mut self.row);
51 self.row = Some(RowInput {
52 label: line,
53 entries: vec![],
54 });
55 if prev.is_some() {
56 return Ok(prev).transpose();
57 }
58 }
59 }
60 }
61 }
62 }
63
64 #[cfg(test)]
65 fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<RowInput, std::io::Error>> {
66 Reader::new(std::io::BufReader::new(input).lines())
67 }
68
69 pub fn tablify(_input: &impl std::io::Read) -> String {
70 String::from("Hello, world!")
71 }
72
73 #[cfg(test)]
74 mod tests {
75 use super::*;
76
77 #[test]
78 fn test_read_rows() {
79 assert_eq!(
80 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
81 vec![RowInput {
82 label: String::from("foo"),
83 entries: vec![]
84 }]
85 );
86 assert_eq!(
87 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
88 vec![RowInput {
89 label: String::from("bar"),
90 entries: vec![]
91 }]
92 );
93 assert_eq!(
94 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
95 vec![
96 RowInput {
97 label: String::from("foo"),
98 entries: vec![]
99 },
100 RowInput {
101 label: String::from("bar"),
102 entries: vec![]
103 }
104 ]
105 );
106 assert_eq!(
107 read_rows(&b"foo\n bar\n"[..]).flatten().collect::<Vec<_>>(),
108 vec![RowInput {
109 label: String::from("foo"),
110 entries: vec![String::from("bar")]
111 }]
112 );
113 assert_eq!(
114 read_rows(&b"foo\n bar\n baz\n"[..])
115 .flatten()
116 .collect::<Vec<_>>(),
117 vec![RowInput {
118 label: String::from("foo"),
119 entries: vec![String::from("bar"), String::from("baz")]
120 }]
121 );
122 assert_eq!(
123 read_rows(&b"foo\n\nbar\n"[..])
124 .flatten()
125 .collect::<Vec<_>>(),
126 vec![
127 RowInput {
128 label: String::from("foo"),
129 entries: vec![]
130 },
131 RowInput {
132 label: String::from("bar"),
133 entries: vec![]
134 }
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 bar \n"[..])
154 .flatten()
155 .collect::<Vec<_>>(),
156 vec![RowInput {
157 label: String::from("foo"),
158 entries: vec![String::from("bar")]
159 }]
160 );
161
162 let bad = read_rows(&b" foo"[..]).next().unwrap();
163 assert!(bad.is_err());
164 assert!(format!("{bad:?}").contains("1: Entry with no header"));
165
166 let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
167 assert!(bad2.is_err());
168 assert!(format!("{bad2:?}").contains("3: Entry with no header"));
169 }
170 }