-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::{ | ||
fs::File, | ||
io::{BufWriter, Write}, | ||
}; | ||
|
||
use super::{CsvError, CsvRowOperator, CsvValue}; | ||
|
||
pub struct PrintOperator; | ||
|
||
impl CsvRowOperator for PrintOperator { | ||
fn operate( | ||
&mut self, | ||
rows: impl Iterator<Item = impl Iterator<Item = CsvValue>>, | ||
) -> Result<(), CsvError> { | ||
rows.for_each(|r| { | ||
println!("---new row---"); | ||
r.for_each(|v| match v.0 { | ||
Ok(k) => println!("{k}"), | ||
Err(e) => println!("{e}"), | ||
}); | ||
}); | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub struct FileWritingOperator { | ||
pub(crate) writer: BufWriter<File>, | ||
} | ||
|
||
impl CsvRowOperator for FileWritingOperator { | ||
fn operate( | ||
&mut self, | ||
rows: impl Iterator<Item = impl Iterator<Item = CsvValue>>, | ||
) -> Result<(), CsvError> { | ||
let _ = rows.map(|r| { | ||
let values: Vec<String> = r.filter_map(|v| v.0.ok()).collect(); | ||
let len = values.len(); | ||
|
||
let _ = values.iter().enumerate().map(|(n, v)| { | ||
self.write(v); | ||
if n != len { | ||
self.sep(";"); | ||
} | ||
}); | ||
|
||
self.end_line(); | ||
}); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl FileWritingOperator { | ||
fn write(&mut self, value: &str) { | ||
let _ = write!(&mut self.writer, "{value}").map_err(|err| CsvError(err.to_string())); | ||
} | ||
fn end_line(&mut self) { | ||
write!(self.writer, "\r\n").unwrap(); | ||
} | ||
fn sep(&mut self, sep: &str) { | ||
write!(self.writer, "{sep}").unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use std::{fs::OpenOptions, io::BufWriter, path::PathBuf}; | ||
|
||
use calamine::{open_workbook_auto, Data, Range, Reader}; | ||
|
||
use super::{operators::FileWritingOperator, CsvError, CsvRow, CsvRowOperator}; | ||
|
||
pub fn read(source: &PathBuf, target: &PathBuf) -> Result<String, String> { | ||
// find source file | ||
let sce = PathBuf::from(source); | ||
match sce.extension().and_then(|s| s.to_str()) { | ||
Some("xlsx" | "xlsm" | "xlsb" | "xls") => (), | ||
_ => panic!("Expecting an excel file"), | ||
} | ||
|
||
// create or append to target file | ||
let target_path = PathBuf::from(target).with_extension("csv"); | ||
let target_file = OpenOptions::new() | ||
.create(true) | ||
.append(true) | ||
.open(target_path) | ||
.unwrap(); | ||
let target = BufWriter::new(target_file); | ||
|
||
// open xl file | ||
let mut xl = open_workbook_auto(&sce).unwrap(); | ||
let range = xl.worksheet_range_at(0).unwrap().unwrap(); | ||
|
||
write_range(&range, FileWritingOperator { writer: target }) | ||
.map(|()| "All done".to_owned()) | ||
.map_err(|err| err.0) | ||
} | ||
|
||
fn write_range(range: &Range<Data>, mut operator: impl CsvRowOperator) -> Result<(), CsvError> { | ||
let all_rows = range.rows().map(CsvRow::iterator); | ||
operator.operate(all_rows) | ||
} |