Keep blank value row in single-column graph CSV round trip

Found while testing crates/engine/src/cmd/graph/data.rs:51 (parse_csv); written by crates/doc/src/graph.rs:497 (GraphSpec::to_csv)

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
NetterSilver
2026-11-11 04:16:30 +00:00
co-authored by Claude Haiku 5.5
parent 4c2b8df865
commit aaf27e1ec6
3 changed files with 16 additions and 1 deletions
+3 -1
View File
@@ -525,7 +525,9 @@ impl GraphSpec {
out.push_str(&field(self.categories.get(i).map(String::as_str).unwrap_or_default(), true));
out.push(',');
}
out.push_str(&r.iter().map(|v| v.map(|v| v.to_string()).unwrap_or_default()).collect::<Vec<_>>().join(","));
let line = r.iter().map(|v| v.map(|v| v.to_string()).unwrap_or_default()).collect::<Vec<_>>().join(",");
// A lone blank value would be an empty line, which readers skip; a quoted empty field keeps the row.
out.push_str(if line.is_empty() && !labelled { "\"\"" } else { &line });
out.push('\n');
}
out
+4
View File
@@ -50,6 +50,10 @@ pub(super) fn parse_csv(csv: &str) -> (Vec<String>, Vec<String>, Vec<Vec<Option<
.map(|l| l.trim_end_matches('\r'))
.filter(|l| !l.trim().is_empty())
.map(|l| {
// A lone `""` line is a single blank value (`to_csv` writes it so the row isn't dropped as empty).
if l.trim() == "\"\"" {
return vec![Cell { text: String::new(), quoted: false }];
}
let tabs = split_unquoted(l, '\t');
let parts = if tabs.len() > 1 { tabs } else { split_unquoted(l, ',') };
parts.into_iter().map(cell).collect()
+9
View File
@@ -4210,3 +4210,12 @@ fn character_formats_are_kept_and_bigger_labels_space_out() {
fn csv_leading_byte_order_mark_is_ignored() {
assert_eq!(super::parse_csv("\u{feff}1,2\n3,4"), super::parse_csv("1,2\n3,4"));
}
#[test]
fn csv_round_trip_keeps_blank_row_in_single_column_graph() {
let mut g = GraphSpec::default();
g.set_cells(vec![vec![Some(1.0)], vec![None], vec![Some(3.0)]]);
let (_, c, r) = super::parse_csv(&g.to_csv());
assert!(c.is_empty());
assert_eq!(r, g.cells());
}