-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
69 lines (63 loc) · 2.27 KB
/
lib.rs
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
use anyhow::{anyhow, Context};
use log::{error, info};
pub mod args;
pub mod bibwriter;
pub mod converter;
pub mod csvreader;
pub fn run(config: &args::Config) -> Result<(), anyhow::Error> {
// open file for reading and writing
let file_input = std::fs::File::open(&config.file_input)
.with_context(|| format!("Could not open csv file: {}", &config.file_input.display()))?;
let buf_output =
std::io::BufWriter::new(std::fs::File::create(&config.file_output).with_context(|| {
format!(
"Could not open file for writing: {}",
&config.file_output.display()
)
})?);
info!(
"Created file \"{}\" to write output.",
&config.file_output.display()
);
// create new csvparser, converter, and writer
//
// TODO cloning here makes absolutely no sense!
let mut csv_field_mapping = config.csv_field_mapping.clone();
let mut verbatim_fields = config.verbatim_fields.clone();
let reader = csvreader::Reader::new(&file_input, &config.csv_delimiter, config.csv_lazy);
let converter = {
let mut ret = converter::FieldConverter::new(&mut csv_field_mapping, &mut verbatim_fields);
if config.mapping_defaults {
ret = ret.add_defaults()
}
ret
};
let mut writer: Box<dyn bibwriter::BibWrite> = match config.output_type {
args::OutputType::Bibtex => Box::new(bibwriter::BibtexWriter::new(buf_output)),
args::OutputType::Biblatex => Box::new(bibwriter::BiblatexWriter::new(buf_output)),
};
// main loop
let start = std::time::Instant::now();
for (index, entry) in reader.enumerate() {
let entry = match entry {
Ok(x) => x,
Err(e) => {
if config.csv_lazy {
error!("Error converting item: {}. Skipping item.", e);
continue;
} else {
return Err(anyhow!("{}. Option \"-l\" might help.", e));
}
}
};
let entry = converter.convert_fields(entry, format!("entry_{index}"));
writer.write(&entry)?;
}
info!(
"Wrote {} entries in {:?}.",
writer.get_num_written_entries(),
start.elapsed()
);
// the end
Ok(())
}