Skip to content

Commit

Permalink
parse \t as tab delimiter (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lireer authored Oct 18, 2021
1 parent c609ea4 commit a832081
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,48 @@ pub fn get_col_data_type(col: &[&str]) -> ValueType {

pub fn parse_delimiter(src: &str) -> Result<u8, String> {
let bytes = src.as_bytes();
match bytes.len() {
1 => Ok(bytes[0]),
match *bytes {
[del] => Ok(del),
[b'\\', b't'] => Ok(b'\t'),
_ => Err(format!(
"expected one byte as a delimiter, got {} bytes (\"{}\")",
"expected one byte as delimiter, got {} bytes (\"{}\")",
bytes.len(),
src
)),
}
}

#[cfg(test)]
mod tests {
use crate::datatype::parse_delimiter;

#[test]
fn one_byte_delimiter() {
assert_eq!(parse_delimiter(","), Ok(b','));
assert_eq!(parse_delimiter(";"), Ok(b';'));
assert_eq!(parse_delimiter("|"), Ok(b'|'));
assert_eq!(parse_delimiter(" "), Ok(b' '));
assert_eq!(parse_delimiter("\t"), Ok(b'\t'));
}

#[test]
fn tab_delimiter() {
assert_eq!(parse_delimiter("\\t"), Ok(b'\t'));
}

#[test]
fn delimiter_wrong_length() {
assert_eq!(
parse_delimiter(""),
Err("expected one byte as delimiter, got 0 bytes (\"\")".to_string())
);
assert_eq!(
parse_delimiter("too long"),
Err("expected one byte as delimiter, got 8 bytes (\"too long\")".to_string())
);
assert_eq!(
parse_delimiter("\\n"),
Err("expected one byte as delimiter, got 2 bytes (\"\\n\")".to_string())
);
}
}

0 comments on commit a832081

Please sign in to comment.