use matten_data::{MattenDataError, Table};
fn main() {
match Table::from_csv_str("sales,sales\n1,2") {
Err(MattenDataError::DuplicateColumn { name }) => {
println!("duplicate header : {name}");
assert_eq!(name, "sales");
}
other => panic!("expected DuplicateColumn, got {other:?}"),
}
match Table::from_csv_str("a,b,c\n1,2,3\n4,5") {
Err(MattenDataError::RaggedRow {
row,
expected,
actual,
}) => {
println!("ragged row : line {row}, expected {expected}, got {actual}");
assert_eq!((row, expected, actual), (3, 3, 2));
}
other => panic!("expected RaggedRow, got {other:?}"),
}
let with_text = Table::from_csv_str("label,value\nok,10\nbad,oops")
.expect("parses fine; the text only fails at numeric conversion");
match with_text
.select_columns(["value"])
.and_then(|t| t.try_numeric())
{
Err(MattenDataError::NonNumericValue { column, row, value }) => {
println!("non-numeric value: column={column}, line={row}, value={value:?}");
assert_eq!((column.as_str(), row, value.as_str()), ("value", 3, "oops"));
}
other => panic!("expected NonNumericValue, got {other:?}"),
}
let with_missing =
Table::from_csv_str("a,b\n1,2\n3,").expect("missing cell is allowed in a Table");
match with_missing.try_numeric() {
Err(MattenDataError::MissingValue { column, row }) => {
println!("missing value : column={column}, line={row}");
assert_eq!((column.as_str(), row), ("b", 3));
}
other => panic!("expected MissingValue, got {other:?}"),
}
println!("data_05_errors: OK");
}