Skip to content

Commit 4a09fc0

Browse files
committed
Directly add classes to lib.rs
1 parent 2694076 commit 4a09fc0

4 files changed

Lines changed: 39 additions & 18 deletions

File tree

codegen/class_inject.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::{fs, io};
2+
3+
use crate::PythonBindType;
4+
5+
pub fn classes_to_lib_rs(type_data: &[PythonBindType]) -> io::Result<()> {
6+
let mut class_names = type_data
7+
.iter()
8+
.map(|generator| format!(" {}", generator.struct_name()))
9+
.collect::<Vec<_>>();
10+
class_names.sort();
11+
12+
let file_contents = format!(" classes: [\n{}\n ],", class_names.join(",\n"));
13+
14+
let mut lib_rs = fs::read_to_string("src/lib.rs")?;
15+
16+
let start = lib_rs.find(" classes: [\n").unwrap();
17+
let end = lib_rs[start..].find("],").unwrap() + 2;
18+
19+
lib_rs.replace_range(start..start + end, &file_contents);
20+
21+
fs::write("src/lib.rs", lib_rs)?;
22+
23+
Ok(())
24+
}

codegen/main.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod class_inject;
12
mod enums;
23
mod generator;
34
mod pyi;
@@ -161,20 +162,6 @@ fn mod_rs_generator(type_data: &[PythonBindType]) -> io::Result<()> {
161162
Ok(())
162163
}
163164

164-
fn class_names_txt_generator(type_data: &[PythonBindType]) -> io::Result<()> {
165-
let mut class_names = type_data
166-
.iter()
167-
.map(|generator| format!(" {}", generator.struct_name()))
168-
.collect::<Vec<_>>();
169-
class_names.sort();
170-
171-
let file_contents = format!("[\n{}\n]", class_names.join(",\n"));
172-
173-
fs::write("classes.txt", file_contents)?;
174-
175-
Ok(())
176-
}
177-
178165
fn run_flatc() -> io::Result<()> {
179166
println!("cargo:rerun-if-changed=flatbuffers-schema/comms.fbs");
180167
println!("cargo:rerun-if-changed=flatbuffers-schema/event.fbs");
@@ -240,7 +227,7 @@ fn main() -> io::Result<()> {
240227

241228
mod_rs_generator(&type_data)?;
242229
pyi::generator(&type_data)?;
243-
class_names_txt_generator(&type_data)?;
230+
class_inject::classes_to_lib_rs(&type_data)?;
244231

245232
Ok(())
246233
}

codegen/structs.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,10 @@ impl StructBindGenerator {
243243

244244
fn generate_new_method(&mut self) {
245245
write_str!(self, " #[new]");
246-
write_str!(self, " #[allow(clippy::too_many_arguments)]");
247246

248247
if self.types.is_empty() {
249248
write_str!(self, " pub fn new() -> Self {");
250-
write_str!(self, " Self::default()");
249+
write_str!(self, " Self {}");
251250
write_str!(self, " }");
252251
return;
253252
}
@@ -289,6 +288,11 @@ impl StructBindGenerator {
289288
signature_parts.push(sig_part);
290289
}
291290

291+
let max_num_types = if needs_python { 6 } else { 7 };
292+
if self.types.len() > max_num_types {
293+
write_str!(self, " #[allow(clippy::too_many_arguments)]");
294+
}
295+
292296
write_fmt!(self, " #[pyo3(signature = ({}))]", signature_parts.join(", "));
293297
write_str!(self, " pub fn new(");
294298

pytest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def random_string():
1515

1616
def random_player_config():
1717
return PlayerConfiguration(
18-
variety=PlayerClass(Psyonix(1.0)),
18+
variety=Psyonix(1.0),
1919
name=random_string(),
2020
location=random_string(),
2121
run_command=random_string(),
@@ -120,6 +120,12 @@ def random_script_config():
120120
)
121121
)
122122

123+
match renderPolyLine.variety.item:
124+
case PolyLine3D():
125+
assert len(renderPolyLine.variety.item.points) == 2048
126+
case _:
127+
assert False
128+
123129
data = renderPolyLine.pack()
124130
print(f"RenderMessage size: {len(data)} bytes")
125131

0 commit comments

Comments
 (0)