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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use std::io;
use std::path::Path;
use crate::chopper::chopper::Source;
use crate::error::{CliResult, Error};
use crate::input::input::{Input, InputFormat, InputType};
use crate::source::csv_configs::CSVInputConfig;
use crate::source::decompress::{self, DecompressionFormat};
use crate::source::{
csv_factory::CSVFactory, dc_factory::DCFactory, source_factory::SourceFactory,
};
use crate::transport::{file::FileInput, http::Http, transport_factory::TransportFactory};
use crate::util::preview::Preview;
use crate::util::reader::ChopperBufPreviewer;
pub struct InputFactory {
transport_factories: Vec<Box<dyn TransportFactory>>,
source_factories: Vec<Box<dyn SourceFactory>>,
}
#[derive(Clone, Debug)]
enum FormatAutodetectResult {
Detected,
NotDetected,
}
#[derive(Clone, Debug)]
enum Format {
UserSpecified(String),
DetectUsingFileNameThenContents(String),
DetectUsingFileContents,
}
impl InputFactory {
pub fn new_without_csv(
user_source_factories: Option<Vec<Box<dyn SourceFactory>>>,
user_transport_factories: Option<Vec<Box<dyn TransportFactory>>>,
) -> CliResult<Self> {
Self::new_with_optional_csv(None, user_source_factories, user_transport_factories)
}
pub fn new(
csv_input_config: CSVInputConfig,
user_source_factories: Option<Vec<Box<dyn SourceFactory>>>,
user_transport_factories: Option<Vec<Box<dyn TransportFactory>>>,
) -> CliResult<Self> {
Self::new_with_optional_csv(
Some(csv_input_config),
user_source_factories,
user_transport_factories,
)
}
fn new_with_optional_csv(
csv_input_config: Option<CSVInputConfig>,
user_source_factories: Option<Vec<Box<dyn SourceFactory>>>,
user_transport_factories: Option<Vec<Box<dyn TransportFactory>>>,
) -> CliResult<Self> {
let mut default_transport_factories = create_default_transport_factories();
let transport_factories: Vec<Box<dyn TransportFactory>> = match user_transport_factories {
Some(mut t) => {
t.append(&mut default_transport_factories);
t
}
None => default_transport_factories,
};
let mut default_source_factories = create_default_source_factories(csv_input_config);
let source_factories = match user_source_factories {
Some(mut s) => {
s.append(&mut default_source_factories);
s
}
None => default_source_factories,
};
Ok(InputFactory {
transport_factories,
source_factories,
})
}
}
impl InputFactory {
pub fn create_source_from_path(&mut self, path: &str) -> CliResult<Box<dyn Source>> {
self.create_source_from_input(&Input {
input: InputType::Path(path.to_owned()),
format: InputFormat::Auto,
})
}
pub fn create_source_from_input(&mut self, input: &Input) -> CliResult<Box<dyn Source>> {
let previewer = match &input.input {
InputType::Path(path) => self.create_previewer(Path::new(path))?,
InputType::StdIn => Box::new(ChopperBufPreviewer::new(io::stdin())?),
};
let file_name = match &input.input {
InputType::Path(path) => {
let path = Path::new(path);
if let Some(file_name) = path.file_name() {
Some(file_name.to_str().unwrap().to_owned())
} else {
None
}
}
InputType::StdIn => None,
};
let format = match &input.format {
InputFormat::Extension(extension) => {
let extension = if extension.starts_with(".") {
extension.to_owned()
} else {
".".to_owned() + extension
};
Format::UserSpecified(extension)
}
InputFormat::Auto => match file_name {
None => Format::DetectUsingFileContents,
Some(file_name) => Format::DetectUsingFileNameThenContents(file_name),
},
};
match format {
Format::UserSpecified(format) => {
let (_, previewer, format) = Self::decompress_using_format(previewer, format)?;
self.create_source_from_format(previewer, format)
}
Format::DetectUsingFileNameThenContents(format) => {
let (decompression_result, previewer, format) =
Self::decompress_using_format(previewer, format)?;
for sf in &mut self.source_factories {
if sf.can_create_from_format(&format) {
return sf.create_source(previewer);
}
}
let previewer = match decompression_result {
FormatAutodetectResult::Detected => previewer,
FormatAutodetectResult::NotDetected => {
let (_, previewer) = Self::decompress_by_autodetecting_format(previewer)?;
previewer
}
};
self.create_source_by_autodetecting_format(previewer)
}
Format::DetectUsingFileContents => {
let (_, previewer) = Self::decompress_by_autodetecting_format(previewer)?;
self.create_source_by_autodetecting_format(previewer)
}
}
}
fn decompress_using_format(
previewer: Box<dyn Preview>,
format: String,
) -> CliResult<(FormatAutodetectResult, Box<dyn Preview>, String)> {
match decompress::is_compressed_using_format(&format) {
Some((decompression_format, new_format)) => {
let new_previewer = Self::decompress(decompression_format, previewer)?;
Ok((FormatAutodetectResult::Detected, new_previewer, new_format))
}
None => Ok((FormatAutodetectResult::NotDetected, previewer, format)),
}
}
fn decompress_by_autodetecting_format(
previewer: Box<dyn Preview>,
) -> CliResult<(FormatAutodetectResult, Box<dyn Preview>)> {
match decompress::is_compressed_using_previewer(previewer.as_ref()) {
Some(decompression_format) => {
let new_previewer = Self::decompress(decompression_format, previewer)?;
Ok((FormatAutodetectResult::Detected, new_previewer))
}
None => Ok((FormatAutodetectResult::NotDetected, previewer)),
}
}
fn decompress(
decompression_format: DecompressionFormat,
previewer: Box<dyn Preview>,
) -> CliResult<Box<dyn Preview>> {
let reader = previewer.get_reader();
let new_reader = decompress::decompress(decompression_format, reader)?;
Ok(Box::new(ChopperBufPreviewer::new(new_reader)?))
}
fn create_source_from_format(
&mut self,
previewer: Box<dyn Preview>,
format: String,
) -> CliResult<Box<dyn Source>> {
for sf in &mut self.source_factories {
if sf.can_create_from_format(&format) {
return sf.create_source(previewer);
}
}
Err(Error::from(format!(
"Cannot find source factory for file format {:?}. \
Note that this might not be the full file name, due to being able to be decompressed.",
format
)))
}
fn create_source_by_autodetecting_format(
&mut self,
previewer: Box<dyn Preview>,
) -> CliResult<Box<dyn Source>> {
for sf in &mut self.source_factories {
if sf.can_create_from_previewer(&previewer) {
return sf.create_source(previewer);
}
}
Err(Error::from(
"Failed to autodetect file format by peeking at file contents.",
))
}
fn create_previewer(&mut self, path: &Path) -> CliResult<Box<dyn Preview>> {
let mut reader: Option<Box<dyn io::Read>> = None;
for factory in &mut self.transport_factories.iter() {
match factory.can_open(path) {
false => continue,
true => reader = Some(factory.open(path)?),
}
}
match reader {
None => {
let msg = format!(
"Cannot open file {:?}. \
Check if the path is valid and/or if a right transport factory is provided.",
&path
);
let err = io::Error::new(io::ErrorKind::Other, msg);
Err(Error::Io(err))
}
Some(reader) => {
let previewer = ChopperBufPreviewer::new(reader)?;
Ok(Box::new(previewer))
}
}
}
}
pub fn create_default_source_factories(
csv_input_config: Option<CSVInputConfig>,
) -> Vec<Box<dyn SourceFactory>> {
let mut source_factories: Vec<Box<dyn SourceFactory>> = Vec::new();
if let Some(csv_input_config) = csv_input_config {
source_factories.push(Box::new(CSVFactory::new(csv_input_config)));
}
source_factories.push(Box::new(DCFactory));
source_factories
}
pub fn create_default_transport_factories() -> Vec<Box<dyn TransportFactory>> {
let transport_factories: Vec<Box<dyn TransportFactory>> =
vec![Box::new(FileInput), Box::new(Http)];
transport_factories
}