mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-11 06:01:18 +00:00
Merge pull request #6266 from Silvertree2010/fix/h13526-graph-csv-round-trip-drops-a-bla
Keep blank value row in single-column graph CSV round trip
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user