2121 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222 * SOFTWARE.
2323 */
24- use serde:: { Serialize , Deserialize } ;
24+ use serde:: { Deserialize , Serialize } ;
2525use serde_json:: json;
2626use std:: error:: Error ;
2727use std:: io:: stdin;
@@ -51,17 +51,11 @@ enum ListValue {
5151#[ serde( tag = "type" ) ]
5252enum FieldValue {
5353 #[ serde( rename = "string" ) ]
54- String {
55- field_id : u32 ,
56- } ,
54+ String { field_id : u32 } ,
5755 #[ serde( rename = "text" ) ]
58- Text {
59- field_id : u32 ,
60- } ,
56+ Text { field_id : u32 } ,
6157 #[ serde( rename = "art_link" ) ]
62- Link {
63- field_id : u32 ,
64- } ,
58+ Link { field_id : u32 } ,
6559 #[ serde( rename = "sb" ) ]
6660 SelectBox {
6761 field_id : u32 ,
@@ -70,15 +64,9 @@ enum FieldValue {
7064 bind_value_ids : Vec < u32 > ,
7165 } ,
7266 #[ serde( rename = "aid" ) ]
73- ArtifactId {
74- field_id : u32 ,
75- label : String ,
76- } ,
67+ ArtifactId { field_id : u32 , label : String } ,
7768 #[ serde( rename = "rb" ) ]
78- RadioButton {
79- field_id : u32 ,
80- label : String ,
81- } ,
69+ RadioButton { field_id : u32 , label : String } ,
8270}
8371
8472#[ derive( Serialize , Deserialize ) ]
@@ -103,7 +91,7 @@ struct TrackerField {
10391#[ derive( Serialize , Deserialize ) ]
10492struct Tracker {
10593 id : u32 ,
106- fields : Vec < TrackerField >
94+ fields : Vec < TrackerField > ,
10795}
10896
10997#[ derive( Serialize , Deserialize ) ]
@@ -120,32 +108,51 @@ fn convert_label_to_integer(label: String) -> Result<u32, Box<dyn Error>> {
120108 }
121109}
122110
123- fn find_select_box_by_label < ' a > ( changeset : & ' a Changeset , target_label : & str ) -> Option < & ' a FieldValue > {
124- changeset. values . iter ( ) . find ( |field_value| match field_value {
125- FieldValue :: SelectBox { label, .. } if label == target_label => true ,
126- _ => false ,
127- } )
111+ fn find_select_box_by_label < ' a > (
112+ changeset : & ' a Changeset ,
113+ target_label : & str ,
114+ ) -> Option < & ' a FieldValue > {
115+ changeset
116+ . values
117+ . iter ( )
118+ . find ( |field_value| match field_value {
119+ FieldValue :: SelectBox { label, .. } if label == target_label => true ,
120+ _ => false ,
121+ } )
128122}
129123
130124fn find_value_matching_label ( field_value : & FieldValue ) -> Option < String > {
131125 match field_value {
132- FieldValue :: SelectBox { values, .. } => values. first ( ) . and_then ( |first_value| match first_value {
133- ListValue :: StaticValue { label, .. } => Some ( label. to_string ( ) ) ,
134- _ => None ,
135- } ) ,
126+ FieldValue :: SelectBox { values, .. } => {
127+ values. first ( ) . and_then ( |first_value| match first_value {
128+ ListValue :: StaticValue { label, .. } => Some ( label. to_string ( ) ) ,
129+ _ => None ,
130+ } )
131+ }
136132 _ => None ,
137133 }
138134}
139135
140- fn find_select_box_value_by_label ( artifact : & Artifact , target_label : & str ) -> Result < Option < u32 > , Box < dyn Error > > {
136+ fn find_select_box_value_by_label (
137+ artifact : & Artifact ,
138+ target_label : & str ,
139+ ) -> Result < Option < u32 > , Box < dyn Error > > {
141140 find_select_box_by_label ( & artifact. current , target_label)
142- . and_then ( |field_value| find_value_matching_label ( field_value) )
143- . and_then ( |label| Some ( convert_label_to_integer ( label) ) )
144- . transpose ( )
141+ . and_then ( |field_value| find_value_matching_label ( field_value) )
142+ . and_then ( |label| Some ( convert_label_to_integer ( label) ) )
143+ . transpose ( )
145144}
146145
147- fn find_risk_value ( artifact : & Artifact , risk_field_label : & str , product : u32 ) -> Result < Option < FieldValueBinding > , Box < dyn Error > > {
148- let risk_field_option = artifact. tracker . fields . iter ( ) . find ( |field| field. label == risk_field_label) ;
146+ fn find_risk_value (
147+ artifact : & Artifact ,
148+ risk_field_label : & str ,
149+ product : u32 ,
150+ ) -> Result < Option < FieldValueBinding > , Box < dyn Error > > {
151+ let risk_field_option = artifact
152+ . tracker
153+ . fields
154+ . iter ( )
155+ . find ( |field| field. label == risk_field_label) ;
149156
150157 if risk_field_option. is_none ( ) {
151158 return Err ( format ! ( "Cannot find field {}" , risk_field_label) . into ( ) ) ;
@@ -157,10 +164,9 @@ fn find_risk_value(artifact: &Artifact, risk_field_label: &str, product: u32) ->
157164 }
158165 let risk_values = risk_field. values . as_ref ( ) . unwrap ( ) ;
159166
160- let matching_value = risk_values. iter ( )
161- . find ( |value| {
162- value. label . as_str ( ) == product. to_string ( )
163- } ) ;
167+ let matching_value = risk_values
168+ . iter ( )
169+ . find ( |value| value. label . as_str ( ) == product. to_string ( ) ) ;
164170
165171 if let Some ( matching_value) = matching_value {
166172 Ok ( Some ( FieldValueBinding {
@@ -172,7 +178,12 @@ fn find_risk_value(artifact: &Artifact, risk_field_label: &str, product: u32) ->
172178 }
173179}
174180
175- fn process_risk_values ( artifact : & Artifact , severity_field_label : & str , probability_field_label : & str , risk_field_label : & str ) -> Result < Option < FieldValueBinding > , Box < dyn Error > > {
181+ fn process_risk_values (
182+ artifact : & Artifact ,
183+ severity_field_label : & str ,
184+ probability_field_label : & str ,
185+ risk_field_label : & str ,
186+ ) -> Result < Option < FieldValueBinding > , Box < dyn Error > > {
176187 let severity = find_select_box_value_by_label ( & artifact, severity_field_label) ?;
177188 if severity. is_none ( ) {
178189 return Ok ( None ) ;
@@ -184,7 +195,7 @@ fn process_risk_values(artifact: &Artifact, severity_field_label: &str, probabil
184195 return Ok ( None ) ;
185196 }
186197 let probability_value = probability. unwrap ( ) ;
187-
198+
188199 let product = severity_value * probability_value;
189200
190201 find_risk_value ( artifact, risk_field_label, product)
@@ -198,26 +209,30 @@ fn main() -> Result<(), Box<dyn Error>> {
198209
199210 let mut bindings: Vec < FieldValueBinding > = Vec :: new ( ) ;
200211 match process_risk_values ( & artifact, "Severity" , "Probability" , "Risk" ) {
201- Ok ( possible_binding) => {
202- match possible_binding {
203- Some ( binding) => bindings. push ( binding) ,
204- None => ( )
205- }
212+ Ok ( possible_binding) => match possible_binding {
213+ Some ( binding) => bindings. push ( binding) ,
214+ None => ( ) ,
206215 } ,
207216 Err ( e) => return Err ( e) ,
208217 }
209- match process_risk_values ( & artifact, "Residual severity" , "Residual probability" , "Residual risk level" ) {
210- Ok ( possible_binding) => {
211- match possible_binding {
212- Some ( binding) => bindings. push ( binding) ,
213- None => ( )
214- }
218+ match process_risk_values (
219+ & artifact,
220+ "Residual severity" ,
221+ "Residual probability" ,
222+ "Residual risk level" ,
223+ ) {
224+ Ok ( possible_binding) => match possible_binding {
225+ Some ( binding) => bindings. push ( binding) ,
226+ None => ( ) ,
215227 } ,
216228 Err ( e) => return Err ( e) ,
217229 }
218- println ! ( "{}" , json!( {
219- "values" : bindings
220- } ) ) ;
230+ println ! (
231+ "{}" ,
232+ json!( {
233+ "values" : bindings
234+ } )
235+ ) ;
221236
222237 Ok ( ( ) )
223238}
0 commit comments