From 1f46bef10ba86621eccebf1f4cd93c304cc52041 Mon Sep 17 00:00:00 2001 From: Lynne Date: Wed, 25 Aug 2021 15:44:21 +1000 Subject: [PATCH] slightly nicer JSON write_all --- src/formats.rs | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/formats.rs b/src/formats.rs index aeb61e4..4ea3c67 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -12,7 +12,7 @@ use snailquote::escape; use crate::findings::ScanError; use crate::utils::clap_long_version; use crate::Findings; -use itertools::Itertools; +use itertools::{Either, Itertools}; /// A macro for creating an array of `Writable`s without needing to pepper your code with `into()`s. /// # Usage @@ -340,21 +340,12 @@ impl Format for Json { findings: &'a Vec<&'a Findings>, } - let result = serde_json::to_writer_pretty( - f, - &SerdeEntries { - errors: &entries - .iter() - .filter_map(|e| e.as_ref().err()) - .sorted_unstable() - .collect(), - findings: &entries - .iter() - .filter_map(|f| f.as_ref().ok()) - .sorted_unstable() - .collect(), - }, - ); + let (errors, findings) = &entries.iter().partition_map(|entry| match entry { + Err(e) => Either::Left(e), + Ok(f) => Either::Right(f), + }); + + let result = serde_json::to_writer_pretty(f, &SerdeEntries { errors, findings }); if let Err(err) = result { log::error!("Error while serialising: {}", err);