summaryrefslogtreecommitdiff
path: root/library
diff options
context:
space:
mode:
authorMarek Barvíř <barvirm@gmail.com>2023-03-30 11:52:27 +0200
committerGitHub <noreply@github.com>2023-03-30 11:52:27 +0200
commit5aa2ba14906a83706849e06581e511d49f66ac49 (patch)
tree456deb9ebb2092a2042cad609be6433beda051b8 /library
parent81c5568aecb6c6b36db3cacdd3536b4754656747 (diff)
FIX "data.typ" test (#412)
Diffstat (limited to 'library')
-rw-r--r--library/src/compute/data.rs25
1 files changed, 11 insertions, 14 deletions
diff --git a/library/src/compute/data.rs b/library/src/compute/data.rs
index 82ff3a4b..0c17972d 100644
--- a/library/src/compute/data.rs
+++ b/library/src/compute/data.rs
@@ -1,5 +1,3 @@
-use std::fmt::Write;
-
use typst::diag::{format_xml_like_error, FileError};
use crate::prelude::*;
@@ -76,9 +74,12 @@ pub fn csv(
let mut reader = builder.from_reader(data.as_slice());
let mut array = Array::new();
- for result in reader.records() {
- let row = result.map_err(format_csv_error).at(span)?;
- let sub = row.iter().map(|field| Value::Str(field.into())).collect();
+ for (line, result) in reader.records().enumerate() {
+ // Original solution use line from error, but that is incorrect with has_headers set to false
+ // See issue: https://github.com/BurntSushi/rust-csv/issues/184
+ let line = line + 1; // Counting lines from 1
+ let row = result.map_err(|err| format_csv_error(err, line)).at(span)?;
+ let sub = row.into_iter().map(|field| Value::Str(field.into())).collect();
array.push(Value::Array(sub))
}
@@ -112,17 +113,13 @@ impl Default for Delimiter {
}
/// Format the user-facing CSV error message.
-fn format_csv_error(error: csv::Error) -> String {
+fn format_csv_error(error: csv::Error, line: usize) -> String {
match error.kind() {
csv::ErrorKind::Utf8 { .. } => "file is not valid utf-8".into(),
- csv::ErrorKind::UnequalLengths { pos, expected_len, len } => {
- let mut msg = format!(
- "failed to parse csv file: found {len} instead of {expected_len} fields"
- );
- if let Some(pos) = pos {
- write!(msg, " in line {}", pos.line()).unwrap();
- }
- msg
+ csv::ErrorKind::UnequalLengths { expected_len, len, .. } => {
+ format!(
+ "failed to parse csv file: found {len} instead of {expected_len} fields in line {line}"
+ )
}
_ => "failed to parse csv file".into(),
}