1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use std::env::args_os;
use std::iter::ExactSizeIterator;
use std::io::{self, Read};
use std::io::{BufReader, BufRead};
use std::fs::File;
use std::path::Path;
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::convert::From;
#[derive(Debug)]
pub struct FailReadFileError {
pub inner: io::Error,
pub filename: String
}
impl Display for FailReadFileError {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
writeln!(f, "could not read file {}", self.filename)?;
write!(f, "caused by: {}", self.inner)?;
Ok(())
}
}
impl Error for FailReadFileError {
fn description(&self) -> &str {
"failed to read file"
}
fn cause(&self) -> Option<&Error> {
Some(&self.inner)
}
}
#[derive(Debug)]
pub struct InputError {
pub badfiles: Vec<FailReadFileError>
}
impl Display for InputError {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
for e in &self.badfiles {
writeln!(f, "{}", e)?;
}
Ok(())
}
}
impl Error for InputError {
fn description(&self) -> &str {
"failed to read one or more files"
}
fn cause(&self) -> Option<&Error> {
let first = self.badfiles.first();
match first {
Some(err) => Some(err),
None => None
}
}
}
impl From<Vec<FailReadFileError>> for InputError {
fn from(err: Vec<FailReadFileError>) -> Self {
InputError { badfiles: err }
}
}
trait TryIterator {
type Item;
type JIter: ExactSizeIterator<Item=Self::Item>;
fn attempt_map<F, T, E>(self, mapper: F) -> Result<Vec<T>, Vec<E>> where
F: Fn(Self::Item) -> Result<T, E>;
}
impl<I> TryIterator for I where
I: ExactSizeIterator
{
type Item = I::Item;
type JIter = I;
fn attempt_map<F, T, E>(self, mapper: F) -> Result<Vec<T>, Vec<E>> where
F: Fn(Self::Item) -> Result<T, E>
{
let mut any_failure = false;
let mut successes = Vec::new();
let mut failures = Vec::new();
for obj in self {
match mapper(obj) {
Ok(output) => {
if !any_failure {
successes.push(output);
}
},
Err(err) => {
any_failure = true;
failures.push(err);
}
};
}
if any_failure { Err(failures) } else { Ok(successes) }
}
}
pub type Lines = io::Lines<BufReader<Box<Read>>>;
pub fn argf_lines() -> Result<Lines, InputError> {
let chained = argf()?;
let buffered = BufReader::new(chained);
Ok(buffered.lines())
}
pub fn argf() -> Result<Box<Read>, InputError> {
let args = args_os().skip(1);
input(args)
}
pub fn input_lines<I, J, S>(inputs: I) -> Result<Lines, InputError> where
I: IntoIterator<Item=S, IntoIter=J>,
J: ExactSizeIterator<Item=S>,
S: AsRef<Path>
{
let chained = input(inputs)?;
let buffered = BufReader::new(chained);
Ok(buffered.lines())
}
pub fn input<I, J, S>(inputs: I) -> Result<Box<Read>, InputError> where
I: IntoIterator<Item=S, IntoIter=J>,
J: ExactSizeIterator<Item=S>,
S: AsRef<Path>
{
let iter = inputs.into_iter();
if iter.len() == 0 {
Ok(Box::new(io::stdin()))
} else {
let reads = iter.attempt_map(|path| from_arg(path.as_ref()))?;
Ok(chain_all_reads(reads))
}
}
fn chain_all_reads<I>(reads: I) -> Box<Read> where
I: IntoIterator<Item=Box<Read>>
{
reads.into_iter().fold(Box::new(io::empty()), |read, next| {
Box::new(read.chain(next))
})
}
fn from_arg<'a>(arg: &'a Path) -> Result<Box<Read>, FailReadFileError> {
let str_repr = arg.to_string_lossy();
if str_repr == "-" {
Ok(Box::new(io::stdin()))
} else {
let file = File::open(arg).map_err(|err| {
FailReadFileError {
inner: err,
filename: arg.to_string_lossy().to_string()
}
})?;
Ok(Box::new(file))
}
}