1#![allow(
2 clippy::missing_errors_doc,
3 clippy::missing_panics_doc,
4 clippy::module_name_repetitions,
5 clippy::must_use_candidate,
6 clippy::too_many_lines,
7 clippy::cognitive_complexity,
8 clippy::doc_markdown,
9 clippy::unnested_or_patterns,
10 clippy::option_if_let_else,
11 clippy::map_unwrap_or,
12 clippy::needless_pass_by_value,
13 clippy::use_self,
14 clippy::similar_names,
15 clippy::single_match_else,
16 clippy::match_same_arms,
17 clippy::items_after_statements,
18 clippy::struct_field_names,
19 clippy::if_not_else,
20 clippy::from_iter_instead_of_collect,
21 clippy::manual_string_new,
22 clippy::wildcard_imports,
23 clippy::enum_glob_use,
24 clippy::unused_self,
25 clippy::trivially_copy_pass_by_ref,
26 clippy::as_conversions,
27 clippy::indexing_slicing,
28 clippy::integer_division,
29 clippy::arithmetic_side_effects,
30 clippy::missing_const_for_fn,
31 clippy::implicit_hasher,
32 clippy::semicolon_if_nothing_returned,
33 clippy::redundant_closure_for_method_calls,
34 clippy::unnecessary_wraps,
35 clippy::return_self_not_must_use,
36 clippy::pub_use,
37 clippy::module_inception,
38 clippy::redundant_pub_crate
39)]
40
41mod generate;
42mod schema;
43
44use std::path::Path;
45
46pub use schema::RuntimeApi;
47
48pub struct GeneratedApi {
49 pub application_version: String,
50 pub api_version: u32,
51 pub events: String,
52 pub event_map: String,
53 pub event_lookup: String,
54 pub event_filter_lookup: String,
55 pub event_module_lookup: String,
56 pub event_filters: String,
57 pub event_data: String,
58 pub defines: String,
59 pub classes: String,
60 pub globals: String,
61 pub concepts: String,
62 pub unions: String,
63}
64
65pub fn parse_runtime_api(json: &str) -> Result<RuntimeApi, serde_json::Error> {
66 serde_json::from_str(json)
67}
68
69pub fn generate_runtime_api(api: &RuntimeApi) -> GeneratedApi {
70 let mappings = generate::collect_event_mappings(api);
71 let class_names = generate::class_names(api);
72 let filter_concept_names = generate::event_filter_concept_names(api);
73 let identification_names = generate::identification_concept_names(api, &filter_concept_names);
74 let identification_signatures = generate::identification_signatures(api, &identification_names);
75 let mut concept_names = generate::generatable_concept_names(api, &filter_concept_names);
76 concept_names.extend(identification_names.iter().cloned());
77 let union_registry = generate::collect_literal_unions(api);
78 let known = generate::KnownTypes {
79 classes: &class_names,
80 concepts: &concept_names,
81 identifications: &identification_names,
82 identification_signatures: &identification_signatures,
83 unions: union_registry.names(),
84 union_registry: &union_registry,
85 };
86 let event_filters = generate::generate_event_filters(api);
87 let concepts = {
88 let mut structs = generate::generate_concepts(api, &known, &filter_concept_names);
89 let ids = generate::generate_identifications(api, &known).to_string();
90 structs.push_str(&ids);
91 structs
92 };
93
94 GeneratedApi {
95 application_version: api.application_version.clone(),
96 api_version: api.api_version,
97 events: generate::generate_events(api),
98 event_map: generate::generate_event_map(&mappings),
99 event_lookup: generate::generate_event_lookup(&mappings),
100 event_filter_lookup: generate::generate_event_filter_lookup(&mappings),
101 event_module_lookup: generate::generate_event_module_lookup(&mappings),
102 event_filters,
103 event_data: generate::generate_event_data(api, &known),
104 defines: generate::generate_defines(&api.defines),
105 classes: generate::generate_classes(api, &known),
106 globals: generate::generate_globals(api, &known),
107 concepts,
108 unions: generate::generate_unions(&union_registry),
109 }
110}
111
112pub fn generate_from_json(json: &str) -> Result<GeneratedApi, serde_json::Error> {
113 let api = parse_runtime_api(json)?;
114 Ok(generate_runtime_api(&api))
115}
116
117pub fn bundled_runtime_api_json() -> &'static str {
118 include_str!("../api/runtime-api.json")
119}
120
121pub fn generate_from_bundled_api() -> Result<GeneratedApi, serde_json::Error> {
122 generate_from_json(bundled_runtime_api_json())
123}
124
125pub fn write_generated_api(output_dir: &Path, generated: &GeneratedApi) -> std::io::Result<()> {
126 std::fs::create_dir_all(output_dir)?;
127
128 write_module(
129 output_dir,
130 "mod.rs",
131 &format!(
132 "// Generated from Factorio runtime API v{} (format v{}).\n\
133 #[allow(unused, clippy::all, clippy::pedantic, clippy::nursery)]\n\n\
134 pub mod classes;\n\
135 pub mod concepts;\n\
136 pub mod defines;\n\
137 pub mod event_data;\n\
138 pub mod event_filters;\n\
139 pub mod events;\n\
140 pub mod globals;\n\
141 pub mod map;\n\
142 pub mod unions;\n",
143 generated.application_version, generated.api_version
144 ),
145 )?;
146 write_module(output_dir, "events.rs", &generated.events)?;
147 write_module(output_dir, "map.rs", &generated.event_map)?;
148 write_module(output_dir, "event_filters.rs", &generated.event_filters)?;
149 write_module(output_dir, "event_data.rs", &generated.event_data)?;
150 write_module(output_dir, "defines.rs", &generated.defines)?;
151 write_module(output_dir, "classes.rs", &generated.classes)?;
152 write_module(output_dir, "globals.rs", &generated.globals)?;
153 write_module(output_dir, "concepts.rs", &generated.concepts)?;
154 write_module(output_dir, "unions.rs", &generated.unions)?;
155
156 Ok(())
157}
158
159pub fn write_macro_event_lookup(
160 output_dir: &Path,
161 generated: &GeneratedApi,
162) -> std::io::Result<()> {
163 std::fs::create_dir_all(output_dir)?;
164 write_module(output_dir, "event_lookup.rs", &generated.event_lookup)?;
165 write_module(
166 output_dir,
167 "event_filter_lookup.rs",
168 &generated.event_filter_lookup,
169 )?;
170 write_module(
171 output_dir,
172 "event_module_lookup.rs",
173 &generated.event_module_lookup,
174 )
175}
176
177fn write_module(output_dir: &Path, file_name: &str, contents: &str) -> std::io::Result<()> {
178 let path = output_dir.join(file_name);
179 std::fs::write(path, contents)
180}