Skip to main content

factorio_api/
lua_libs.rs

1//! Standard Lua libraries available as Factorio globals (`math`, `string`, `table`).
2//!
3//! Stubs exist for `cargo check` / IDE support only. Calls lower to the real Lua
4//! library methods. Overloads that need distinct Rust names (`random_int`,
5//! `format_2`, ...) are remapped to the Lua name by the frontend.
6
7/// Lua `math` library (Factorio uses deterministic implementations).
8#[derive(Debug, Clone, Copy, PartialEq)]
9pub struct LuaMath {
10    pub pi: f64,
11    pub huge: f64,
12}
13
14impl Default for LuaMath {
15    fn default() -> Self {
16        Self::new()
17    }
18}
19
20impl LuaMath {
21    #[must_use]
22    pub const fn new() -> Self {
23        Self {
24            pi: 3.141_592_653_589_793,
25            huge: f64::INFINITY,
26        }
27    }
28
29    #[allow(unused_variables)]
30    pub fn abs(&self, x: f64) -> f64 {
31        0.0
32    }
33    #[allow(unused_variables)]
34    pub fn acos(&self, x: f64) -> f64 {
35        0.0
36    }
37    #[allow(unused_variables)]
38    pub fn asin(&self, x: f64) -> f64 {
39        0.0
40    }
41    #[allow(unused_variables)]
42    pub fn atan(&self, x: f64) -> f64 {
43        0.0
44    }
45    #[allow(unused_variables)]
46    pub fn atan2(&self, y: f64, x: f64) -> f64 {
47        0.0
48    }
49    #[allow(unused_variables)]
50    pub fn ceil(&self, x: f64) -> f64 {
51        0.0
52    }
53    #[allow(unused_variables)]
54    pub fn cos(&self, x: f64) -> f64 {
55        0.0
56    }
57    #[allow(unused_variables)]
58    pub fn cosh(&self, x: f64) -> f64 {
59        0.0
60    }
61    #[allow(unused_variables)]
62    pub fn deg(&self, x: f64) -> f64 {
63        0.0
64    }
65    #[allow(unused_variables)]
66    pub fn exp(&self, x: f64) -> f64 {
67        0.0
68    }
69    #[allow(unused_variables)]
70    pub fn floor(&self, x: f64) -> f64 {
71        0.0
72    }
73    #[allow(unused_variables)]
74    pub fn fmod(&self, x: f64, y: f64) -> f64 {
75        0.0
76    }
77    #[allow(unused_variables)]
78    pub fn frexp(&self, x: f64) -> (f64, i32) {
79        (0.0, 0)
80    }
81    #[allow(unused_variables)]
82    pub fn ldexp(&self, x: f64, exp: i32) -> f64 {
83        0.0
84    }
85    #[allow(unused_variables)]
86    pub fn log(&self, x: f64) -> f64 {
87        0.0
88    }
89    #[allow(unused_variables)]
90    pub fn log10(&self, x: f64) -> f64 {
91        0.0
92    }
93    #[allow(unused_variables)]
94    pub fn max(&self, a: f64, b: f64) -> f64 {
95        0.0
96    }
97    #[allow(unused_variables)]
98    pub fn min(&self, a: f64, b: f64) -> f64 {
99        0.0
100    }
101    #[allow(unused_variables)]
102    pub fn modf(&self, x: f64) -> (f64, f64) {
103        (0.0, 0.0)
104    }
105    #[allow(unused_variables)]
106    pub fn pow(&self, x: f64, y: f64) -> f64 {
107        0.0
108    }
109    #[allow(unused_variables)]
110    pub fn rad(&self, x: f64) -> f64 {
111        0.0
112    }
113    /// `math.random()` - float in `[0, 1)`.
114    pub fn random(&self) -> f64 {
115        0.0
116    }
117    /// `math.random(n)` - integer in `[1, n]`. Lowers as `math.random`.
118    #[allow(unused_variables)]
119    pub fn random_int(&self, n: i64) -> i64 {
120        0
121    }
122    /// `math.random(m, n)` - integer in `[m, n]`. Lowers as `math.random`.
123    #[allow(unused_variables)]
124    pub fn random_range(&self, m: i64, n: i64) -> i64 {
125        0
126    }
127    /// No-op in Factorio; use `LuaRandomGenerator` for custom seeding.
128    #[allow(unused_variables)]
129    pub fn randomseed(&self, seed: i64) {}
130    #[allow(unused_variables)]
131    pub fn sin(&self, x: f64) -> f64 {
132        0.0
133    }
134    #[allow(unused_variables)]
135    pub fn sinh(&self, x: f64) -> f64 {
136        0.0
137    }
138    #[allow(unused_variables)]
139    pub fn sqrt(&self, x: f64) -> f64 {
140        0.0
141    }
142    #[allow(unused_variables)]
143    pub fn tan(&self, x: f64) -> f64 {
144        0.0
145    }
146    #[allow(unused_variables)]
147    pub fn tanh(&self, x: f64) -> f64 {
148        0.0
149    }
150}
151
152/// Lua `string` library (includes Factorio's `pack` / `packsize` / `unpack`).
153#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
154pub struct LuaStringLib;
155
156impl LuaStringLib {
157    #[must_use]
158    pub const fn new() -> Self {
159        Self
160    }
161
162    #[allow(unused_variables)]
163    pub fn byte(&self, s: &'static str, i: Option<i64>, j: Option<i64>) -> Option<u8> {
164        None
165    }
166    #[allow(unused_variables)]
167    pub fn char(&self, bytes: impl Into<crate::LuaAny>) -> &'static str {
168        ""
169    }
170    #[allow(unused_variables)]
171    pub fn find(
172        &self,
173        s: &'static str,
174        pattern: &'static str,
175        init: Option<i64>,
176        plain: Option<bool>,
177    ) -> Option<(i64, i64)> {
178        None
179    }
180    /// `string.format(fmt)` with no values.
181    #[allow(unused_variables)]
182    pub fn format(&self, fmt: &'static str) -> &'static str {
183        ""
184    }
185    /// `string.format(fmt, a)`. Lowers as `string.format`.
186    #[allow(unused_variables)]
187    pub fn format_1(&self, fmt: &'static str, a: impl Into<crate::LuaAny>) -> &'static str {
188        ""
189    }
190    /// `string.format(fmt, a, b)`. Lowers as `string.format`.
191    #[allow(unused_variables)]
192    pub fn format_2(
193        &self,
194        fmt: &'static str,
195        a: impl Into<crate::LuaAny>,
196        b: impl Into<crate::LuaAny>,
197    ) -> &'static str {
198        ""
199    }
200    /// `string.format(fmt, a, b, c)`. Lowers as `string.format`.
201    #[allow(unused_variables)]
202    pub fn format_3(
203        &self,
204        fmt: &'static str,
205        a: impl Into<crate::LuaAny>,
206        b: impl Into<crate::LuaAny>,
207        c: impl Into<crate::LuaAny>,
208    ) -> &'static str {
209        ""
210    }
211    /// `string.format(fmt, a, b, c, d)`. Lowers as `string.format`.
212    #[allow(unused_variables)]
213    pub fn format_4(
214        &self,
215        fmt: &'static str,
216        a: impl Into<crate::LuaAny>,
217        b: impl Into<crate::LuaAny>,
218        c: impl Into<crate::LuaAny>,
219        d: impl Into<crate::LuaAny>,
220    ) -> &'static str {
221        ""
222    }
223    #[allow(unused_variables)]
224    pub fn gmatch(&self, s: &'static str, pattern: &'static str) -> crate::LuaAny {
225        crate::LuaAny
226    }
227    #[allow(unused_variables)]
228    pub fn gsub(
229        &self,
230        s: &'static str,
231        pattern: &'static str,
232        repl: impl Into<crate::LuaAny>,
233        n: Option<i64>,
234    ) -> (&'static str, i64) {
235        ("", 0)
236    }
237    #[allow(unused_variables)]
238    pub fn len(&self, s: &'static str) -> i64 {
239        0
240    }
241    #[allow(unused_variables)]
242    pub fn lower(&self, s: &'static str) -> &'static str {
243        ""
244    }
245    #[allow(unused_variables)]
246    pub fn r#match(
247        &self,
248        s: &'static str,
249        pattern: &'static str,
250        init: Option<i64>,
251    ) -> Option<&'static str> {
252        None
253    }
254    /// Binary pack (Lua 5.4 backport in Factorio).
255    #[allow(unused_variables)]
256    pub fn pack(&self, fmt: &'static str, values: impl Into<crate::LuaAny>) -> &'static str {
257        ""
258    }
259    #[allow(unused_variables)]
260    pub fn packsize(&self, fmt: &'static str) -> i64 {
261        0
262    }
263    #[allow(unused_variables)]
264    pub fn rep(&self, s: &'static str, n: i64, sep: Option<&'static str>) -> &'static str {
265        ""
266    }
267    #[allow(unused_variables)]
268    pub fn reverse(&self, s: &'static str) -> &'static str {
269        ""
270    }
271    #[allow(unused_variables)]
272    pub fn sub(&self, s: &'static str, i: i64, j: Option<i64>) -> &'static str {
273        ""
274    }
275    /// Binary unpack (Lua 5.4 backport in Factorio).
276    #[allow(unused_variables)]
277    pub fn unpack(&self, fmt: &'static str, s: &'static str, pos: Option<i64>) -> crate::LuaAny {
278        crate::LuaAny
279    }
280    #[allow(unused_variables)]
281    pub fn upper(&self, s: &'static str) -> &'static str {
282        ""
283    }
284}
285
286/// Lua `table` library.
287#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
288pub struct LuaTableLib;
289
290impl LuaTableLib {
291    #[must_use]
292    pub const fn new() -> Self {
293        Self
294    }
295
296    #[allow(unused_variables)]
297    pub fn concat(
298        &self,
299        list: impl Into<crate::LuaAny>,
300        sep: Option<&'static str>,
301        i: Option<i64>,
302        j: Option<i64>,
303    ) -> &'static str {
304        ""
305    }
306    #[allow(unused_variables)]
307    pub fn insert(&self, list: impl Into<crate::LuaAny>, value: impl Into<crate::LuaAny>) {}
308    /// `table.insert(list, pos, value)`. Lowers as `table.insert`.
309    #[allow(unused_variables)]
310    pub fn insert_at(
311        &self,
312        list: impl Into<crate::LuaAny>,
313        pos: i64,
314        value: impl Into<crate::LuaAny>,
315    ) {
316    }
317    #[allow(unused_variables)]
318    pub fn pack(&self, values: impl Into<crate::LuaAny>) -> crate::LuaAny {
319        crate::LuaAny
320    }
321    #[allow(unused_variables)]
322    pub fn remove(&self, list: impl Into<crate::LuaAny>, pos: Option<i64>) -> crate::LuaAny {
323        crate::LuaAny
324    }
325    #[allow(unused_variables)]
326    pub fn sort(&self, list: impl Into<crate::LuaAny>, comp: Option<crate::LuaFunction>) {}
327    #[allow(unused_variables)]
328    pub fn unpack(
329        &self,
330        list: impl Into<crate::LuaAny>,
331        i: Option<i64>,
332        j: Option<i64>,
333    ) -> crate::LuaAny {
334        crate::LuaAny
335    }
336}