Skip to main content

factorio_api/
lib.rs

1//! Generated Factorio runtime API bindings.
2//!
3//! These types exist for Rust type-checking and IDE support. Mod code is
4//! transpiled to Lua and never executes these stub implementations.
5
6#![allow(
7    dead_code,
8    unused_imports,
9    unused_variables,
10    non_upper_case_globals,
11    clippy::all,
12    clippy::pedantic,
13    clippy::nursery
14)]
15
16/// Marker trait for autogenerated Factorio API types.
17/// Do not implement this trait manually.
18pub trait LuaObject {}
19
20/// Opaque placeholder for complex Factorio Lua API values.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
22pub struct LuaAny;
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
25pub struct LuaStorage;
26
27impl LuaStorage {
28    #[must_use]
29    pub const fn new() -> Self {
30        Self
31    }
32}
33
34impl From<LuaStorage> for LuaAny {
35    fn from(_: LuaStorage) -> Self {
36        LuaAny
37    }
38}
39
40impl std::ops::Index<&str> for LuaStorage {
41    type Output = LuaAny;
42
43    fn index(&self, _key: &str) -> &LuaAny {
44        &LUA_ANY
45    }
46}
47
48const LUA_ANY: LuaAny = LuaAny;
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
51pub struct Serpent;
52
53impl Serpent {
54    #[must_use]
55    pub const fn new() -> Self {
56        Self
57    }
58
59    /// Multi-line pretty-print (`serpent.block`).
60    #[allow(unused_variables)]
61    pub fn block(&self, value: impl Into<LuaAny>) -> &'static str {
62        ""
63    }
64
65    /// Single-line pretty-print (`serpent.line`).
66    #[allow(unused_variables)]
67    pub fn line(&self, value: impl Into<LuaAny>) -> &'static str {
68        ""
69    }
70
71    /// Serialize to a string that `serpent.load` can revive (`serpent.dump`).
72    #[allow(unused_variables)]
73    pub fn dump(&self, value: impl Into<LuaAny>) -> &'static str {
74        ""
75    }
76}
77
78#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
79pub struct LuaFunction;
80
81impl LuaFunction {
82    #[must_use]
83    pub const fn new() -> Self {
84        Self
85    }
86}
87
88impl From<LuaFunction> for LuaAny {
89    fn from(_: LuaFunction) -> Self {
90        LuaAny
91    }
92}
93
94/// Factorio [`LocalisedString`](https://lua-api.factorio.com/latest/concepts/LocalisedString.html):
95/// a plain string or a translation table `{ "category.key", arg1, ... }`.
96///
97/// Pass a string, or an array of strings (locale key plus `__1__` / `__2__` args).
98/// `.into()` is transparent when lowering, so `[key, name].into()` becomes
99/// `{ key, name }` in Lua.
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
101pub struct LocalisedString;
102
103impl From<LocalisedString> for LuaAny {
104    fn from(_: LocalisedString) -> Self {
105        LuaAny
106    }
107}
108
109impl<'a> From<&'a str> for LocalisedString {
110    fn from(_: &'a str) -> Self {
111        Self
112    }
113}
114
115impl From<String> for LocalisedString {
116    fn from(_: String) -> Self {
117        Self
118    }
119}
120
121impl<'a, const N: usize> From<[&'a str; N]> for LocalisedString {
122    fn from(_: [&'a str; N]) -> Self {
123        Self
124    }
125}
126
127impl<R> From<fn() -> R> for LuaFunction {
128    fn from(_: fn() -> R) -> Self {
129        LuaFunction
130    }
131}
132
133impl<A, R> From<fn(A) -> R> for LuaFunction {
134    fn from(_: fn(A) -> R) -> Self {
135        LuaFunction
136    }
137}
138
139impl<A, B, R> From<fn(A, B) -> R> for LuaFunction {
140    fn from(_: fn(A, B) -> R) -> Self {
141        LuaFunction
142    }
143}
144
145impl<A, B, C, R> From<fn(A, B, C) -> R> for LuaFunction {
146    fn from(_: fn(A, B, C) -> R) -> Self {
147        LuaFunction
148    }
149}
150
151impl<A, B, C, D, R> From<fn(A, B, C, D) -> R> for LuaFunction {
152    fn from(_: fn(A, B, C, D) -> R) -> Self {
153        LuaFunction
154    }
155}
156
157/// Coerce a Rust `fn` item or closure to [`LuaFunction`].
158///
159/// Prefer this for closures (`lua_fn(|e| { ... })`). Fn items also implement
160/// [`From`] for [`LuaFunction`] directly for common arities.
161#[must_use]
162pub fn lua_fn<F, A, R>(_f: F) -> LuaFunction
163where
164    F: Fn(A) -> R,
165{
166    LuaFunction
167}
168
169#[must_use]
170pub fn lua_fn0<F, R>(_f: F) -> LuaFunction
171where
172    F: Fn() -> R,
173{
174    LuaFunction
175}
176
177#[must_use]
178pub fn lua_fn2<F, A, B, R>(_f: F) -> LuaFunction
179where
180    F: Fn(A, B) -> R,
181{
182    LuaFunction
183}
184
185pub trait IntoOptionalLuaFunction {
186    fn into_optional_lua_function(self) -> Option<LuaFunction>;
187}
188
189impl IntoOptionalLuaFunction for LuaFunction {
190    fn into_optional_lua_function(self) -> Option<LuaFunction> {
191        Some(self)
192    }
193}
194
195impl IntoOptionalLuaFunction for Option<LuaFunction> {
196    fn into_optional_lua_function(self) -> Option<LuaFunction> {
197        self
198    }
199}
200
201impl<R> IntoOptionalLuaFunction for fn() -> R {
202    fn into_optional_lua_function(self) -> Option<LuaFunction> {
203        Some(self.into())
204    }
205}
206
207impl<A, R> IntoOptionalLuaFunction for fn(A) -> R {
208    fn into_optional_lua_function(self) -> Option<LuaFunction> {
209        Some(self.into())
210    }
211}
212
213impl<A, B, R> IntoOptionalLuaFunction for fn(A, B) -> R {
214    fn into_optional_lua_function(self) -> Option<LuaFunction> {
215        Some(self.into())
216    }
217}
218
219impl<A, B, C, R> IntoOptionalLuaFunction for fn(A, B, C) -> R {
220    fn into_optional_lua_function(self) -> Option<LuaFunction> {
221        Some(self.into())
222    }
223}
224
225impl<A, B, C, D, R> IntoOptionalLuaFunction for fn(A, B, C, D) -> R {
226    fn into_optional_lua_function(self) -> Option<LuaFunction> {
227        Some(self.into())
228    }
229}
230
231/// Index-or-name parameter used by APIs such as `game.get_player` /
232/// `game.get_surface` (`uint32 | string` in the Factorio schema).
233#[derive(Debug, Clone, Copy, PartialEq, Eq)]
234pub enum IndexOrName {
235    Index(u32),
236    Name(&'static str),
237}
238
239impl Default for IndexOrName {
240    fn default() -> Self {
241        Self::Index(0)
242    }
243}
244
245impl From<u32> for IndexOrName {
246    fn from(value: u32) -> Self {
247        Self::Index(value)
248    }
249}
250
251impl From<&'static str> for IndexOrName {
252    fn from(value: &'static str) -> Self {
253        Self::Name(value)
254    }
255}
256
257impl From<IndexOrName> for LuaAny {
258    fn from(_: IndexOrName) -> Self {
259        LuaAny
260    }
261}
262
263// Primitive types usable as LuaAny positions via `.into()`.
264impl<'a> From<&'a str> for LuaAny {
265    fn from(_: &'a str) -> Self {
266        LuaAny
267    }
268}
269impl From<String> for LuaAny {
270    fn from(_: String) -> Self {
271        LuaAny
272    }
273}
274impl From<bool> for LuaAny {
275    fn from(_: bool) -> Self {
276        LuaAny
277    }
278}
279impl From<u8> for LuaAny {
280    fn from(_: u8) -> Self {
281        LuaAny
282    }
283}
284impl From<u16> for LuaAny {
285    fn from(_: u16) -> Self {
286        LuaAny
287    }
288}
289impl From<u32> for LuaAny {
290    fn from(_: u32) -> Self {
291        LuaAny
292    }
293}
294impl From<u64> for LuaAny {
295    fn from(_: u64) -> Self {
296        LuaAny
297    }
298}
299impl From<i8> for LuaAny {
300    fn from(_: i8) -> Self {
301        LuaAny
302    }
303}
304impl From<i16> for LuaAny {
305    fn from(_: i16) -> Self {
306        LuaAny
307    }
308}
309impl From<i32> for LuaAny {
310    fn from(_: i32) -> Self {
311        LuaAny
312    }
313}
314impl From<i64> for LuaAny {
315    fn from(_: i64) -> Self {
316        LuaAny
317    }
318}
319impl From<f32> for LuaAny {
320    fn from(_: f32) -> Self {
321        LuaAny
322    }
323}
324impl From<f64> for LuaAny {
325    fn from(_: f64) -> Self {
326        LuaAny
327    }
328}
329impl From<usize> for LuaAny {
330    fn from(_: usize) -> Self {
331        LuaAny
332    }
333}
334
335// Concept types usable as LuaAny positions.
336impl From<crate::concepts::MapPosition> for LuaAny {
337    fn from(_: crate::concepts::MapPosition) -> Self {
338        LuaAny
339    }
340}
341impl From<crate::concepts::BoundingBox> for LuaAny {
342    fn from(_: crate::concepts::BoundingBox) -> Self {
343        LuaAny
344    }
345}
346impl From<crate::concepts::Color> for LuaAny {
347    fn from(_: crate::concepts::Color) -> Self {
348        LuaAny
349    }
350}
351
352impl std::ops::Index<&str> for LuaAny {
353    type Output = crate::settings::ModSettingValue;
354    fn index(&self, _key: &str) -> &Self::Output {
355        &crate::settings::UNIT_MOD_SETTING
356    }
357}
358
359/// Common "virtual fields" on `LuaAny` values.
360impl LuaAny {
361    pub fn x(self) -> f64 {
362        0.0
363    }
364    pub fn y(self) -> f64 {
365        0.0
366    }
367    pub fn left_top(self) -> LuaAny {
368        LuaAny
369    }
370    pub fn right_bottom(self) -> LuaAny {
371        LuaAny
372    }
373    pub fn value(self) -> LuaAny {
374        LuaAny
375    }
376}
377
378/// Marker trait for types that can be used as Factorio mod setting values.
379pub trait SettingValue: Copy + 'static {
380    const STUB: Self;
381}
382
383impl SettingValue for bool {
384    const STUB: Self = false;
385}
386impl SettingValue for u8 {
387    const STUB: Self = 0;
388}
389impl SettingValue for u16 {
390    const STUB: Self = 0;
391}
392impl SettingValue for u32 {
393    const STUB: Self = 0;
394}
395impl SettingValue for u64 {
396    const STUB: Self = 0;
397}
398impl SettingValue for usize {
399    const STUB: Self = 0;
400}
401impl SettingValue for i8 {
402    const STUB: Self = 0;
403}
404impl SettingValue for i16 {
405    const STUB: Self = 0;
406}
407impl SettingValue for i32 {
408    const STUB: Self = 0;
409}
410impl SettingValue for i64 {
411    const STUB: Self = 0;
412}
413impl SettingValue for f32 {
414    const STUB: Self = 0.0;
415}
416impl SettingValue for f64 {
417    const STUB: Self = 0.0;
418}
419impl SettingValue for &'static str {
420    const STUB: Self = "";
421}
422
423impl LuaAny {
424    /// Read a typed mod setting value by name.
425    ///
426    /// # Example
427    /// ```ignore
428    /// const CASUAL_MODE: bool = settings.startup.get::<bool>("ms-casual-mode");
429    /// const SPAWN_RADIUS: i64 = settings.startup.get::<i64>("ms-spawn-radius");
430    /// ```
431    pub const fn get<T: SettingValue>(&self, _key: &str) -> T {
432        T::STUB
433    }
434}
435
436include!(concat!(env!("OUT_DIR"), "/mod.rs"));
437
438pub mod lua_libs;
439pub mod settings;
440
441pub use event_filters::{EventFilterEntry, FilterMethodSpec, filter_method_spec};
442pub use lua_libs::{LuaMath, LuaStringLib, LuaTableLib};
443pub use map::{event_filter_type, event_type_to_name};
444pub use unions::literal_enum_variant_str;
445
446pub mod prelude {
447    pub use crate::LocalisedString;
448    pub use crate::SettingValue;
449    pub use crate::concepts::*;
450    pub use crate::event_data::*;
451    pub use crate::event_filters::*;
452    pub use crate::event_type_to_name;
453    pub use crate::events::*;
454    pub use crate::globals::*;
455    pub use crate::lua_fn;
456    pub use crate::lua_fn0;
457    pub use crate::lua_fn2;
458    pub use crate::settings::{
459        BoolSetting, DoubleSetting, IntSetting, ModSettingValue, StringSetting, data, settings,
460    };
461    pub use crate::unions::*;
462}