@@ -294,6 +294,8 @@ impl PythonBindGenerator {
294
294
self . write_str ( "}" ) ;
295
295
self . write_str ( "" ) ;
296
296
297
+ self . generate_union_py_methods ( ) ;
298
+
297
299
self . write_str ( "#[pyclass(module = \" rlbot_flatbuffers\" , get_all, set_all)]" ) ;
298
300
self . write_str ( "#[derive(Debug, Default, Clone, GetSize)]" ) ;
299
301
self . write_string ( format ! ( "pub struct {} {{" , self . struct_name) ) ;
@@ -319,6 +321,38 @@ impl PythonBindGenerator {
319
321
self . write_str ( "" ) ;
320
322
}
321
323
324
+ fn generate_union_py_methods ( & mut self ) {
325
+ self . write_str ( "#[pymethods]" ) ;
326
+ self . write_string ( format ! ( "impl {}Type {{" , self . struct_name) ) ;
327
+
328
+ self . write_str ( " #[new]" ) ;
329
+ assert ! ( u8 :: try_from( self . types. len( ) ) . is_ok( ) ) ;
330
+
331
+ self . write_str ( " #[pyo3(signature = (value=Default::default()))]" ) ;
332
+ self . write_str ( " pub fn new(value: u8) -> Self {" ) ;
333
+ self . write_str ( " match value {" ) ;
334
+
335
+ for ( i, variable_info) in self . types . iter ( ) . enumerate ( ) {
336
+ let variable_name = & variable_info[ 0 ] ;
337
+
338
+ self . file_contents
339
+ . push ( Cow :: Owned ( format ! ( " {i} => Self::{variable_name}," , ) ) ) ;
340
+ }
341
+
342
+ if self . types . len ( ) != usize:: from ( u8:: MAX ) {
343
+ self . write_str ( " v => panic!(\" Unknown value: {v}\" )," ) ;
344
+ }
345
+
346
+ self . write_str ( " }" ) ;
347
+ self . write_str ( " }" ) ;
348
+
349
+ self . write_str ( "" ) ;
350
+
351
+ self . generate_str_method ( ) ;
352
+
353
+ self . write_str ( "}" ) ;
354
+ }
355
+
322
356
fn generate_from_flat_impls ( & mut self ) {
323
357
match self . bind_type {
324
358
PythonBindType :: Enum => self . generate_enum_from_flat_impls ( ) ,
@@ -914,8 +948,11 @@ impl PythonBindGenerator {
914
948
self . generate_new_method ( ) ;
915
949
self . write_str ( "" ) ;
916
950
self . generate_str_method ( ) ;
917
- self . write_str ( "" ) ;
918
- self . generate_repr_method ( ) ;
951
+
952
+ if self . bind_type != PythonBindType :: Enum {
953
+ self . write_str ( "" ) ;
954
+ self . generate_repr_method ( ) ;
955
+ }
919
956
920
957
if self . bind_type != PythonBindType :: Union {
921
958
self . write_str ( "" ) ;
@@ -997,7 +1034,6 @@ fn pyi_generator(type_data: &[(String, String, Vec<Vec<String>>)]) -> io::Result
997
1034
let mut file_contents = vec ! [
998
1035
Cow :: Borrowed ( "from __future__ import annotations" ) ,
999
1036
Cow :: Borrowed ( "" ) ,
1000
- Cow :: Borrowed ( "from enum import Enum" ) ,
1001
1037
Cow :: Borrowed ( "from typing import Optional" ) ,
1002
1038
Cow :: Borrowed ( "" ) ,
1003
1039
Cow :: Borrowed ( "__doc__: str" ) ,
@@ -1023,14 +1059,24 @@ fn pyi_generator(type_data: &[(String, String, Vec<Vec<String>>)]) -> io::Result
1023
1059
let is_union = !types. is_empty ( ) && types[ 0 ] [ 1 ] . is_empty ( ) ;
1024
1060
1025
1061
if is_union {
1026
- file_contents. push ( Cow :: Owned ( format ! ( "class {type_name}Type(Enum) :" ) ) ) ;
1062
+ file_contents. push ( Cow :: Owned ( format ! ( "class {type_name}Type:" ) ) ) ;
1027
1063
1028
1064
for ( i, variable_info) in types. iter ( ) . enumerate ( ) {
1029
1065
let variable_name = variable_info[ 0 ] . as_str ( ) ;
1030
- file_contents. push ( Cow :: Owned ( format ! ( " {variable_name} = {i} " , ) ) ) ;
1066
+ file_contents. push ( Cow :: Owned ( format ! ( " {variable_name} = {type_name}Type({i}) " , ) ) ) ;
1031
1067
}
1032
1068
1033
1069
file_contents. push ( Cow :: Borrowed ( "" ) ) ;
1070
+
1071
+ file_contents. push ( Cow :: Borrowed ( " def __init__(self, value: int = 0): ..." ) ) ;
1072
+ file_contents. push ( Cow :: Borrowed ( " def __str__(self) -> str: ..." ) ) ;
1073
+ file_contents. push ( Cow :: Borrowed ( " def __repr__(self) -> str: ..." ) ) ;
1074
+ file_contents. push ( Cow :: Borrowed ( " def __int__(self) -> int: ..." ) ) ;
1075
+ file_contents. push ( Cow :: Owned ( format ! (
1076
+ " def __richcmp__(self, other: {type_name}, op: int) -> bool: ..."
1077
+ ) ) ) ;
1078
+
1079
+ file_contents. push ( Cow :: Borrowed ( "" ) ) ;
1034
1080
}
1035
1081
1036
1082
file_contents. push ( Cow :: Owned ( format ! ( "class {type_name}:" ) ) ) ;
@@ -1055,7 +1101,7 @@ fn pyi_generator(type_data: &[(String, String, Vec<Vec<String>>)]) -> io::Result
1055
1101
} ;
1056
1102
1057
1103
if is_enum {
1058
- file_contents. push ( Cow :: Owned ( format ! ( " {variable_name} = {variable_type}" , ) ) ) ;
1104
+ file_contents. push ( Cow :: Owned ( format ! ( " {variable_name} = {type_name}({ variable_type})" ) ) ) ;
1059
1105
continue ;
1060
1106
}
1061
1107
@@ -1149,6 +1195,13 @@ fn pyi_generator(type_data: &[(String, String, Vec<Vec<String>>)]) -> io::Result
1149
1195
file_contents. push ( Cow :: Borrowed ( " def __str__(self) -> str: ..." ) ) ;
1150
1196
file_contents. push ( Cow :: Borrowed ( " def __repr__(self) -> str: ..." ) ) ;
1151
1197
1198
+ if is_enum {
1199
+ file_contents. push ( Cow :: Borrowed ( " def __int__(self) -> int: ..." ) ) ;
1200
+ file_contents. push ( Cow :: Owned ( format ! (
1201
+ " def __richcmp__(self, other: {type_name}, op: int) -> bool: ..."
1202
+ ) ) ) ;
1203
+ }
1204
+
1152
1205
if !is_union {
1153
1206
file_contents. push ( Cow :: Borrowed ( " def pack(self) -> bytes: ..." ) ) ;
1154
1207
file_contents. push ( Cow :: Borrowed ( " @staticmethod" ) ) ;
@@ -1198,7 +1251,11 @@ fn main() -> io::Result<()> {
1198
1251
1199
1252
let out_folder = Path :: new ( OUT_FOLDER ) . join ( "rlbot" ) . join ( "flat" ) ;
1200
1253
1201
- assert ! ( out_folder. exists( ) , "Could not find generated folder: {}" , out_folder. display( ) ) ;
1254
+ assert ! (
1255
+ out_folder. exists( ) ,
1256
+ "Could not find generated folder: {}" ,
1257
+ out_folder. display( )
1258
+ ) ;
1202
1259
1203
1260
// ^ the above generates the Rust flatbuffers code,
1204
1261
// and the below we generates the wanted additional Python binds
0 commit comments