11use crate :: {
2+ generator:: Generator ,
23 structs:: { InnerOptionType , InnerVecType , RustType } ,
34 PythonBindType ,
45} ;
@@ -76,10 +77,16 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
7677 write_str ! ( file, "" ) ;
7778 write_str ! ( file, " def __init__(self, value: int = 0):" ) ;
7879 write_str ! ( file, " \" \" \" " ) ;
79- write_str ! ( file, " :raises ValueError: If the `value` is not a valid enum value" ) ;
80+ write_str ! (
81+ file,
82+ " :raises ValueError: If the `value` is not a valid enum value"
83+ ) ;
8084 write_str ! ( file, " \" \" \" \n " ) ;
8185 write_str ! ( file, " def __int__(self) -> int: ..." ) ;
82- write_fmt ! ( file, " def __richcmp__(self, other: {type_name}, op: int) -> bool: ..." ) ;
86+ write_fmt ! (
87+ file,
88+ " def __richcmp__(self, other: {type_name}, op: int) -> bool: ..."
89+ ) ;
8390 }
8491 PythonBindType :: Struct ( gen) => {
8592 let mut python_types = Vec :: new ( ) ;
@@ -134,7 +141,7 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
134141 } else {
135142 type_name. as_str ( )
136143 } ;
137-
144+
138145 python_types. push ( format ! ( "Optional[{python_type}]" ) ) ;
139146 }
140147 RustType :: Box ( inner_type) => {
@@ -145,6 +152,22 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
145152 python_types. push ( "str" . to_string ( ) ) ;
146153 write_fmt ! ( file, " {variable_name}: str" ) ;
147154 }
155+ RustType :: Union ( type_name) => {
156+ write_fmt ! ( file, " {variable_name}: {type_name}" ) ;
157+
158+ // search for the union with the name `type_name` and get the types
159+ let union_types = type_data
160+ . iter ( )
161+ . find_map ( |item| match item {
162+ PythonBindType :: Union ( gen) if gen. struct_name ( ) == type_name => {
163+ Some ( gen. types . iter ( ) . skip ( 1 ) . map ( |v| v. name . as_str ( ) ) . collect :: < Vec < _ > > ( ) )
164+ }
165+ _ => None ,
166+ } )
167+ . unwrap ( ) ;
168+ let types = union_types. join ( " | " ) ;
169+ python_types. push ( format ! ( "Optional[{types}]" ) ) ;
170+ }
148171 RustType :: Custom ( type_name) | RustType :: Other ( type_name) | RustType :: Base ( type_name) => {
149172 python_types. push ( type_name. to_string ( ) ) ;
150173 write_fmt ! ( file, " {variable_name}: {type_name}" ) ;
@@ -168,14 +191,14 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
168191 "String" => Cow :: Borrowed ( "\" \" " ) ,
169192 "Vec<u8>" => Cow :: Borrowed ( "b\" \" " ) ,
170193 t => {
171- if t. starts_with ( "Vec<" ) {
194+ if python_type. starts_with ( "Optional" ) || t. starts_with ( "Option<" ) {
195+ Cow :: Borrowed ( "None" )
196+ } else if t. starts_with ( "Vec<" ) {
172197 Cow :: Borrowed ( "[]" )
173198 } else if t. starts_with ( "Box<" ) {
174199 let inner_type =
175200 t. trim_start_matches ( "Box<" ) . trim_end_matches ( '>' ) . trim_end_matches ( 'T' ) ;
176201 Cow :: Owned ( format ! ( "{inner_type}()" ) )
177- } else if t. starts_with ( "Option<" ) {
178- Cow :: Borrowed ( "None" )
179202 } else {
180203 Cow :: Owned ( format ! ( "{}()" , t. trim_end_matches( 'T' ) ) )
181204 }
0 commit comments