From 739b3ee2472d48246985d24b62b2e44ec7ff7348 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Fri, 22 Jul 2022 17:57:25 +1000 Subject: [PATCH] Update `Crates2Json::append` to accept iter instead of one single pair of value. Signed-off-by: Jiahao XU --- src/metafiles/v2.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/metafiles/v2.rs b/src/metafiles/v2.rs index f9ea6a68..b6c8cc58 100644 --- a/src/metafiles/v2.rs +++ b/src/metafiles/v2.rs @@ -1,6 +1,7 @@ use std::{ collections::{BTreeMap, BTreeSet}, fs, io, + iter::IntoIterator, path::{Path, PathBuf}, }; @@ -66,11 +67,13 @@ impl Crates2Json { Ok(()) } - pub fn append_to_path( + pub fn append_to_path( path: impl AsRef, - cvs: &CrateVersionSource, - info: CrateInfo, - ) -> Result<(), Crates2JsonParseError> { + iter: Iter, + ) -> Result<(), Crates2JsonParseError> + where + Iter: IntoIterator, + { let mut c2 = match Self::load_from_path(path.as_ref()) { Ok(c2) => c2, Err(Crates2JsonParseError::Io(io_err)) if io_err.kind() == io::ErrorKind::NotFound => { @@ -78,14 +81,19 @@ impl Crates2Json { } Err(err) => return Err(err), }; - c2.insert(cvs, info); + for (cvs, info) in iter { + c2.insert(&cvs, info); + } c2.write_to_path(path.as_ref())?; Ok(()) } - pub fn append(cvs: &CrateVersionSource, info: CrateInfo) -> Result<(), Crates2JsonParseError> { - Self::append_to_path(Self::default_path()?, cvs, info) + pub fn append(iter: Iter) -> Result<(), Crates2JsonParseError> + where + Iter: IntoIterator, + { + Self::append_to_path(Self::default_path()?, iter) } }