mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-21 13:08:42 +00:00
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 <Jiahao_XU@outlook.com>
This commit is contained in:
parent
31d9716d28
commit
0157a594e6
1 changed files with 9 additions and 3 deletions
|
@ -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<String>,
|
||||
) -> 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),
|
||||
|
|
Loading…
Add table
Reference in a new issue