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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use crate::PermanentMapEdits;
use serde_json::Value;
pub fn upgrade(mut value: Value) -> Result<PermanentMapEdits, String> {
if value.get("version").is_none() {
fix_offset(&mut value);
fix_intersection_ids(&mut value);
value
.as_object_mut()
.unwrap()
.insert("version".to_string(), Value::Number(0.into()));
}
if value["version"] == Value::Number(0.into()) {
fix_road_direction(&mut value);
}
abstutil::from_json(&value.to_string().into_bytes()).map_err(|x| x.to_string())
}
fn walk<F: Fn(&mut serde_json::Map<String, Value>) -> bool>(value: &mut Value, transform: &F) {
match value {
Value::Array(list) => {
for x in list {
walk(x, transform);
}
}
Value::Object(map) => {
if !(transform)(map) {
for x in map.values_mut() {
walk(x, transform);
}
}
}
_ => {}
}
}
fn fix_offset(value: &mut Value) {
walk(value, &|map| {
if map.len() == 1 && map.contains_key("TrafficSignal") {
let ts = map
.get_mut("TrafficSignal")
.unwrap()
.as_object_mut()
.unwrap();
if ts.get("offset_seconds").is_none() {
ts.insert("offset_seconds".to_string(), Value::Number(0.into()));
}
true
} else {
false
}
})
}
fn fix_intersection_ids(value: &mut Value) {
match value {
Value::Array(list) => {
for x in list {
fix_intersection_ids(x);
}
}
Value::Object(map) => {
if map.len() == 1 && map.contains_key("osm_node_id") {
*value = Value::Number(map["osm_node_id"].as_i64().unwrap().into());
} else {
for x in map.values_mut() {
fix_intersection_ids(x);
}
}
}
_ => {}
}
}
fn fix_road_direction(value: &mut Value) {
walk(value, &|map| {
if map.contains_key("num_fwd") {
map.insert(
"dir".to_string(),
if map["fwd"].as_bool().unwrap() {
"Fwd".into()
} else {
"Back".into()
},
);
true
} else {
false
}
});
}