From 6f2933c6f31802825d0a49d0f444aa72eec4dfdb Mon Sep 17 00:00:00 2001 From: Nicolas Sanchez Date: Sun, 5 Oct 2025 22:15:17 +0200 Subject: [PATCH] modification du trim --- src/arguments.rs | 24 ++++++++++++++++++++++-- src/xlsxtocsv.rs | 11 +++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/arguments.rs b/src/arguments.rs index b5d8ce3..4316031 100644 --- a/src/arguments.rs +++ b/src/arguments.rs @@ -19,6 +19,26 @@ impl ToString for FillMergedCells { } } +#[derive(Clone, Debug, ValueEnum)] +pub enum TrimSpaces { + End, + Start, + Both, + None +} + +impl ToString for TrimSpaces { + fn to_string(&self) -> String { + match self { + TrimSpaces::End => "end".into(), + TrimSpaces::Start => "start".into(), + TrimSpaces::Both => "both".into(), + TrimSpaces::None => "none".into(), + } + } +} + + #[derive(Parser, Debug)] pub struct Arguments { /// Path to the xlsx file @@ -40,6 +60,6 @@ pub struct Arguments { #[arg(short, long, default_value_t = String::from("0"))] pub worksheet: String, /// Trim white spaces at end of cells - #[arg(short, long, default_value_t = false)] - pub trim_end: bool, + #[arg(short, long, default_value_t = TrimSpaces::None)] + pub trim: TrimSpaces, } diff --git a/src/xlsxtocsv.rs b/src/xlsxtocsv.rs index 4d103d2..0327b66 100644 --- a/src/xlsxtocsv.rs +++ b/src/xlsxtocsv.rs @@ -1,7 +1,7 @@ use std::path::Path; use umya_spreadsheet::{Range, Worksheet, reader}; -use crate::arguments::Arguments; +use crate::arguments::{Arguments, TrimSpaces}; use crate::error::Error; pub fn xlsxtocsv(args: &Arguments) -> Result<(), Error> { @@ -85,9 +85,12 @@ pub fn xlsxtocsv(args: &Arguments) -> Result<(), Error> { } // apply modifications to cells value (trim spaces, replace separator chars, line breaks etc.) - if args.trim_end { - value = String::from(value.trim_end()); - } + value = match args.trim { + TrimSpaces::End => String::from(value.trim_end()), + TrimSpaces::Start => String::from(value.trim_start()), + TrimSpaces::Both => String::from(value.trim()), + TrimSpaces::None => value, + }; value = value.replace('\r', "").replace('\n', " "); if let Some(ref replacement) = args.replace_separator_by { value = value.replace(args.separator, replacement);