Update Crates2Json::append to accept iter

instead of one single pair of value.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-22 17:57:25 +10:00
parent 6964eee5d1
commit 739b3ee247
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -1,6 +1,7 @@
use std::{ use std::{
collections::{BTreeMap, BTreeSet}, collections::{BTreeMap, BTreeSet},
fs, io, fs, io,
iter::IntoIterator,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@ -66,11 +67,13 @@ impl Crates2Json {
Ok(()) Ok(())
} }
pub fn append_to_path( pub fn append_to_path<Iter>(
path: impl AsRef<Path>, path: impl AsRef<Path>,
cvs: &CrateVersionSource, iter: Iter,
info: CrateInfo, ) -> Result<(), Crates2JsonParseError>
) -> Result<(), Crates2JsonParseError> { where
Iter: IntoIterator<Item = (CrateVersionSource, CrateInfo)>,
{
let mut c2 = match Self::load_from_path(path.as_ref()) { let mut c2 = match Self::load_from_path(path.as_ref()) {
Ok(c2) => c2, Ok(c2) => c2,
Err(Crates2JsonParseError::Io(io_err)) if io_err.kind() == io::ErrorKind::NotFound => { Err(Crates2JsonParseError::Io(io_err)) if io_err.kind() == io::ErrorKind::NotFound => {
@ -78,14 +81,19 @@ impl Crates2Json {
} }
Err(err) => return Err(err), 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())?; c2.write_to_path(path.as_ref())?;
Ok(()) Ok(())
} }
pub fn append(cvs: &CrateVersionSource, info: CrateInfo) -> Result<(), Crates2JsonParseError> { pub fn append<Iter>(iter: Iter) -> Result<(), Crates2JsonParseError>
Self::append_to_path(Self::default_path()?, cvs, info) where
Iter: IntoIterator<Item = (CrateVersionSource, CrateInfo)>,
{
Self::append_to_path(Self::default_path()?, iter)
} }
} }