From fb3e35624b30d9380bd24b339495efccee00688c Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 21 Jul 2022 19:59:36 +1000 Subject: [PATCH] Fix err handling in `Crates2Json::append_to_path` Only uses `Self::default()` if the file is not found. Signed-off-by: Jiahao XU --- src/metafiles/v2.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/metafiles/v2.rs b/src/metafiles/v2.rs index 1501e238..3be0ead6 100644 --- a/src/metafiles/v2.rs +++ b/src/metafiles/v2.rs @@ -1,6 +1,6 @@ use std::{ collections::{BTreeMap, BTreeSet}, - fs, + fs, io, path::{Path, PathBuf}, }; @@ -69,7 +69,13 @@ impl Crates2Json { cvs: CrateVersionSource, info: CrateInfo, ) -> Result<(), Crates2JsonParseError> { - let mut c2 = Self::load_from_path(path.as_ref()).unwrap_or_default(); + 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 => { + Self::default() + } + Err(err) => return Err(err), + }; c2.insert(cvs, info); c2.write_to_path(path.as_ref())?; @@ -84,7 +90,7 @@ impl Crates2Json { #[derive(Debug, Diagnostic, Error)] pub enum Crates2JsonParseError { #[error(transparent)] - Io(#[from] std::io::Error), + Io(#[from] io::Error), #[error(transparent)] Json(#[from] serde_json::Error),