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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//! The state document and its fold: where the game is, not what the
//! scene is (spec/state.md). A save game is base + state declaration +
//! a player's session log — the fold below is the arithmetic.
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::oplog::OpLogEntry;
use crate::session::SessionOp;
/// `state.json` — the world's declared state and its initial values.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct StateDoc {
/// The state document's own version.
pub format_version: u32,
/// The declared fields: dotted name → `{type, initial}`.
pub fields: BTreeMap<String, StateField>,
}
/// One declared field.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateField {
/// The field's type: int, float, bool, string, map, list, json.
#[serde(rename = "type")]
pub kind: String,
/// The initial value; absent means the type's zero (or null for json).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub initial: Option<Value>,
}
impl StateField {
/// The value a reset returns to: the declared initial, else the
/// type's zero.
pub fn initial_value(&self) -> Value {
self.initial
.clone()
.unwrap_or_else(|| match self.kind.as_str() {
"int" | "float" => Value::from(0),
"bool" => Value::from(false),
"string" => Value::from(""),
"map" => serde_json::json!({}),
"list" => serde_json::json!([]),
_ => Value::Null,
})
}
}
/// A state fold's result.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct StateFold {
/// The values at the last entry.
pub values: BTreeMap<String, Value>,
/// Keys no declaration covers — carried, and said so, because
/// history must never break the fold.
pub undeclared: Vec<String>,
}
/// Fold a session log's `state` ops over a state document: the values at
/// the last entry. Separate from the document fold — state ops never
/// touch entities — and equally tolerant.
///
/// The rules (spec/state.md): a key naming a declared field sets it,
/// and `null` resets it to its initial; a key *under* a declared `map`
/// field sets or removes that entry; a key declaring nothing is carried
/// and reported.
pub fn fold_state(state_doc: &StateDoc, entries: &[OpLogEntry]) -> StateFold {
let mut values: BTreeMap<String, Value> = state_doc
.fields
.iter()
.map(|(name, field)| (name.clone(), field.initial_value()))
.collect();
let mut undeclared: Vec<String> = Vec::new();
for entry in entries {
for op in &entry.ops {
let SessionOp::State(record) = op else {
continue;
};
for (key, value) in &record.state {
if state_doc.fields.contains_key(key) {
match value {
Value::Null => {
values.insert(key.clone(), state_doc.fields[key].initial_value());
}
v => {
values.insert(key.clone(), v.clone());
}
}
continue;
}
if let Some((base, inner)) = key.split_once('.')
&& state_doc.fields.get(base).map(|f| f.kind.as_str()) == Some("map")
&& let Some(Value::Object(map)) = values.get_mut(base)
{
match value {
Value::Null => {
map.remove(inner);
}
v => {
map.insert(inner.to_string(), v.clone());
}
}
continue;
}
match value {
Value::Null => {
values.remove(key);
}
v => {
values.insert(key.clone(), v.clone());
if !undeclared.contains(key) {
undeclared.push(key.clone());
}
}
}
}
}
}
StateFold { values, undeclared }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::session::StateRecord;
fn entry(state: &[(&str, Value)]) -> OpLogEntry {
OpLogEntry {
revision: 1,
author: crate::author::Author {
peer: None,
name: "t".into(),
source: None,
},
ops: vec![SessionOp::State(StateRecord {
state: state
.iter()
.map(|(k, v)| (k.to_string(), v.clone()))
.collect(),
})],
timestamp_ms: 0,
id: None,
parent: None,
message: None,
}
}
/// spec/state.md: if a declared field exactly matches a dotted key,
/// the declared field ALWAYS takes precedence over map sub-keys.
/// Declaring both `inventory` (a map) and `inventory.rope` (an int)
/// is the whole game.
#[test]
fn a_declared_field_beats_the_map_subkey_it_resembles() {
let state_doc = StateDoc {
format_version: 1,
fields: BTreeMap::from([
(
"inventory".to_string(),
StateField {
kind: "map".into(),
initial: Some(serde_json::json!({})),
},
),
(
"inventory.rope".to_string(),
StateField {
kind: "int".into(),
initial: Some(serde_json::json!(0)),
},
),
]),
};
let folded = fold_state(
&state_doc,
&[entry(&[("inventory.rope", serde_json::json!(5))])],
);
assert_eq!(folded.values["inventory.rope"], serde_json::json!(5));
// The map kept its initial emptiness: not a sub-key hit.
assert_eq!(folded.values["inventory"], serde_json::json!({}));
assert!(folded.undeclared.is_empty());
// And a null on the declared field resets it, rather than
// deleting an inventory entry.
let folded = fold_state(&state_doc, &[entry(&[("inventory.rope", Value::Null)])]);
assert_eq!(folded.values["inventory.rope"], serde_json::json!(0));
}
#[test]
fn map_subkeys_still_work_without_a_shadowing_declaration() {
let state_doc = StateDoc {
format_version: 1,
fields: BTreeMap::from([(
"inventory".to_string(),
StateField {
kind: "map".into(),
initial: Some(serde_json::json!({})),
},
)]),
};
let folded = fold_state(
&state_doc,
&[entry(&[
("inventory.rope", serde_json::json!(1)),
("inventory.torch", serde_json::json!(2)),
("inventory.rope", Value::Null),
])],
);
assert_eq!(folded.values["inventory"], serde_json::json!({"torch": 2}));
}
}