Skip to content

Commit

Permalink
Accept tab delimiter specified as -d \t without quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
YS-L committed Jul 29, 2024
1 parent b5b5037 commit e8612ff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ impl App {

let delimiter = match delimiter {
Delimiter::Default => b',',
Delimiter::Tab => b'\t',
Delimiter::Character(d) => d,
Delimiter::Auto => sniff_delimiter(filename).unwrap_or(b','),
};
Expand Down
11 changes: 9 additions & 2 deletions src/delimiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ pub enum Delimiter {
/// Use the default delimiter (comma)
Default,

/// Use tab as the delimiter
Tab,

/// Use the specified delimiter
Character(u8),

Expand All @@ -16,21 +19,25 @@ impl Delimiter {
/// Create a Delimiter by parsing the command line argument for the delimiter
pub fn from_arg(delimiter_arg: &Option<String>, tab_separation: bool) -> CsvlensResult<Self> {
if tab_separation {
return Ok(Delimiter::Character('\t'.try_into()?));
return Ok(Delimiter::Tab);
}

if let Some(s) = delimiter_arg {
if s == "auto" {
return Ok(Delimiter::Auto);
}
if s == r"\t" {
return Ok(Delimiter::Character(b'\t'));
return Ok(Delimiter::Tab);
}
let mut chars = s.chars();
let c = chars.next().ok_or_else(|| CsvlensError::DelimiterEmpty)?;
if !c.is_ascii() {
return Err(CsvlensError::DelimiterNotAscii(c));
}
if c == 't' {
// commonly occurrs when argument is specified like "-d \t" without quotes
return Ok(Delimiter::Tab);
}
if chars.next().is_some() {
return Err(CsvlensError::DelimiterMultipleCharacters(s.clone()));
}
Expand Down

0 comments on commit e8612ff

Please sign in to comment.