Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse \t as tab delimiter #99

Merged
merged 1 commit into from
Oct 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions src/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,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),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is del?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[del] matches any array with exactly one byte and creates a variable named del with the value of that byte. del can then be used on the right hand side and put into the Ok.

[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())
);
}
}