1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::collections::BTreeMap;
use std::error::Error;
use serde::de::DeserializeOwned;
use crate::{basename, list_dir, maybe_read_binary, parent_path, slurp_file, Timer};
pub fn maybe_read_json<T: DeserializeOwned>(
path: String,
timer: &mut Timer,
) -> Result<T, Box<dyn Error>> {
if !path.ends_with(".json") && !path.ends_with(".geojson") {
panic!("read_json needs {} to end with .json or .geojson", path);
}
timer.start(format!("parse {}", path));
let result: Result<T, Box<dyn Error>> =
slurp_file(&path).and_then(|raw| serde_json::from_slice(&raw).map_err(|x| x.into()));
timer.stop(format!("parse {}", path));
result
}
pub fn read_json<T: DeserializeOwned>(path: String, timer: &mut Timer) -> T {
match maybe_read_json(path.clone(), timer) {
Ok(obj) => obj,
Err(err) => panic!("Couldn't read_json({}): {}", path, err),
}
}
pub fn read_binary<T: DeserializeOwned>(path: String, timer: &mut Timer) -> T {
match maybe_read_binary(path.clone(), timer) {
Ok(obj) => obj,
Err(err) => panic!("Couldn't read_binary({}): {}", path, err),
}
}
pub fn read_object<T: DeserializeOwned>(
path: String,
timer: &mut Timer,
) -> Result<T, Box<dyn Error>> {
if path.ends_with(".bin") {
maybe_read_binary(path, timer)
} else {
maybe_read_json(path, timer)
}
}
pub fn find_prev_file(orig: String) -> Option<String> {
let mut files = list_dir(parent_path(&orig));
files.reverse();
files.into_iter().find(|f| *f < orig)
}
pub fn find_next_file(orig: String) -> Option<String> {
let files = list_dir(parent_path(&orig));
files.into_iter().find(|f| *f > orig)
}
pub fn load_all_objects<T: DeserializeOwned>(dir: String) -> Vec<(String, T)> {
let mut timer = Timer::new(format!("load_all_objects from {}", dir));
let mut tree: BTreeMap<String, T> = BTreeMap::new();
for path in list_dir(dir) {
match read_object(path.clone(), &mut timer) {
Ok(obj) => {
tree.insert(basename(&path), obj);
}
Err(err) => {
error!("Couldn't load {}: {}", path, err);
}
}
}
tree.into_iter().collect()
}
pub fn list_all_objects(dir: String) -> Vec<String> {
list_dir(dir).into_iter().map(|x| basename(&x)).collect()
}