@@ -48,6 +48,7 @@ use crate::conversion::*;
4848use crate :: doc:: * ;
4949use crate :: formatting:: * ;
5050use heck:: CamelCase ;
51+ use oo_bindgen:: constants:: * ;
5152use oo_bindgen:: formatting:: * ;
5253use oo_bindgen:: native_enum:: * ;
5354use oo_bindgen:: native_function:: * ;
@@ -81,6 +82,7 @@ pub fn generate_dotnet_bindings(
8182
8283 generate_native_func_class ( lib, config) ?;
8384
85+ generate_constants ( lib, config) ?;
8486 generate_structs ( lib, config) ?;
8587 generate_enums ( lib, config) ?;
8688 generate_classes ( lib, config) ?;
@@ -170,6 +172,20 @@ fn generate_native_func_class(lib: &Library, config: &DotnetBindgenConfig) -> Fo
170172 } )
171173}
172174
175+ fn generate_constants ( lib : & Library , config : & DotnetBindgenConfig ) -> FormattingResult < ( ) > {
176+ for constants in lib. constants ( ) {
177+ // Open file
178+ let mut filename = config. output_dir . clone ( ) ;
179+ filename. push ( & constants. name ) ;
180+ filename. set_extension ( "cs" ) ;
181+ let mut f = FilePrinter :: new ( filename) ?;
182+
183+ generate_constant_set ( & mut f, constants, lib) ?;
184+ }
185+
186+ Ok ( ( ) )
187+ }
188+
173189fn generate_structs ( lib : & Library , config : & DotnetBindgenConfig ) -> FormattingResult < ( ) > {
174190 for native_struct in lib. structs ( ) {
175191 // Open file
@@ -198,6 +214,49 @@ fn generate_enums(lib: &Library, config: &DotnetBindgenConfig) -> FormattingResu
198214 Ok ( ( ) )
199215}
200216
217+ fn generate_constant_set (
218+ f : & mut impl Printer ,
219+ set : & ConstantSetHandle ,
220+ lib : & Library ,
221+ ) -> FormattingResult < ( ) > {
222+ fn get_type_as_string ( value : & ConstantValue ) -> & ' static str {
223+ match value {
224+ ConstantValue :: U8 ( _, _) => "byte" ,
225+ }
226+ }
227+
228+ fn get_value_as_string ( value : & ConstantValue ) -> String {
229+ match value {
230+ ConstantValue :: U8 ( x, Representation :: Hex ) => format ! ( "0x{:02X?}" , x) ,
231+ }
232+ }
233+
234+ print_license ( f, & lib. license ) ?;
235+ print_imports ( f) ?;
236+ f. newline ( ) ?;
237+
238+ namespaced ( f, & lib. name , |f| {
239+ documentation ( f, |f| {
240+ // Print top-level documentation
241+ xmldoc_print ( f, & set. doc , lib)
242+ } ) ?;
243+
244+ f. writeln ( & format ! ( "public static class {}" , set. name. to_camel_case( ) ) ) ?;
245+ blocked ( f, |f| {
246+ for value in & set. values {
247+ documentation ( f, |f| xmldoc_print ( f, & value. doc , lib) ) ?;
248+ f. writeln ( & format ! (
249+ "public const {} {} = {};" ,
250+ get_type_as_string( & value. value) ,
251+ value. name. to_camel_case( ) ,
252+ get_value_as_string( & value. value) ,
253+ ) ) ?;
254+ }
255+ Ok ( ( ) )
256+ } )
257+ } )
258+ }
259+
201260fn generate_enum (
202261 f : & mut impl Printer ,
203262 native_enum : & NativeEnumHandle ,
0 commit comments