@@ -82,7 +82,7 @@ pub fn execute_cwlfile(cwlfile: impl AsRef<Path>, raw_inputs: &[String], outdir:
8282 }
8383 }
8484
85- let output_values = execute ( cwlfile, input_values, outdir, None ) ?;
85+ let output_values = execute ( cwlfile, & input_values, outdir, None ) ?;
8686 let json = serde_json:: to_string_pretty ( & output_values) ?;
8787 println ! ( "{json}" ) ;
8888
@@ -91,7 +91,7 @@ pub fn execute_cwlfile(cwlfile: impl AsRef<Path>, raw_inputs: &[String], outdir:
9191
9292pub fn execute (
9393 cwlfile : impl AsRef < Path > ,
94- input_values : InputObject ,
94+ input_values : & InputObject ,
9595 outdir : Option < impl AsRef < Path > > ,
9696 cwl_doc : Option < & CWLDocument > ,
9797) -> Result < HashMap < String , DefaultValue > , Box < dyn Error > > {
@@ -130,6 +130,11 @@ pub struct InputObject {
130130 #[ serde( rename = "cwl:hints" ) ]
131131 #[ serde( default ) ]
132132 pub hints : Vec < Requirement > ,
133+
134+ #[ serde( skip) ]
135+ cwl_requirements : Vec < Requirement > ,
136+ #[ serde( skip) ]
137+ cwl_hints : Vec < Requirement > ,
133138}
134139
135140impl InputObject {
@@ -141,21 +146,56 @@ impl InputObject {
141146 }
142147
143148 pub fn add_requirement ( & mut self , requirement : & Requirement ) {
144- if self
145- . requirements
146- . iter ( )
147- . any ( |r| std:: mem:: discriminant ( r) == std:: mem:: discriminant ( requirement) )
149+ if let Some ( r ) = self
150+ . cwl_requirements
151+ . iter_mut ( )
152+ . find ( |r| std:: mem:: discriminant ( * r) == std:: mem:: discriminant ( requirement) )
148153 {
149- return ;
154+ * r = requirement. clone ( ) ;
155+ } else {
156+ self . cwl_requirements . push ( requirement. clone ( ) ) ;
150157 }
151- self . requirements . push ( requirement. clone ( ) ) ;
152158 }
153159
154160 pub fn add_hint ( & mut self , hint : & Requirement ) {
155- if self . hints . iter ( ) . any ( |r| std:: mem:: discriminant ( r) == std:: mem:: discriminant ( hint) ) {
156- return ;
161+ if let Some ( r) = self
162+ . cwl_hints
163+ . iter_mut ( )
164+ . find ( |r| std:: mem:: discriminant ( * r) == std:: mem:: discriminant ( hint) )
165+ {
166+ * r = hint. clone ( ) ;
167+ } else {
168+ self . cwl_hints . push ( hint. clone ( ) ) ;
169+ }
170+ }
171+
172+ pub fn handle_requirements ( & self , requirements : & [ Requirement ] , hints : & [ Requirement ] ) -> Self {
173+ let mut new_obj = self . clone ( ) ;
174+ for hint in hints {
175+ new_obj. add_hint ( hint) ;
176+ }
177+
178+ for req in requirements {
179+ new_obj. add_requirement ( req) ;
157180 }
158- self . hints . push ( hint. clone ( ) ) ;
181+ new_obj
182+ }
183+
184+ pub fn lock ( & mut self ) {
185+ fn merge ( dst : & mut Vec < Requirement > , src : & [ Requirement ] ) {
186+ for req in src {
187+ if let Some ( r) = dst. iter_mut ( ) . find ( |r| std:: mem:: discriminant ( * r) == std:: mem:: discriminant ( req) ) {
188+ * r = req. clone ( ) ;
189+ } else {
190+ dst. push ( req. clone ( ) ) ;
191+ }
192+ }
193+ }
194+ merge ( & mut self . cwl_requirements , & self . requirements ) ;
195+ self . requirements = self . cwl_requirements . clone ( ) ;
196+
197+ merge ( & mut self . cwl_hints , & self . hints ) ;
198+ self . hints = self . cwl_hints . clone ( ) ;
159199 }
160200}
161201
@@ -264,3 +304,26 @@ pub fn set_container_engine(value: ContainerEngine) {
264304pub fn container_engine ( ) -> ContainerEngine {
265305 CONTAINER_ENGINE . with ( |engine| * engine. borrow ( ) )
266306}
307+
308+ #[ cfg( test) ]
309+ mod tests {
310+ use super :: * ;
311+ use cwl:: { requirements:: EnvVarRequirement , types:: EnviromentDefs } ;
312+
313+ #[ test]
314+ fn test_add_requirement ( ) {
315+ let mut input = InputObject :: default ( ) ;
316+ let base_req = Requirement :: EnvVarRequirement ( EnvVarRequirement {
317+ env_def : EnviromentDefs :: Map ( HashMap :: from ( [ ( "MY_ENV" . to_string ( ) , "BASE" . to_string ( ) ) ] ) ) ,
318+ } ) ;
319+ input. add_requirement ( & base_req) ;
320+ assert_eq ! ( input. requirements. len( ) , 1 ) ;
321+
322+ let requirement = Requirement :: EnvVarRequirement ( EnvVarRequirement {
323+ env_def : EnviromentDefs :: Map ( HashMap :: from ( [ ( "MY_ENV" . to_string ( ) , "OVERWRITE" . to_string ( ) ) ] ) ) ,
324+ } ) ;
325+ input. add_requirement ( & requirement) ;
326+ assert_eq ! ( input. requirements. len( ) , 1 ) ;
327+ assert_eq ! ( input. requirements[ 0 ] , requirement) ;
328+ }
329+ }
0 commit comments