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/// Index-or-name parameter used by APIs such as `game.get_player` /
25/// `game.get_surface` (`uint32 | string` in the Factorio schema).
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub enum IndexOrName {
28    Index(u32),
29    Name(&'static str),
30}
31
32impl Default for IndexOrName {
33    fn default() -> Self {
34        Self::Index(0)
35    }
36}
37
38impl From<u32> for IndexOrName {
39    fn from(value: u32) -> Self {
40        Self::Index(value)
41    }
42}
43
44impl From<&'static str> for IndexOrName {
45    fn from(value: &'static str) -> Self {
46        Self::Name(value)
47    }
48}
49
50impl From<IndexOrName> for LuaAny {
51    fn from(_: IndexOrName) -> Self {
52        LuaAny
53    }
54}
55
56// Primitive types usable as LuaAny positions via `.into()`.
57impl<'a> From<&'a str> for LuaAny {
58    fn from(_: &'a str) -> Self {
59        LuaAny
60    }
61}
62impl From<String> for LuaAny {
63    fn from(_: String) -> Self {
64        LuaAny
65    }
66}
67impl From<bool> for LuaAny {
68    fn from(_: bool) -> Self {
69        LuaAny
70    }
71}
72impl From<u8> for LuaAny {
73    fn from(_: u8) -> Self {
74        LuaAny
75    }
76}
77impl From<u16> for LuaAny {
78    fn from(_: u16) -> Self {
79        LuaAny
80    }
81}
82impl From<u32> for LuaAny {
83    fn from(_: u32) -> Self {
84        LuaAny
85    }
86}
87impl From<u64> for LuaAny {
88    fn from(_: u64) -> Self {
89        LuaAny
90    }
91}
92impl From<i8> for LuaAny {
93    fn from(_: i8) -> Self {
94        LuaAny
95    }
96}
97impl From<i16> for LuaAny {
98    fn from(_: i16) -> Self {
99        LuaAny
100    }
101}
102impl From<i32> for LuaAny {
103    fn from(_: i32) -> Self {
104        LuaAny
105    }
106}
107impl From<i64> for LuaAny {
108    fn from(_: i64) -> Self {
109        LuaAny
110    }
111}
112impl From<f32> for LuaAny {
113    fn from(_: f32) -> Self {
114        LuaAny
115    }
116}
117impl From<f64> for LuaAny {
118    fn from(_: f64) -> Self {
119        LuaAny
120    }
121}
122impl From<usize> for LuaAny {
123    fn from(_: usize) -> Self {
124        LuaAny
125    }
126}
127
128// Concept types usable as LuaAny positions.
129impl From<crate::concepts::MapPosition> for LuaAny {
130    fn from(_: crate::concepts::MapPosition) -> Self {
131        LuaAny
132    }
133}
134impl From<crate::concepts::BoundingBox> for LuaAny {
135    fn from(_: crate::concepts::BoundingBox) -> Self {
136        LuaAny
137    }
138}
139impl From<crate::concepts::Color> for LuaAny {
140    fn from(_: crate::concepts::Color) -> Self {
141        LuaAny
142    }
143}
144
145impl std::ops::Index<&str> for LuaAny {
146    type Output = crate::settings::ModSettingValue;
147    fn index(&self, _key: &str) -> &Self::Output {
148        &crate::settings::UNIT_MOD_SETTING
149    }
150}
151
152/// Common "virtual fields" on `LuaAny` values.
153impl LuaAny {
154    pub fn x(self) -> f64 {
155        0.0
156    }
157    pub fn y(self) -> f64 {
158        0.0
159    }
160    pub fn left_top(self) -> LuaAny {
161        LuaAny
162    }
163    pub fn right_bottom(self) -> LuaAny {
164        LuaAny
165    }
166    pub fn value(self) -> LuaAny {
167        LuaAny
168    }
169}
170
171/// Marker trait for types that can be used as Factorio mod setting values.
172pub trait SettingValue: Copy + 'static {
173    const STUB: Self;
174}
175
176impl SettingValue for bool {
177    const STUB: Self = false;
178}
179impl SettingValue for u8 {
180    const STUB: Self = 0;
181}
182impl SettingValue for u16 {
183    const STUB: Self = 0;
184}
185impl SettingValue for u32 {
186    const STUB: Self = 0;
187}
188impl SettingValue for u64 {
189    const STUB: Self = 0;
190}
191impl SettingValue for usize {
192    const STUB: Self = 0;
193}
194impl SettingValue for i8 {
195    const STUB: Self = 0;
196}
197impl SettingValue for i16 {
198    const STUB: Self = 0;
199}
200impl SettingValue for i32 {
201    const STUB: Self = 0;
202}
203impl SettingValue for i64 {
204    const STUB: Self = 0;
205}
206impl SettingValue for f32 {
207    const STUB: Self = 0.0;
208}
209impl SettingValue for f64 {
210    const STUB: Self = 0.0;
211}
212impl SettingValue for &'static str {
213    const STUB: Self = "";
214}
215
216impl LuaAny {
217    /// Read a typed mod setting value by name.
218    ///
219    /// # Example
220    /// ```ignore
221    /// const CASUAL_MODE: bool = settings.startup.get::<bool>("ms-casual-mode");
222    /// const SPAWN_RADIUS: i64 = settings.startup.get::<i64>("ms-spawn-radius");
223    /// ```
224    pub const fn get<T: SettingValue>(&self, _key: &str) -> T {
225        T::STUB
226    }
227}
228
229include!(concat!(env!("OUT_DIR"), "/mod.rs"));
230
231pub mod settings;
232
233pub use event_filters::{EventFilterEntry, FilterMethodSpec, filter_method_spec};
234pub use map::{event_filter_type, event_type_to_name};
235pub use unions::literal_enum_variant_str;
236
237pub mod prelude {
238    pub use crate::SettingValue;
239    pub use crate::concepts::*;
240    pub use crate::event_data::*;
241    pub use crate::event_filters::*;
242    pub use crate::event_type_to_name;
243    pub use crate::events::*;
244    pub use crate::globals::*;
245    pub use crate::settings::{
246        BoolSetting, DoubleSetting, IntSetting, ModSettingValue, StringSetting, data, settings,
247    };
248    pub use crate::unions::*;
249}