]> git.scottworley.com Git - tablify/blame - src/lib.rs
Line numbers in error messages
[tablify] / src / lib.rs
CommitLineData
75bb888a 1#[cfg(test)]
9dfa98b7
SW
2use std::io::BufRead;
3#[cfg(test)]
75bb888a
SW
4use std::iter::Iterator;
5
6#[derive(Debug, PartialEq, Eq)]
7struct RowInput {
8 label: String,
9 entries: Vec<String>,
10}
11
201b9ef3 12struct Reader<Input: Iterator<Item = Result<String, std::io::Error>>> {
8110b492 13 input: std::iter::Enumerate<Input>,
201b9ef3
SW
14 row: Option<RowInput>,
15}
16impl<Input: Iterator<Item = Result<String, std::io::Error>>> Reader<Input> {
17 #[cfg(test)]
18 fn new(input: Input) -> Self {
8110b492
SW
19 Self {
20 input: input.enumerate(),
21 row: None,
22 }
201b9ef3
SW
23 }
24}
25impl<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 {
1f6bd845
SW
29 match self
30 .input
31 .next()
8110b492 32 .map(|(n, r)| (n, r.map(|line| String::from(line.trim_end()))))
1f6bd845 33 {
201b9ef3 34 None => return Ok(std::mem::take(&mut self.row)).transpose(),
8110b492
SW
35 Some((_, Err(e))) => return Some(Err(e)),
36 Some((_, Ok(line))) if line.is_empty() && self.row.is_some() => {
201b9ef3
SW
37 return Ok(std::mem::take(&mut self.row)).transpose()
38 }
8110b492
SW
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 }
201b9ef3
SW
47 Some(ref mut row) => row.entries.push(String::from(line.trim())),
48 },
8110b492 49 Some((_, Ok(line))) => {
201b9ef3
SW
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
75bb888a 64#[cfg(test)]
201b9ef3
SW
65fn read_rows(input: impl std::io::Read) -> impl Iterator<Item = Result<RowInput, std::io::Error>> {
66 Reader::new(std::io::BufReader::new(input).lines())
75bb888a
SW
67}
68
ece97615
SW
69pub fn tablify(_input: &impl std::io::Read) -> String {
70 String::from("Hello, world!")
71}
75bb888a
SW
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn test_read_rows() {
79 assert_eq!(
201b9ef3 80 read_rows(&b"foo"[..]).flatten().collect::<Vec<_>>(),
75bb888a
SW
81 vec![RowInput {
82 label: String::from("foo"),
83 entries: vec![]
84 }]
85 );
9dfa98b7 86 assert_eq!(
201b9ef3 87 read_rows(&b"bar"[..]).flatten().collect::<Vec<_>>(),
9dfa98b7
SW
88 vec![RowInput {
89 label: String::from("bar"),
90 entries: vec![]
91 }]
92 );
2aa9ef94 93 assert_eq!(
201b9ef3 94 read_rows(&b"foo\nbar\n"[..]).flatten().collect::<Vec<_>>(),
2aa9ef94
SW
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 );
201b9ef3
SW
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 );
1f6bd845
SW
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 );
201b9ef3
SW
161
162 let bad = read_rows(&b" foo"[..]).next().unwrap();
163 assert!(bad.is_err());
8110b492 164 assert!(format!("{bad:?}").contains("1: Entry with no header"));
201b9ef3
SW
165
166 let bad2 = read_rows(&b"foo\n\n bar"[..]).nth(1).unwrap();
167 assert!(bad2.is_err());
8110b492 168 assert!(format!("{bad2:?}").contains("3: Entry with no header"));
75bb888a
SW
169 }
170}