factorio-api-gen 0.3.2

Generates Rust Factorio API bindings from runtime-api.json and prototype-api.json for factorio-rs
Documentation
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use factorio_api_gen::{generate_from_bundled_api, generate_runtime_api, parse_runtime_api};

#[test]
fn bundled_runtime_api_parses() {
    let api = generate_from_bundled_api().expect("bundled runtime-api.json should parse");
    assert!(!api.events.is_empty());
    assert!(api.event_map.contains("event_type_to_name"));
    assert!(api.classes.contains("LuaGameScript"));
    assert!(api.globals.contains("pub static game"));
}

#[test]
fn emits_all_runtime_and_auxiliary_globals() {
    let generated = generate_from_bundled_api().expect("generate");
    let globals = &generated.globals;

    // `global_objects` from runtime-api.json
    for name in [
        "commands",
        "game",
        "helpers",
        "prototypes",
        "rcon",
        "remote",
        "rendering",
        "script",
        "settings",
    ] {
        let needle = format!("pub static {name}");
        assert!(
            globals.contains(&needle) || globals.contains(&format!("pub static {name} ")),
            "missing schema global `{name}`"
        );
    }

    // Auxiliary (not in global_objects)
    for name in ["storage", "serpent", "math", "string", "table"] {
        assert!(
            globals.contains(&format!("pub static {name}"))
                || globals.contains(&format!("pub static {name} ")),
            "missing auxiliary global `{name}`"
        );
    }

    // Global functions from the schema / libraries page
    for name in ["log", "localised_print", "table_size"] {
        assert!(
            globals.contains(&format!("pub fn {name}"))
                || globals.contains(&format!("pub fn {name} ")),
            "missing global function `{name}`"
        );
    }
}

#[test]
fn maps_events_to_rust_names() {
    let api = parse_runtime_api(factorio_api_gen::bundled_runtime_api_json())
        .expect("bundled runtime-api.json should parse");
    let generated = generate_runtime_api(&api);

    assert!(generated.events.contains("OnSingleplayerInit"));
    assert!(generated.event_map.contains("on_singleplayer_init"));
    assert!(generated.event_lookup.contains("OnSingleplayerInit"));
}

#[test]
fn nests_known_concepts_in_copy_fields() {
    let generated = generate_from_bundled_api().expect("generate");
    let concepts = &generated.concepts;

    assert!(
        concepts.contains("pub color : crate :: concepts :: Color")
            || concepts.contains("pub color: crate::concepts::Color"),
        "PrintSettings.color should be Color, got concepts without nested Color"
    );
    assert!(
        concepts.contains("pub left_top : crate :: concepts :: MapPosition")
            || concepts.contains("pub left_top: crate::concepts::MapPosition"),
        "BoundingBox.left_top should be MapPosition"
    );
    assert!(
        !concepts.contains("pub left_top : crate :: LuaAny")
            && !concepts.contains("pub left_top: crate::LuaAny"),
        "BoundingBox.left_top must not be LuaAny"
    );
}

#[test]
fn emits_luastruct_flag_sets_and_tags() {
    let generated = generate_from_bundled_api().expect("generate");
    let concepts = &generated.concepts;

    for name in [
        "GameViewSettings",
        "MapSettings",
        "DifficultySettings",
        "MouseButtonFlags",
        "Tags",
        "MapGenSize",
        "RenderLayer",
        "PropertyExpressionNames",
    ] {
        assert!(
            concepts.contains(&format!("pub struct {name}"))
                || concepts.contains(&format!("pub enum {name}")),
            "missing typed concept {name}"
        );
    }
    assert!(
        concepts.contains("pub fn is_flag_set_type"),
        "flag-set helper missing"
    );
    assert!(
        generated
            .classes
            .contains("fn tags (& self) -> crate :: concepts :: Tags")
            || generated
                .classes
                .contains("fn tags(&self) -> crate::concepts::Tags"),
        "LuaEntity/LuaGuiElement tags() should return concepts::Tags"
    );
}

#[test]
fn map_location_self_cycle_stays_lua_any() {
    let generated = generate_from_bundled_api().expect("generate");
    assert!(
        generated.concepts.contains("pub struct MapLocation"),
        "MapLocation should be generated"
    );
    assert!(
        generated
            .concepts
            .contains("pub position : crate :: LuaAny")
            || generated.concepts.contains("pub position: crate::LuaAny"),
        "MapLocation.position should stay LuaAny to preserve Copy"
    );
}

