Skip to main content

factorio_ir/
module.rs

1use crate::{
2    block::Block,
3    locale::{LocaleFile, PendingLocaleFile},
4    scope::Scope,
5    stage::Stage,
6    statement::Statement,
7};
8
9#[derive(Debug, Clone, PartialEq)]
10pub struct Symbol {
11    pub scope: Scope,
12    pub statement: Statement,
13}
14
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct ModuleImport {
17    /// Dotted module path inside the Factorio mod (e.g. `shared.player`).
18    pub module: String,
19    pub local: String,
20    pub items: Vec<ImportedItem>,
21    /// When set, `require` targets this Factorio mod instead of the consuming mod.
22    pub factorio_mod: Option<String>,
23    /// Path prefix inside the Factorio mod (`Some("lua")`, `Some("")`, ...).
24    /// When [`Self::factorio_mod`] is `None`, codegen always uses `lua`.
25    pub module_root: Option<String>,
26}
27
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct ImportedItem {
30    pub name: String,
31    pub local: String,
32}
33
34#[derive(Debug, Clone, PartialEq)]
35pub struct Module {
36    pub name: String,
37    pub stage: Stage,
38    pub body: Block,
39    pub symbols: Vec<Symbol>,
40    pub imports: Vec<ModuleImport>,
41    pub submodules: Vec<String>,
42    /// Locale `.cfg` data declared via `locale!` in this module.
43    pub locales: Vec<LocaleFile>,
44    /// Parsed `locale!` blocks waiting for `Type::CONST` resolution (possibly
45    /// across imports). Cleared once [`locales`](Self::locales) is filled.
46    pub pending_locales: Vec<PendingLocaleFile>,
47}
48
49impl Module {
50    #[must_use]
51    pub fn imported_item_local(&self, type_name: &str) -> Option<&str> {
52        self.imports
53            .iter()
54            .flat_map(|import| import.items.iter())
55            .find_map(|item| {
56                if item.name == type_name {
57                    Some(item.local.as_str())
58                } else {
59                    None
60                }
61            })
62    }
63
64    #[must_use]
65    pub fn is_imported_type_extension(&self, struct_decl: &crate::structure::Struct) -> bool {
66        struct_decl.fields.is_empty() && self.imported_item_local(&struct_decl.name).is_some()
67    }
68}