1- use serde:: Serialize ;
1+ use serde:: { Deserialize , Serialize } ;
2+ use std:: collections:: HashMap ;
23use tucana:: shared:: {
34 DefinitionDataType , FlowType , FunctionDefinition , Module , ModuleConfigurationDefinition ,
45 RuntimeFlowType , RuntimeFunctionDefinition , Translation ,
@@ -10,6 +11,7 @@ use crate::reader::{Meta, MetaType, Reader};
1011pub struct DefinitionError {
1112 pub definition : String ,
1213 pub definition_type : crate :: reader:: MetaType ,
14+ pub path : String ,
1315 pub error : String ,
1416}
1517
@@ -18,14 +20,15 @@ pub struct Parser {
1820 pub modules : Vec < DefinitionModule > ,
1921}
2022
21- #[ derive( Serialize , Clone , Debug , Default ) ]
23+ #[ derive( Serialize , Deserialize , Clone , Debug , Default ) ]
2224pub struct ModuleConfiguration {
2325 pub identifier : String ,
2426 pub name : Vec < Translation > ,
2527 pub description : Vec < Translation > ,
2628 pub documentation : String ,
2729 pub author : String ,
2830 pub icon : String ,
31+ #[ serde( default , skip_serializing_if = "String::is_empty" ) ]
2932 pub version : String ,
3033}
3134
@@ -79,19 +82,21 @@ impl Parser {
7982
8083 pub fn from_reader ( reader : Reader ) -> Self {
8184 let mut modules: Vec < DefinitionModule > = vec ! [ ] ;
85+ let mut module_indices_by_name: HashMap < String , usize > = HashMap :: new ( ) ;
8286
8387 for meta in & reader. meta {
84- let module = modules
85- . iter_mut ( )
86- . find ( |m| m. config . identifier == meta. name ) ;
87-
88- if let Some ( existing) = module {
89- Parser :: append_meta ( existing, meta) ;
88+ let module_index = if let Some ( index) = module_indices_by_name. get ( & meta. name ) {
89+ * index
9090 } else {
9191 let mut new_mod = DefinitionModule :: default ( ) ;
9292 Parser :: append_meta ( & mut new_mod, meta) ;
9393 modules. push ( new_mod) ;
94- }
94+ let new_index = modules. len ( ) - 1 ;
95+ module_indices_by_name. insert ( meta. name . clone ( ) , new_index) ;
96+ continue ;
97+ } ;
98+
99+ Parser :: append_meta ( & mut modules[ module_index] , meta) ;
95100 }
96101
97102 Parser { modules }
@@ -132,6 +137,7 @@ impl Parser {
132137 Err ( err) => feature. errors . push ( DefinitionError {
133138 definition : Parser :: extract_identifier ( definition, MetaType :: DataType ) ,
134139 definition_type : MetaType :: DataType ,
140+ path : meta. path . clone ( ) ,
135141 error : err. to_string ( ) ,
136142 } ) ,
137143 } ,
@@ -140,6 +146,7 @@ impl Parser {
140146 Err ( err) => feature. errors . push ( DefinitionError {
141147 definition : Parser :: extract_identifier ( definition, MetaType :: FlowType ) ,
142148 definition_type : MetaType :: FlowType ,
149+ path : meta. path . clone ( ) ,
143150 error : err. to_string ( ) ,
144151 } ) ,
145152 } ,
@@ -152,6 +159,7 @@ impl Parser {
152159 MetaType :: RuntimeFunction ,
153160 ) ,
154161 definition_type : MetaType :: RuntimeFunction ,
162+ path : meta. path . clone ( ) ,
155163 error : err. to_string ( ) ,
156164 } ) ,
157165 }
@@ -165,6 +173,7 @@ impl Parser {
165173 MetaType :: RuntimeFlowType ,
166174 ) ,
167175 definition_type : MetaType :: RuntimeFlowType ,
176+ path : meta. path . clone ( ) ,
168177 error : err. to_string ( ) ,
169178 } ) ,
170179 }
@@ -174,18 +183,31 @@ impl Parser {
174183 Err ( err) => feature. errors . push ( DefinitionError {
175184 definition : Parser :: extract_identifier ( definition, MetaType :: Function ) ,
176185 definition_type : MetaType :: Function ,
186+ path : meta. path . clone ( ) ,
177187 error : err. to_string ( ) ,
178188 } ) ,
179189 } ,
180190 MetaType :: Configs => {
181191 match serde_json:: from_str :: < ModuleConfigurationDefinition > ( definition) {
182192 Ok ( v) => feature. module_configs . push ( v) ,
193+ Err ( err) => feature. errors . push ( DefinitionError {
194+ definition : Parser :: extract_identifier ( definition, MetaType :: Configs ) ,
195+ definition_type : MetaType :: Configs ,
196+ path : meta. path . clone ( ) ,
197+ error : err. to_string ( ) ,
198+ } ) ,
199+ }
200+ }
201+ MetaType :: ModuleDefinition => {
202+ match serde_json:: from_str :: < ModuleConfiguration > ( definition) {
203+ Ok ( v) => feature. config = v,
183204 Err ( err) => feature. errors . push ( DefinitionError {
184205 definition : Parser :: extract_identifier (
185206 definition,
186- MetaType :: RuntimeFlowType ,
207+ MetaType :: ModuleDefinition ,
187208 ) ,
188- definition_type : MetaType :: RuntimeFlowType ,
209+ definition_type : MetaType :: ModuleDefinition ,
210+ path : meta. path . clone ( ) ,
189211 error : err. to_string ( ) ,
190212 } ) ,
191213 }
0 commit comments