Skip to content
Merged
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions crates/data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ ruzstd = { workspace = true }
lerc-reader = { workspace = true }
lerc-core = { workspace = true }

# A Shapefile's `.dbf` header holds the day it was written, which `dbase` reads from `time`'s
# clock. Without this feature that clock is std's, which panics in the browser, so a Shapefile
# export stopped the web app (opengeos/GeoLibre#3648); with it, the clock is JavaScript's.
[target.'cfg(target_arch = "wasm32")'.dependencies]
time = { version = "0.3", default-features = false, features = ["wasm-bindgen"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
rusqlite = { workspace = true }
postgres = { workspace = true }
Expand Down
37 changes: 37 additions & 0 deletions crates/data/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6042,6 +6042,43 @@ mod tests {
use super::*;
use std::io::Write;

/// A Shapefile's `.dbf` header holds the day it was written, which `dbase` takes from `time`'s
/// clock: std's unless `time`'s `wasm-bindgen` feature is on, and std's clock panics on
/// wasm32-unknown-unknown, so every Shapefile export stopped the web app with no message
/// (opengeos/GeoLibre#3648). The browser build can't run here, so this checks both halves on
/// the host: that writing a `.dbf` stamps today's date (it reads the clock), and that this
/// crate turns the feature on for wasm32.
#[test]
fn a_shapefile_dbf_is_stamped_with_the_day_it_was_written() {
let today = || {
let secs = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs();
let (y, m, d) = crate::date::civil_from_days(i64::try_from(secs / 86_400).unwrap());
(y - 1900, m, d)
};
let mut ds = Dataset::new("pts", GeometryType::Point, vec![Field::new("n", FieldType::Integer)]);
ds.push(Some(Geometry::Point(Coord::new(1.0, 2.0))), vec![Value::Int(1)]);
let before = today();
let (parts, _) = shapefile_parts_in(&ds, Crs::LonLat).unwrap();
let after = today();
let dbf = &parts.iter().find(|(e, _)| *e == "dbf").unwrap().1;
// Bytes 1 to 3: the year since 1900, the month and the day it was written (UTC), either
// side of a midnight passed during the write.
let stamped = (i64::from(dbf[1]), i64::from(dbf[2]), i64::from(dbf[3]));
assert!(stamped == before || stamped == after, "{stamped:?}, written between {before:?} and {after:?}");

// A plain text match, so the test needs no TOML parser: it is loud, not silent, if the
// section is ever written another way (a `[target.….dependencies.time]` table).
let manifest = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml")).replace("\r\n", "\n");
Comment thread
giswqs marked this conversation as resolved.
Resolved
let wasm = manifest
.split("\n[")
.find(|section| section.starts_with("target.'cfg(target_arch = \"wasm32\")'.dependencies]"))
.expect("crates/data/Cargo.toml has a [target.'cfg(target_arch = \"wasm32\")'.dependencies] section");
assert!(
wasm.lines().any(|l| l.starts_with("time ") && l.contains("\"wasm-bindgen\"")),
"time's wasm-bindgen feature is on for wasm32, or a Shapefile export panics in the browser:\n{wasm}"
);
}

/// A write to a name nothing holds reserves it first: a second write to it is refused before
/// anything is written, and dropping the write removes the reservation and the folder made for
/// it; once installed, both stay (#2865).
Expand Down
Loading