#[test]
fn emits_attribute_setters_without_write_only_getters() {
    let generated = generate_from_bundled_api().expect("generate");
    let classes = &generated.classes;
    let style_start = classes
        .find("impl LuaStyle")
        .expect("LuaStyle impl missing");
    let style_chunk = &classes[style_start..style_start.saturating_add(50_000)];
    let style = style_chunk.replace(' ', "");

    assert!(
        classes
            .replace(' ', "")
            .contains("pubfnset_caption(&self,value:implInto<crate::LocalisedString>)"),
        "LuaGuiElement.set_caption missing"
    );
    assert!(
        classes
            .replace(' ', "")
            .contains("pubfncaption(&self)->crate::LocalisedString"),
        "LuaGuiElement.caption getter missing"
    );
    assert!(
        style.contains("pubfnset_width(&self,value:i32)"),
        "LuaStyle.set_width missing"
    );
    assert!(
        !style.contains("pubfnwidth(&self)"),
        "write-only LuaStyle.width must not have a getter"
    );
    assert!(
        classes
            .replace(' ', "")
            .contains("pubfnwrite_driving(&self"),
        "LuaControl.driving setter should be write_driving (set_driving is a real method)"
    );
    let classes_compact = classes.replace(' ', "");
    assert!(
        classes_compact.contains("pubfnstyle(&self)->crate::classes::LuaStyle"),
        "LuaGuiElement.style getter should return LuaStyle"
    );
    assert!(
        classes_compact.contains("pubfnset_style(&self,value:&'staticstr)"),
        "LuaGuiElement.set_style should take a style name string"
    );
    assert!(
        classes.replace(' ', "").contains("pubdirection:Option<")
            && classes.contains("LuaGuiElementAddParams")
            && classes.contains("direction"),
        "LuaGuiElementAddParams should include variant field `direction`"
    );
    let lookup = generated.attribute_setters.replace(' ', "");
    assert!(
        lookup.contains("\"set_caption\"=>Some(\"caption\")"),
        "attribute setter lookup should map set_caption"
    );
}

#[test]
fn emits_numeric_concept_aliases() {
    let generated = generate_from_bundled_api().expect("generate");
    assert!(
        generated
            .concepts
            .contains("pub type RealOrientation = f32")
            || generated.concepts.contains("pub type RealOrientation=f32"),
        "RealOrientation should be a numeric alias"
    );
    assert!(
        generated.concepts.contains("pub type Weight = f64")
            || generated.concepts.contains("pub type Weight=f64"),
        "Weight should be a numeric alias"
    );
}

#[test]
fn emits_identification_enums() {
    let generated = generate_from_bundled_api().expect("generate");
    let concepts = &generated.concepts;

    assert!(
        concepts.contains("pub enum ForceID"),
        "ForceID enum missing"
    );
    assert!(
        concepts.contains("pub enum PlayerIdentification"),
        "PlayerIdentification enum missing"
    );
    assert!(
        concepts.contains("pub enum ScriptRenderTarget"),
        "ScriptRenderTarget enum missing"
    );
    assert!(
        concepts.contains("Force (crate :: classes :: LuaForce)")
            || concepts.contains("Force(crate::classes::LuaForce)"),
        "ForceID should have LuaForce arm"
    );
    assert!(
        concepts.contains("Position (crate :: concepts :: MapPosition)")
            || concepts.contains("Position(crate::concepts::MapPosition)"),
        "ScriptRenderTarget should have MapPosition arm"
    );
}

#[test]
fn optional_concept_fields_are_option() {
    let generated = generate_from_bundled_api().expect("generate");
    let concepts = &generated.concepts;
    assert!(
        concepts.contains("pub a : Option <")
            || concepts.contains("pub a: Option<")
            || concepts.contains("pub a : Option<"),
        "Color.a should be Option<_>, got concepts without Option a"
    );
    assert!(
        concepts.contains("pub color : Option <")
            || concepts.contains("pub color: Option<")
            || concepts.contains("pub color : Option<"),
        "PrintSettings.color should be Option<_>"
    );
}

#[test]
fn optional_takes_table_fields_are_option() {
    let generated = generate_from_bundled_api().expect("generate");
    let classes = &generated.classes;
    assert!(
        classes.contains("pub force : Option <")
            || classes.contains("pub force: Option<")
            || classes.contains("pub force : Option<"),
        "LuaEntityMineParams.force should be Option<_>"
    );
}

#[test]
fn emits_is_identification_type_helper() {
    let generated = generate_from_bundled_api().expect("generate");
    assert!(
        generated.debug_types.contains("fn is_identification_type"),
        "debug_types should expose is_identification_type"
    );
    assert!(
        generated.debug_types.contains("ForceID"),
        "is_identification_type should match ForceID"
    );
    assert!(
        generated.debug_types.contains("fn is_payload_ctor_type"),
        "debug_types should expose is_payload_ctor_type"
    );
}

