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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//! Settings and migrations: the settings text, synced values and migration steps.
use std::collections::HashMap;
use crate::abi::Level;
use crate::abi::Str;
use crate::migrations::MigrationError;
use super::FakeHost;
use super::Installed;
use super::table;
impl FakeHost {
/// The settings file another mod `id` keeps, which
/// [`take_setting`](crate::migrations::Migration::take_setting) steps
/// read in [`migrate_settings`](Installed::migrate_settings).
///
/// # Examples
///
/// ```
/// use enshwick::migrations::Migrations;
///
/// let loader = enshwick::test::FakeHost::new()
/// .other_settings("me.comfort", "multiplier = 2.0\n")
/// .install();
/// # let _ = loader;
/// ```
#[must_use]
pub fn other_settings(mut self, id: &str, text: &str) -> Self {
self.other_settings.insert(id.to_owned(), text.to_owned());
self
}
/// The text of the mod's settings file; without it the mod reads its own
/// defaults, as on first start.
///
/// # Examples
///
/// See [`crate`].
#[must_use]
pub fn settings(mut self, text: &str) -> Self {
self.settings = Some(text.to_owned());
self
}
/// The path `Load::data_dir` returns; `mod-data/test` by default. The
/// fake creates nothing there.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().data_dir("target/mod-data/bonus").install();
/// # let _ = loader;
/// ```
#[must_use]
pub fn data_dir(mut self, path: &str) -> Self {
path.clone_into(&mut self.data_dir);
self
}
/// The most detailed level the loader keeps for the mod's lines.
///
/// # Examples
///
/// See [`FakeHost`].
#[must_use]
pub fn log_level(mut self, level: Level) -> Self {
self.log_level = level;
self
}
}
#[expect(
clippy::unused_self,
reason = "the fake's state is process-wide; `self` proves it is installed"
)]
impl Installed {
/// Replaces the text of the mod's settings file, as a player's edit
/// would; raise [`events::SettingsChanged`](crate::events::SettingsChanged)
/// after it.
///
/// # Examples
///
/// ```
/// use enshwick::events;
///
/// let loader = enshwick::test::FakeHost::new().settings("limit = 7\n").install();
/// loader.set_settings("limit = 9\n");
/// assert_eq!(loader.raise::<events::SettingsChanged>(&()), 0);
/// ```
pub fn set_settings(&self, text: &str) {
table::with((), |s| s.host.settings = Some(text.to_owned()));
}
/// Sets the join values of the server this client joined, which the mod
/// gets over its file's on its next read, or `None` for none, as after
/// joining a server without them; raise
/// [`events::SettingsChanged`](crate::events::SettingsChanged)
/// after it, as the loader does.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// loader.set_server_synced(Some("{ altars = [2, 4] }"));
/// assert!(loader.reported_synced().is_empty());
/// ```
pub fn set_server_synced(&self, synced: Option<&str>) {
table::with((), |s| s.host.server_synced = synced.map(str::to_owned));
}
/// The join values the mod reported each time it read its settings, as
/// its file holds them.
///
/// # Examples
///
/// See [`Installed::set_server_synced`].
#[must_use]
pub fn reported_synced(&self) -> Vec<String> {
table::with(Vec::new(), |s| s.synced.clone())
}
/// The defaults the mod passed each time it read its settings.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.settings_defaults().is_empty());
/// ```
#[must_use]
pub fn settings_defaults(&self) -> Vec<String> {
table::with(Vec::new(), |s| s.defaults.clone())
}
/// The settings schema the mod handed the loader, which
/// [`Load::config`](crate::Load::config) does; `None` before.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.config_schema().is_none());
/// ```
#[must_use]
pub fn config_schema(&self) -> Option<crate::config::Schema> {
table::with(None, |s| s.config_schema.clone())
.and_then(|text| crate::config::Schema::parse(&text).ok())
}
/// Runs the mod's check of a new settings text, as the loader does
/// before it writes a change: `Ok` when the mod accepts `text`, else
/// its reason; `Ok` for a mod without a check.
///
/// # Errors
///
/// [`SettingsRefused`] with the mod's reason for refusing `text`.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert_eq!(loader.check_settings("x = 1"), Ok(()));
/// ```
pub fn check_settings(&self, text: &str) -> Result<(), SettingsRefused> {
let Some((callback, user)) = table::with(None, |s| s.config_check) else {
return Ok(());
};
let mut why = Str::EMPTY;
// SAFETY: the mod's check with its own pointer; the text lives for
// the call.
let status = unsafe {
callback(
Str::new(text),
&raw mut why,
core::ptr::with_exposed_provenance_mut(user),
)
};
if status.is_ok() {
return Ok(());
}
// SAFETY: the reason stays valid until the mod's next call.
let reason = unsafe { why.as_str() }.unwrap_or("refused").to_owned();
Err(SettingsRefused { reason })
}
/// The ids of the migrations the mod declared, in order.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert!(loader.migrations().is_empty());
/// ```
#[must_use]
pub fn migrations(&self) -> Vec<String> {
table::with(Vec::new(), |s| {
s.migrations
.as_ref()
.map(|m| m.list.iter().map(|(id, _)| id.clone()).collect())
.unwrap_or_default()
})
}
/// Runs the settings steps of every migration the mod declared on the
/// settings text `old`, as the loader does on a file an older version
/// wrote; the new text, without comments.
///
/// # Errors
///
/// Why a step failed: the text is no TOML, or a step's map refused.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// assert_eq!(loader.migrate_settings("a = 1\n"), Ok("a = 1\n".to_owned()));
/// ```
pub fn migrate_settings(&self, old: &str) -> Result<String, MigrationError> {
use crate::abi::MigrationStep as S;
let migrations = table::with(None, |s| s.migrations.clone());
let others = table::with(HashMap::new(), |s| s.host.other_settings.clone());
let mut table: toml::Table = toml::from_str(old)?;
let list = migrations.as_ref().map_or(&[][..], |m| &m.list[..]);
for (index, (_, steps)) in list.iter().enumerate() {
for (step, (kind, from, to)) in steps.iter().enumerate() {
match *kind {
S::TAKE_SETTING => take(&mut table, from, to, &others)?,
S::RENAME_SETTING => {
if crate::config::value_at(&table, to).is_none()
&& let Some(value) = remove_path(&mut table, from)
{
set_path(&mut table, to, value)?;
}
}
S::REMOVE_SETTING => {
let _gone = remove_path(&mut table, from);
}
S::MAP_SETTING => {
let (Some(value), Some(map)) = (table.get(from), migrations.as_ref())
else {
continue;
};
let text = crate::migrations::write_value(value)?;
let new = map
.call(index, step, &text)
.map_err(|e| MigrationError::new(format!("setting {from}: {e}")))?;
table.insert(from.clone(), crate::migrations::parse_value(&new)?);
}
_ => {}
}
}
}
toml::to_string(&table).map_err(|e| MigrationError::new(e.to_string()))
}
/// Runs the store steps of every migration the mod declared on the
/// world store `name` holding `old`, following renames; the name and
/// text it ends with.
///
/// # Errors
///
/// Why a step's map refused.
///
/// # Examples
///
/// ```
/// let loader = enshwick::test::FakeHost::new().install();
/// let migrated = loader.migrate_store("owners", "count = 1\n");
/// assert_eq!(migrated, Ok(("owners".to_owned(), "count = 1\n".to_owned())));
/// ```
pub fn migrate_store(&self, name: &str, old: &str) -> Result<(String, String), MigrationError> {
use crate::abi::MigrationStep as S;
let migrations = table::with(None, |s| s.migrations.clone());
let (mut name, mut text) = (name.to_owned(), old.to_owned());
let list = migrations.as_ref().map_or(&[][..], |m| &m.list[..]);
for (index, (_, steps)) in list.iter().enumerate() {
for (step, (kind, from, to)) in steps.iter().enumerate() {
match *kind {
S::RENAME_STORE if *from == name => name.clone_from(to),
S::MAP_STORE if *from == name => {
let Some(map) = migrations.as_ref() else {
continue;
};
text = map
.call(index, step, &text)
.map_err(|e| MigrationError::new(format!("store {from}: {e}")))?;
}
_ => {}
}
}
}
Ok((name, text))
}
}
/// A mod's check refused a settings text, as [`Installed::check_settings`]
/// reports it.
///
/// # Examples
///
/// See [`Installed::check_settings`].
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("{reason}")]
pub struct SettingsRefused {
/// The mod's reason.
pub reason: String,
}
/// A migration step as the fake keeps it: kind, from, to.
pub(crate) type FakeStep = (u32, String, String);
/// The migrations a mod declared, as the fake keeps them: id and steps as
/// `(kind, from, to)`, and the mod's map.
#[derive(Clone, Debug)]
pub(crate) struct FakeMigrations {
pub(crate) list: Vec<(String, Vec<FakeStep>)>,
pub(crate) map: crate::abi::MigrateFn,
pub(crate) user: usize,
}
impl FakeMigrations {
/// Runs the map of step `step` of migration `migration` on `input`.
fn call(&self, migration: usize, step: usize, input: &str) -> Result<String, MigrationError> {
let (Ok(migration), Ok(step)) = (u32::try_from(migration), u32::try_from(step)) else {
return Err(MigrationError::new("too many steps"));
};
let mut out = Str::EMPTY;
// SAFETY: the mod's map, with its pointer and a text valid for the call.
let status = unsafe {
(self.map)(
core::ptr::with_exposed_provenance_mut(self.user),
migration,
step,
Str::new(input),
&raw mut out,
)
};
// SAFETY: the text stays valid until the next call.
let text = unsafe { out.as_str() }.unwrap_or_default().to_owned();
if status.is_ok() {
Ok(text)
} else {
Err(MigrationError::new(text))
}
}
}
/// Sets the dotted key `to` of `table` from `from`, `<mod id>:<key>`, in the
/// settings of `others`, as the loader's take step does.
fn take(
table: &mut toml::Table,
from: &str,
to: &str,
others: &HashMap<String, String>,
) -> Result<(), MigrationError> {
let (id, key) = from
.split_once(':')
.ok_or_else(|| MigrationError::new(format!("setting {from}: not <mod id>:<key>")))?;
let Some(text) = others.get(id) else {
return Ok(());
};
let other: toml::Table = toml::from_str(text)?;
let mut parts = key.split('.');
let mut value = parts.next().and_then(|first| other.get(first));
for part in parts {
value = value.and_then(|v| v.get(part));
}
let Some(value) = value.cloned() else {
return Ok(());
};
set_path(table, to, value)
}
/// Sets the dotted key `key` of `table` to `value`, making missing tables.
fn set_path(table: &mut toml::Table, key: &str, value: toml::Value) -> Result<(), MigrationError> {
let (path, leaf) = key.rsplit_once('.').map_or(("", key), |(p, l)| (p, l));
let mut at = table;
for part in path.split('.').filter(|p| !p.is_empty()) {
at = at
.entry(part)
.or_insert_with(|| toml::Value::Table(toml::Table::new()))
.as_table_mut()
.ok_or_else(|| MigrationError::new(format!("setting {key}: {part} is not a table")))?;
}
at.insert(leaf.to_owned(), value);
Ok(())
}
/// Takes the dotted key `key` out of `table`.
fn remove_path(table: &mut toml::Table, key: &str) -> Option<toml::Value> {
let (path, leaf) = key.rsplit_once('.').map_or(("", key), |(p, l)| (p, l));
let mut at = table;
for part in path.split('.').filter(|p| !p.is_empty()) {
at = at.get_mut(part)?.as_table_mut()?;
}
at.remove(leaf)
}