From 0157a594e6f117316e777e84aee9d2dc7973c751 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 21 Jul 2022 19:54:45 +1000 Subject: [PATCH] Fix err handling in `CratesToml::append_to_path` Make it more robust to `io::Error`: Only create a `Self::default()` if fails to open the file. Signed-off-by: Jiahao XU --- src/metafiles/v1.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/metafiles/v1.rs b/src/metafiles/v1.rs index 12e34641..e0dbcb1e 100644 --- a/src/metafiles/v1.rs +++ b/src/metafiles/v1.rs @@ -1,6 +1,6 @@ use std::{ collections::{BTreeMap, BTreeSet}, - fs, + fs, io, path::{Path, PathBuf}, str::FromStr, }; @@ -48,7 +48,13 @@ impl CratesToml { cvs: CrateVersionSource, bins: BTreeSet, ) -> Result<(), CratesTomlParseError> { - let mut c1 = Self::load_from_path(path.as_ref()).unwrap_or_default(); + let mut c1 = match Self::load_from_path(path.as_ref()) { + Ok(c1) => c1, + Err(CratesTomlParseError::Io(io_err)) if io_err.kind() == io::ErrorKind::NotFound => { + Self::default() + } + Err(err) => return Err(err), + }; c1.insert(cvs, bins); c1.write_to_path(path.as_ref())?; @@ -73,7 +79,7 @@ impl FromStr for CratesToml { #[derive(Debug, Diagnostic, Error)] pub enum CratesTomlParseError { #[error(transparent)] - Io(#[from] std::io::Error), + Io(#[from] io::Error), #[error(transparent)] TomlParse(#[from] toml::de::Error),