#[test]
fn function_parameters_use_lua_function() {
    let generated = generate_from_bundled_api().expect("generate");
    let classes = &generated.classes;

    assert!(
        classes.contains("impl Into < crate :: LuaFunction >")
            || classes.contains("impl Into<crate::LuaFunction>"),
        "add_command-style handlers should accept Into<LuaFunction>"
    );
    // quote! may insert spaces around `::` / `<`.
    assert!(
        classes.contains("impl crate :: IntoOptionalLuaFunction")
            || classes.contains("impl crate::IntoOptionalLuaFunction"),
        "on_event-style handlers should accept IntoOptionalLuaFunction"
    );
    assert!(
        !classes.contains("handler : crate :: LuaAny")
            && !classes.contains("handler: crate::LuaAny"),
        "event handlers must not be typed as LuaAny"
    );
    assert!(
        classes.contains("filters : Option < Vec < crate :: EventFilterEntry > >")
            || classes.contains("filters: Option<Vec<crate::EventFilterEntry>>"),
        "on_event filters should be Option<Vec<EventFilterEntry>>"
    );
    assert!(
        classes.contains("impl Into < crate :: LocalisedString >")
            || classes.contains("impl Into<crate::LocalisedString>"),
        "LocalisedString parameters should accept Into<LocalisedString>"
    );
    // Sorted by Factorio `order`: name, help, function (not JSON array order).
    let add_command = classes
        .find("fn add_command")
        .map(|i| &classes[i..i.saturating_add(280)])
        .expect("add_command method");
    let name_at = add_command.find("name").expect("name param");
    let help_at = add_command.find("help").expect("help param");
    let function_at = add_command.find("function").expect("function param");
    assert!(
        name_at < help_at && help_at < function_at,
        "add_command parameters should follow Factorio order (name, help, function), got:\n{add_command}"
    );
}

#[test]
fn controller_state_setters_use_inline_structs() {
    let generated = generate_from_bundled_api().expect("generate");
    let classes = generated.classes.replace(' ', "");
    for setter in [
        "set_walking_state",
        "set_mining_state",
        "set_shooting_state",
        "set_repair_state",
    ] {
        let needle = format!("fn{setter}");
        let mut from = 0;
        let mut hits = 0;
        while let Some(rel) = classes[from..].find(&needle) {
            let at = from + rel;
            let window = &classes[at..classes.len().min(at + 80)];
            assert!(
                !window.contains("value:crate::LuaAny") && !window.contains("value:LuaAny"),
                "{setter} must not take LuaAny, got: {window}"
            );
            hits += 1;
            from = at + needle.len();
        }
        assert!(hits >= 1, "expected at least one {setter}");
    }
    assert!(
        classes.contains("value:LuaControlWalkingState"),
        "set_walking_state should take LuaControlWalkingState"
    );
}

#[test]
fn elem_value_and_filters_are_typed() {
    let generated = generate_from_bundled_api().expect("generate");
    let classes = &generated.classes;
    let concepts = &generated.concepts;
    let filters = &generated.event_filters;

    assert!(
        concepts.contains("enum ElemValue") || concepts.contains("pub enum ElemValue"),
        "concepts should emit ElemValue"
    );
    assert!(
        classes.contains("ElemValue")
            && (classes.contains("fn elem_value") || classes.contains("fn set_elem_value")),
        "elem_value should use ElemValue"
    );
    assert!(
        !classes.contains("fn set_elem_value(& self , value : crate :: LuaAny)")
            && !classes.contains("fn set_elem_value(&self, value: crate::LuaAny)"),
        "set_elem_value must not take LuaAny"
    );
    assert!(
        classes.contains("PrototypeFilterEntry")
            && (classes.contains("fn elem_filters") || classes.contains("fn set_elem_filters")),
        "elem_filters should use PrototypeFilterEntry"
    );
    assert!(
        filters.contains("struct PrototypeFilterEntry")
            || filters.contains("pub struct PrototypeFilterEntry"),
        "event_filters should define PrototypeFilterEntry"
    );
    assert!(
        filters.contains("struct EntityPrototypeFilter")
            || filters.contains("pub struct EntityPrototypeFilter"),
        "should emit EntityPrototypeFilter builders"
    );
}

#[test]
fn opened_uses_opened_target() {
    let generated = generate_from_bundled_api().expect("generate");
    let classes = generated.classes.replace(' ', "");
    let concepts = &generated.concepts;
    assert!(
        concepts.contains("enum OpenedTarget") || concepts.contains("pub enum OpenedTarget"),
        "concepts should emit OpenedTarget"
    );
    assert!(
        classes.contains("Option<crate::concepts::OpenedTarget>")
            || classes.contains("Option<crate::concepts::OpenedTarget>"),
        "opened should be Option<OpenedTarget>"
    );
    assert!(
        !classes.contains("fnset_opened(&self,value:crate::LuaAny)"),
        "set_opened must not take LuaAny"
    );
}