@@ -95,9 +95,11 @@ pub struct CodeGenerator {
9595 target_namespace : String ,
9696 native_types : HashSet < String > ,
9797 id_path : String ,
98+ namespace_to_import_path : HashMap < String , String > ,
9899}
99100
100101impl CodeGenerator {
102+ #[ allow( clippy:: too_many_arguments) ]
101103 pub fn new (
102104 external_import_map : HashMap < String , ExternalType > ,
103105 native_types : HashSet < String > ,
@@ -106,6 +108,7 @@ impl CodeGenerator {
106108 config : CodeGenItemConfig ,
107109 target_namespace : String ,
108110 id_path : String ,
111+ namespace_to_import_path : HashMap < String , String > ,
109112 ) -> Self {
110113 Self {
111114 import_map : external_import_map
@@ -119,7 +122,10 @@ impl CodeGenerator {
119122 Some ( "ExtensionObject" | "OptionSet" ) => {
120123 Some ( FieldType :: ExtensionObject ( None ) )
121124 }
122- Some ( t) => Some ( FieldType :: Normal ( t. to_owned ( ) ) ) ,
125+ Some ( t) => Some ( FieldType :: Normal {
126+ name : t. to_owned ( ) ,
127+ namespace : None ,
128+ } ) ,
123129 None => None ,
124130 } ,
125131 path : v. path ,
@@ -137,6 +143,7 @@ impl CodeGenerator {
137143 target_namespace,
138144 native_types,
139145 id_path,
146+ namespace_to_import_path,
140147 }
141148 }
142149
@@ -159,7 +166,7 @@ impl CodeGenerator {
159166 }
160167
161168 let Some ( it) = self . import_map . get ( name) else {
162- // Not in the import map means it's a builtin, we assume these have defaults for now.
169+ // Not in the import map means it's a builtin or external reference , we assume these have defaults for now.
163170 return true ;
164171 } ;
165172
@@ -175,8 +182,8 @@ impl CodeGenerator {
175182 LoadedType :: Struct ( s) => {
176183 for k in & s. fields {
177184 let has_default = match & k. typ {
178- StructureFieldType :: Field ( FieldType :: Normal ( f ) ) => {
179- self . is_default_recursive ( f )
185+ StructureFieldType :: Field ( FieldType :: Normal { name , .. } ) => {
186+ self . is_default_recursive ( name )
180187 }
181188 StructureFieldType :: Array ( _) | StructureFieldType :: Field ( _) => true ,
182189 } ;
@@ -279,7 +286,7 @@ impl CodeGenerator {
279286 }
280287
281288 /// Get the fully qualified path of a type, by looking it up in the import map.
282- fn get_type_path ( & self , name : & str ) -> String {
289+ fn get_type_path ( & self , name : & str , namespace : Option < & str > ) -> String {
283290 // Type is known, use the external path.
284291 if let Some ( ext) = self . import_map . get ( name) {
285292 return format ! ( "{}::{}" , ext. path, name) ;
@@ -288,6 +295,12 @@ impl CodeGenerator {
288295 if self . native_types . contains ( name) {
289296 return name. to_owned ( ) ;
290297 }
298+
299+ if let Some ( namespace) = namespace {
300+ if let Some ( import_path) = self . namespace_to_import_path . get ( namespace) {
301+ return format ! ( "{}::{}" , import_path, name) ;
302+ }
303+ }
291304 // Assume the type is a builtin.
292305 format ! ( "opcua::types::{name}" )
293306 }
@@ -548,7 +561,7 @@ impl CodeGenerator {
548561 fn is_extension_object ( & self , typ : Option < & FieldType > ) -> bool {
549562 let name = match & typ {
550563 Some ( FieldType :: Abstract ( _) ) | Some ( FieldType :: ExtensionObject ( _) ) => return true ,
551- Some ( FieldType :: Normal ( s ) ) => s ,
564+ Some ( FieldType :: Normal { name , .. } ) => name ,
552565 None => return false ,
553566 } ;
554567 let name = match name. split_once ( ":" ) {
@@ -596,18 +609,22 @@ impl CodeGenerator {
596609
597610 for field in item. visible_fields ( ) {
598611 let typ: Type = match & field. typ {
599- StructureFieldType :: Field ( f) => {
600- syn:: parse_str ( & self . get_type_path ( f. as_type_str ( ) ) ) . map_err ( |e| {
601- CodeGenError :: from ( e)
602- . with_context ( format ! ( "Generating path for {}" , f. as_type_str( ) ) )
603- } ) ?
604- }
612+ StructureFieldType :: Field ( f) => syn:: parse_str (
613+ & self . get_type_path ( f. as_type_str ( ) , f. namespace ( ) ) ,
614+ )
615+ . map_err ( |e| {
616+ CodeGenError :: from ( e)
617+ . with_context ( format ! ( "Generating path for {}" , f. as_type_str( ) ) )
618+ } ) ?,
605619 StructureFieldType :: Array ( f) => {
606620 let path: Path =
607- syn:: parse_str ( & self . get_type_path ( f. as_type_str ( ) ) ) . map_err ( |e| {
608- CodeGenError :: from ( e)
609- . with_context ( format ! ( "Generating path for {}" , f. as_type_str( ) ) )
610- } ) ?;
621+ syn:: parse_str ( & self . get_type_path ( f. as_type_str ( ) , f. namespace ( ) ) )
622+ . map_err ( |e| {
623+ CodeGenError :: from ( e) . with_context ( format ! (
624+ "Generating path for {}" ,
625+ f. as_type_str( )
626+ ) )
627+ } ) ?;
611628 parse_quote ! { Option <Vec <#path>> }
612629 }
613630 } ;
0 commit comments