Skip to content

Commit

Permalink
feat: respect separator
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenLoc committed Mar 15, 2024
1 parent 2b89c49 commit a29da44
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

## Added

- respect separaotir for excel conversion


## [1.3.1] - 2024-02-14

Expand Down
1 change: 1 addition & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn figure() -> Result<String, String> {
Some(Commands::Excel { source, target }) => from_exel::all(
&std::string::String::combine_with_working_dir(source, cli.working_dir.clone()),
&std::string::String::combine_with_working_dir(target, cli.working_dir),
&cli.sep,
),
None => Ok("try qwit --help for information on how to use qwit".to_string()),
};
Expand Down
5 changes: 3 additions & 2 deletions src/from_exel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use self::read_n_x::read;
mod operators;
mod read_n_x;

pub fn all(source: &PathBuf, target: &PathBuf) -> Result<String, String> {
read(source, target)
pub fn all(source: &PathBuf, target: &PathBuf, sep: &str) -> Result<String, String> {
read(source, target, sep)
}

#[derive(Debug)]
Expand All @@ -16,6 +16,7 @@ struct CsvError(String);
trait CsvRowOperator {
fn operate(
&mut self,
separator: String,
rows: impl Iterator<Item = impl Iterator<Item = CsvValue>>,
) -> Result<(), CsvError>;
}
Expand Down
4 changes: 3 additions & 1 deletion src/from_exel/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct PrintOperator;
impl CsvRowOperator for PrintOperator {
fn operate(
&mut self,
_separator: String,
rows: impl Iterator<Item = impl Iterator<Item = CsvValue>>,
) -> Result<(), CsvError> {
rows.for_each(|r| {
Expand All @@ -30,6 +31,7 @@ pub struct FileWritingOperator {
impl CsvRowOperator for FileWritingOperator {
fn operate(
&mut self,
separator: String,
rows: impl Iterator<Item = impl Iterator<Item = CsvValue>>,
) -> Result<(), CsvError> {
rows.for_each(|r| {
Expand All @@ -39,7 +41,7 @@ impl CsvRowOperator for FileWritingOperator {
values.iter().enumerate().for_each(|(n, v)| {
self.write(v);
if n != len {
self.sep(";");
self.sep(&separator);
}
});

Expand Down
20 changes: 14 additions & 6 deletions src/from_exel/read_n_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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> {
pub fn read(source: &PathBuf, target: &PathBuf, sep: &str) -> Result<String, String> {
// find source file
let sce = PathBuf::from(source);
match sce.extension().and_then(|s| s.to_str()) {
Expand All @@ -27,9 +27,13 @@ pub fn read(source: &PathBuf, target: &PathBuf) -> Result<String, String> {
.unwrap();
let target = BufWriter::new(target_file);

let res = write_range(range, FileWritingOperator { writer: target })
.map(|()| format!("Done writing sheet: {title}"))
.map_err(|err| err.0);
let res = write_range(
range,
FileWritingOperator { writer: target },
sep.to_owned(),
)
.map(|()| format!("Done writing sheet: {title}"))
.map_err(|err| err.0);

match res {
Ok(o) => println!("{o}"),
Expand All @@ -40,7 +44,11 @@ pub fn read(source: &PathBuf, target: &PathBuf) -> Result<String, String> {
Ok("All Done".to_owned())
}

fn write_range(range: &Range<Data>, mut operator: impl CsvRowOperator) -> Result<(), CsvError> {
fn write_range(
range: &Range<Data>,
mut operator: impl CsvRowOperator,
sep: String,
) -> Result<(), CsvError> {
let all_rows = range.rows().map(CsvRow::iterator);
operator.operate(all_rows)
operator.operate(sep, all_rows)
}

0 comments on commit a29da44

Please sign in to comment.