Skip to content
Open
Show file tree
Hide file tree
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
13 changes: 9 additions & 4 deletions crates/data/src/dbf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,20 @@ pub(crate) fn cause(encoding: &str) -> String {

/// What opening a `.dbf` says of its field names that aren't valid in its `encoding` ([`not_utf8`]),
/// naming the first five by their column (from 1). `None` when there are none.
pub(crate) fn not_utf8_names_note(columns: &[usize], encoding: &str) -> Option<String> {
pub(crate) fn not_utf8_names_note(columns: &[usize], encoding: &str) -> Option<crate::loss::Note> {
let (what, label) = match columns.len() {
0 => return None,
1 => (format!("1 field name isn't valid {encoding} and was"), "field"),
n => (format!("{n} field names aren't valid {encoding} and were"), "fields"),
};
let named: Vec<String> = columns.iter().take(5).map(usize::to_string).collect();
let more = if columns.len() > 5 { format!(" and {} more", columns.len() - 5) } else { String::new() };
Some(format!(
let text = format!(
"Shapefile: {what} read with U+FFFD in place of the bytes that can't be read, in {label} {}{more}; {}",
named.join(", "),
cause(encoding)
))
);
Some(crate::loss::Note::loss(crate::loss::LossCode::ChangedEncoding, text))
}

/// What a `.dbf` holds that isn't valid in its `encoding`: which of its field names (by column, from
Expand Down Expand Up @@ -670,17 +671,21 @@ mod tests {
fn field_names_that_arent_utf8_are_said_with_their_columns() {
assert_eq!(not_utf8_names_note(&[], "UTF-8"), None);
let one = not_utf8_names_note(&[3], "UTF-8").unwrap();
assert_eq!(one.loss, Some(crate::loss::LossCode::ChangedEncoding));
let one = one.as_str();
assert!(one.starts_with("Shapefile: 1 field name isn't valid UTF-8 and was read with U+FFFD"), "{one}");
assert!(one.contains("in field 3;"), "{one}");
let two = not_utf8_names_note(&[3, 7], "UTF-8").unwrap();
let two = two.as_str();
assert!(two.starts_with("Shapefile: 2 field names aren't valid UTF-8 and were read with U+FFFD"), "{two}");
assert!(two.contains("in fields 3, 7;"), "{two}");
assert!(two.ends_with(NO_OTHER_ENCODING), "{two}");
let many = not_utf8_names_note(&[1, 2, 3, 4, 5, 6, 7], "UTF-8").unwrap();
let many = many.as_str();
assert!(many.contains("in fields 1, 2, 3, 4, 5 and 2 more;"), "{many}");
// A declared code page is named, as what the file declares.
assert_eq!(
not_utf8_names_note(&[1], "Big5").as_deref(),
not_utf8_names_note(&[1], "Big5").as_ref().map(crate::loss::Note::as_str),
Some(
"Shapefile: 1 field name isn't valid Big5 and was read with U+FFFD in place of the bytes that can't be read, in field 1; the .cpg or the .dbf's language driver declares Big5"
)
Expand Down
9 changes: 7 additions & 2 deletions crates/data/src/gml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ pub fn read(file_name: &str, bytes: &[u8]) -> Result<Vec<(Dataset, ReadReport)>>
));
}
if let Some(n) = crate::source::null_text_note(&ds.fields, &null_text) {
rep.messages.push(format!("{}: {n}", ds.name));
rep.messages.push(n.prefixed(&ds.name));
}
if parts > 0 {
let what = if parts == 1 { "part" } else { "parts" };
Expand Down Expand Up @@ -875,7 +875,12 @@ mod tests {
assert_eq!(ds.value(0, code), &Value::Text(" R-1".into()));
assert_eq!(ds.value(1, code), &Value::Null);
assert_eq!(ds.value(2, code), &Value::Text("Laid out".into()), "an indented value loses its layout");
assert!(report.messages.iter().any(|m| m == "s: 1 value written as Null was read as empty, in 'code'"), "{:?}", report.messages);
assert!(
report.messages.iter().any(|m| m.as_str() == "s: 1 value written as Null was read as empty, in 'code'"
&& m.loss == Some(crate::loss::LossCode::AssumedNullMarker)),
"{:?}",
report.messages
);
let clean = gml.replace("&lt;Null&gt;", "B-2");
let layers = read("s.gml", clean.as_bytes()).unwrap();
assert!(layers[0].1.messages.iter().all(|m| !m.contains("written as Null")), "no marker, no notice: {:?}", layers[0].1.messages);
Expand Down
2 changes: 1 addition & 1 deletion crates/data/src/gpx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ mod tests {
assert_eq!(ds.fields[k].ty, FieldType::DateTime);
assert!(ds.value(0, k).is_null() && ds.value(2, k).is_null());
assert_eq!(ds.value(1, k).to_string(), "2026-03-05T14:30:00.123456+02:00");
assert!(report.unread_dates.as_deref().unwrap().starts_with("1 value"));
assert!(report.unread_dates.as_ref().map(crate::loss::Note::as_str).unwrap().starts_with("1 value"));
assert!(read("clean.gpx", GPX.as_bytes()).unwrap().iter().all(|(_, r)| r.unread_dates.is_none()));
}

Expand Down
33 changes: 23 additions & 10 deletions crates/data/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3769,8 +3769,9 @@ struct DbfRead {
/// `name` is the layer's, for the error of a `.dbf` it can't open.
fn dbf_reader(mut bytes: Vec<u8>, cpg: Option<&[u8]>, name: &str) -> Result<DbfRead> {
use std::io::Cursor;
let mut encoding_notes = Vec::new();
let encoding = crate::dbf::encoding(cpg, bytes.get(29).copied().unwrap_or(0), &mut encoding_notes);
let mut declared = Vec::new();
let encoding = crate::dbf::encoding(cpg, bytes.get(29).copied().unwrap_or(0), &mut declared);
let mut encoding_notes = crate::loss::Notes::from(declared);
// Counted before a date column is marked a character one below: its values aren't text.
let (names, not_utf8) = crate::dbf::not_utf8(&bytes, &encoding);
encoding_notes.extend(crate::dbf::not_utf8_names_note(&names, encoding.name()));
Expand Down Expand Up @@ -3804,7 +3805,7 @@ fn dbf_reader(mut bytes: Vec<u8>, cpg: Option<&[u8]>, name: &str) -> Result<DbfR
})
.collect();
let fields = order.into_iter().map(|(n, t)| Field::new(n, t)).collect();
let report = ReadReport { messages: encoding_notes.into(), ..Default::default() };
let report = ReadReport { messages: encoding_notes, ..Default::default() };
Ok(DbfRead { reader: dbf, fields, decimals, report, not_utf8, deleted, encoding: encoding.name() })
}

Expand Down Expand Up @@ -3954,18 +3955,21 @@ fn dbf_notices(ds: &Dataset, report: &mut ReadReport, counts: &DbfCounts) {
let DbfCounts { unread_dates, nonfinite, null_text, not_utf8, encoding } = counts;
let name = &ds.name;
report.unread_dates = crate::source::unread_dates_note(&ds.fields, unread_dates);
report.messages.extend(report.unread_dates.iter().map(|n| format!("{name}: {n}")));
report.messages.extend(report.unread_dates.iter().map(|n| n.clone().prefixed(name)));
let nonfinite_total: usize = nonfinite.iter().sum();
if nonfinite_total > 0 {
let what = if nonfinite_total == 1 {
"1 number isn't finite (nan or inf) and was".to_string()
} else {
format!("{nonfinite_total} numbers aren't finite (nan or inf) and were")
};
report.messages.push(format!("{name}: {what} read as empty, in {}", named_fields(&ds.fields, nonfinite)));
report.messages.push(crate::loss::Note::loss(
crate::loss::LossCode::DroppedValue,
format!("{name}: {what} read as empty, in {}", named_fields(&ds.fields, nonfinite)),
));
}
report.messages.extend(crate::source::null_text_note(&ds.fields, null_text).map(|n| format!("{name}: {n}")));
report.messages.extend(crate::source::not_utf8_note(&ds.fields, not_utf8, encoding).map(|n| format!("{name}: {n}")));
report.messages.extend(crate::source::null_text_note(&ds.fields, null_text).map(|n| n.prefixed(name)));
report.messages.extend(crate::source::not_utf8_note(&ds.fields, not_utf8, encoding).map(|n| n.prefixed(name)));
}

/// The files of a Shapefile, read into memory.
Expand Down Expand Up @@ -8076,7 +8080,11 @@ mod tests {
for (read, (back, report)) in [("whole", whole), ("streamed", streamed)] {
assert_eq!(back.features[1].attrs[0], Value::Null, "{read}");
let said = "permits: 1 value written as Null was read as empty, in 'issued'";
assert!(report.messages.iter().any(|m| m == said), "{read}: {:?}", report.messages);
assert!(
report.messages.iter().any(|m| m.as_str() == said && m.loss == Some(crate::loss::LossCode::AssumedNullMarker)),
"{read}: {:?}",
report.messages
);
assert!(!report.messages.iter().any(|m| m.contains("date")), "{read}: {:?}", report.messages);
}
}
Expand Down Expand Up @@ -8109,7 +8117,11 @@ mod tests {
assert_eq!(ds.fields[3].ty, FieldType::Double, "{what}: `<Null>` doesn't type a column");
assert_eq!(column(3), [Value::Float(1.5), Value::Null, Value::Float(2.5), Value::Float(3.5)], "{what}");
let said = "3 values written as Null were read as empty, in 'code', 'note', 'x'";
assert!(report.messages.iter().any(|m| m == said), "{what}: {:?}", report.messages);
assert!(
report.messages.iter().any(|m| m.as_str() == said && m.loss == Some(crate::loss::LossCode::AssumedNullMarker)),
"{what}: {:?}",
report.messages
);
}
let (_, report) = read_csv("code\n 2535\n", "t").unwrap();
assert!(report.messages.iter().all(|m| !m.contains("written as Null")), "{:?}", report.messages);
Expand Down Expand Up @@ -9290,7 +9302,8 @@ mod tests {
assert_eq!(days, [at("2026-03-05"), Value::Null, Value::Null, at("0001-01-01"), Value::Null], "{read}");
assert!(back.features.iter().all(|f| f.geometry.is_some()), "{read}");
let note = report.messages.iter().find(|m| m.contains("date")).unwrap_or_else(|| panic!("{read}: {report:?}"));
assert_eq!(note, "permits: 1 value isn't a date and was read as empty, in 'issued'", "{read}");
assert_eq!(note.as_str(), "permits: 1 value isn't a date and was read as empty, in 'issued'", "{read}");
assert_eq!(note.loss, Some(crate::loss::LossCode::DroppedValue), "{read}");
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,10 +716,10 @@ pub struct ReadReport {
pub synced: Option<sync::Synced>,
/// What a read of a source says of values in date fields that weren't dates
/// ([`source::unread_dates_note`]), also in `messages`: for a caller that says only this.
pub unread_dates: Option<String>,
pub unread_dates: Option<loss::Note>,
/// What it says of date-times it rounded to the microsecond ([`source::rounded_dates_note`]),
/// also in `messages`.
pub rounded_dates: Option<String>,
pub rounded_dates: Option<loss::Note>,
/// The extrusion the reader suggests for the layer (a city model's volumes): applied when the
/// layer is added, and only then; a reload keeps the layer's own.
pub extrusion: Option<SuggestedExtrusion>,
Expand Down
15 changes: 15 additions & 0 deletions crates/data/src/loss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ loss_codes! {
),
ChangedTruncated => ("changed.truncated", Changed, "Use a decimal field to keep the fractional part."),
ChangedBody => ("changed.body", Changed, "Export in the layer's own coordinate system, which keeps its body."),
ChangedEncoding => (
"changed.encoding",
Changed,
"Put a .cpg file naming the file's own encoding beside it."
),
ChangedRounded => (
"changed.rounded",
Changed,
"Keep the source file where times finer than a microsecond matter."
),
ChangedPosition => (
"changed.position",
Changed,
Expand All @@ -95,6 +105,11 @@ loss_codes! {
Approximated,
"Export in the layer's own coordinate system, or give that system with a known transformation to WGS 84."
),
AssumedNullMarker => (
"assumed.null-marker",
Assumed,
"Write the value another way if the text Null is meant as text, not as an empty value."
),
}

impl LossCode {
Expand Down
3 changes: 2 additions & 1 deletion crates/data/src/mapinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,8 @@ mod tests {
assert_eq!(ds.value(0, 0), &Value::Text(" 2535 ".into()));
assert_eq!(ds.value(0, 1), &Value::Int(7));
assert_eq!(ds.value(1, 0), &Value::Null);
assert_eq!(report.messages, ["1 value written as Null was read as empty, in 'code'"]);
assert_eq!(report.messages.texts().collect::<Vec<_>>(), ["1 value written as Null was read as empty, in 'code'"]);
assert_eq!(report.messages[0].loss, Some(crate::loss::LossCode::AssumedNullMarker));
let (_, clean) = read(mif, Some("\" 2535 \", 7 \n\"b\",8\n"), "t").unwrap();
assert!(clean.messages.is_empty(), "no marker, no notice: {:?}", clean.messages);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/data/src/sheets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -769,14 +769,14 @@ mod tests {
let (ds, report) = book(["45000", "60", "45002"]);
assert_eq!(ds.fields[0].ty, FieldType::Date);
assert!(ds.value(1, 0).is_null());
assert_eq!(report.unread_dates.as_deref(), Some("1 value isn't a date and was read as empty, in 'a'"));
assert_eq!(report.unread_dates.as_ref().map(crate::loss::Note::as_str), Some("1 value isn't a date and was read as empty, in 'a'"));
// A mixed column (text among the dates) says the same, and keeps the rest as text.
let (ds, report) = book(["45000", "60", "x"]);
assert_eq!(ds.fields[0].ty, FieldType::Text);
// (A row left with nothing in it is skipped, as any empty row is.)
let seen: Vec<String> = (0..ds.features.len()).map(|r| ds.value(r, 0).to_string()).collect();
assert_eq!(seen, ["2023-03-15", "x"], "{seen:?}");
assert_eq!(report.unread_dates.as_deref(), Some("1 value isn't a date and was read as empty, in 'a'"));
assert_eq!(report.unread_dates.as_ref().map(crate::loss::Note::as_str), Some("1 value isn't a date and was read as empty, in 'a'"));
// Without it, nothing is said.
let (_, report) = book(["45000", "61", "x"]);
assert_eq!(report.unread_dates, None);
Expand Down
27 changes: 16 additions & 11 deletions crates/data/src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,31 +327,33 @@ pub fn add_unread_dates(counts: &mut Vec<usize>, more: &[usize]) {

/// What a read says of values in date fields that weren't dates ([`Batch::unread_dates`]), naming
/// the first five fields. `None` when there were none.
pub fn unread_dates_note(fields: &[crate::Field], counts: &[usize]) -> Option<String> {
pub fn unread_dates_note(fields: &[crate::Field], counts: &[usize]) -> Option<crate::loss::Note> {
let total: usize = counts.iter().sum();
if total == 0 {
return None;
}
let what = if total == 1 { "1 value isn't a date and was".to_string() } else { format!("{total} values aren't dates and were") };
Some(format!("{what} read as empty, in {}", crate::io::named_fields(fields, counts)))
let text = format!("{what} read as empty, in {}", crate::io::named_fields(fields, counts));
Some(crate::loss::Note::loss(crate::loss::LossCode::DroppedValue, text))
}

/// What a read says of text values `<Null>` it read as null ([`crate::Value::is_null_marker`],
/// [`Batch::null_text`]), naming the first five fields. `None` when there were none. It names the
/// marker as `Null`, without its brackets: notices are redacted, which takes `<Null>` for a tag.
pub fn null_text_note(fields: &[crate::Field], counts: &[usize]) -> Option<String> {
pub fn null_text_note(fields: &[crate::Field], counts: &[usize]) -> Option<crate::loss::Note> {
let total: usize = counts.iter().sum();
if total == 0 {
return None;
}
let what = if total == 1 { "1 value written as Null was".to_string() } else { format!("{total} values written as Null were") };
Some(format!("{what} read as empty, in {}", crate::io::named_fields(fields, counts)))
let text = format!("{what} read as empty, in {}", crate::io::named_fields(fields, counts));
Some(crate::loss::Note::loss(crate::loss::LossCode::AssumedNullMarker, text))
}

/// What a read says of a `.dbf`'s text values that aren't valid in the `encoding` they were read in
/// ([`Batch::not_utf8`], [`FeatureSource::text_encoding`]): how many, in which fields (the first five
/// named), and why they were read in it. `None` when there were none.
pub fn not_utf8_note(fields: &[crate::Field], counts: &[usize], encoding: &str) -> Option<String> {
pub fn not_utf8_note(fields: &[crate::Field], counts: &[usize], encoding: &str) -> Option<crate::loss::Note> {
let total: usize = counts.iter().sum();
if total == 0 {
return None;
Expand All @@ -361,22 +363,25 @@ pub fn not_utf8_note(fields: &[crate::Field], counts: &[usize], encoding: &str)
} else {
format!("{total} text values aren't valid {encoding} and were")
};
Some(format!(
let text = format!(
"{what} read with U+FFFD in place of the bytes that can't be read, in {}; {}",
crate::io::named_fields(fields, counts),
crate::dbf::cause(encoding)
))
);
Some(crate::loss::Note::loss(crate::loss::LossCode::ChangedEncoding, text))
}

/// What a read says of date-times it rounded to the microsecond ([`Batch::rounded_dates`]), naming
/// the first five fields. `None` when there were none.
pub fn rounded_dates_note(fields: &[crate::Field], counts: &[usize]) -> Option<String> {
pub fn rounded_dates_note(fields: &[crate::Field], counts: &[usize]) -> Option<crate::loss::Note> {
let total: usize = counts.iter().sum();
if total == 0 {
return None;
}
let what = if total == 1 { "1 date-time was".to_string() } else { format!("{total} date-times were") };
Some(format!("{what} rounded to the microsecond, as more digits than that aren't kept, in {}", crate::io::named_fields(fields, counts)))
let text =
format!("{what} rounded to the microsecond, as more digits than that aren't kept, in {}", crate::io::named_fields(fields, counts));
Some(crate::loss::Note::loss(crate::loss::LossCode::ChangedRounded, text))
}

/// A vector data source read in place.
Expand Down Expand Up @@ -780,9 +785,9 @@ fn materialize_within(src: &dyn FeatureSource, budget: u64) -> Result<(Dataset,
};
let null_text = null_text_note(&info.fields, &null_text);
let not_utf8 = not_utf8_note(&info.fields, &not_utf8, src.text_encoding());
report.messages.extend(unread_dates.into_iter().chain(rounded).chain(null_text).map(|n| format!("{}: {n}", info.name)));
report.messages.extend(unread_dates.into_iter().chain(rounded).chain(null_text).map(|n| n.prefixed(&info.name)));
report.messages.extend(notes.prefixed(&info.name));
report.messages.extend(not_utf8.into_iter().map(|n| format!("{}: {n}", info.name)));
report.messages.extend(not_utf8.into_iter().map(|n| n.prefixed(&info.name)));
if ds.geometry_type == GeometryType::None
&& let Some(g) = ds.features.iter().find_map(|f| f.geometry.as_ref())
{
Expand Down
3 changes: 2 additions & 1 deletion crates/data/src/source/shp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ impl ShpSource {
notes.push("coordinates are not longitude/latitude; RustGIS reads them as WGS 84 and they may draw out of place".into());
}
let (names, not_utf8) = read_names(&dbf, table.columns.len(), &encoding)?;
let mut notes = crate::loss::Notes::from(notes);
notes.extend(crate::dbf::not_utf8_names_note(&not_utf8, encoding.name()));
let fields = table.columns.iter().zip(names).map(|(c, n)| Field::new(n, c.out)).collect();
let mut info = SourceInfo {
Expand All @@ -249,7 +250,7 @@ impl ShpSource {
// (`SourceInfo::coordinate_system`) as a whole read does.
crs: reproject.crs.definition_text(),
indexed: index.indexed(),
notes: notes.into(),
notes,
zm_dropped: false,
};
// PointZ … MultiPointM: the header's type says every shape carries Z or M values.
Expand Down
Loading
Loading