Handle duplicate files on web when they're both bundled in and uploaded to S3. In particular, don't crash on the all city picker on web.
//! Since the local filesystem can't be read from a web browser, instead bundle system data files in
//! the WASM binary using include_dir. For now, no support for saving files.
+use std::collections::BTreeSet;
use std::error::Error;
use serde::de::DeserializeOwned;
@@ -34,25 +35,24 @@ pub fn file_exists<I: Into<String>>(path: I) -> bool {
}
pub fn list_dir(dir: String) -> Vec<String> {
- let mut results = Vec::new();
+ let mut results = BTreeSet::new();
if let Some(dir) = SYSTEM_DATA.get_dir(dir.trim_start_matches("../data/system/")) {
for f in dir.files() {
- results.push(format!("../data/system/{}", f.path().display()));
+ results.insert(format!("../data/system/{}", f.path().display()));
}
} else {
error!("Can't list_dir({})", dir);
}
- // Merge with remote files
+ // Merge with remote files. Duplicates handled by BTreeSet.
let dir = dir.trim_start_matches("../");
for f in Manifest::load().entries.keys() {
if f.starts_with(dir) {
- results.push(format!("../{}", f));
+ results.insert(format!("../{}", f));
}
}
- results.sort();
- results
+ results.into_iter().collect()
}
pub fn slurp_file(path: &str) -> Result<Vec<u8>, Box<dyn Error>> {
@@ -75,7 +75,7 @@ pub fn maybe_read_binary<T: DeserializeOwned>(
}
pub fn write_json<T: Serialize>(_path: String, _obj: &T) {
- // TODO not yet
+ // TODO
}
pub fn write_binary<T: Serialize>(_path: String, _obj: &T) {
