From 9c474d665e724bf603a4e80c4ad54dd699b7e47a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christina=20S=C3=B8rensen?= Date: Tue, 17 Oct 2023 06:59:06 +0200 Subject: [PATCH] refactor(link): add LinkError::IoError wrapping, fix happy path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christina Sørensen --- src/git.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/git.rs b/src/git.rs index 13e672c..55ed35b 100644 --- a/src/git.rs +++ b/src/git.rs @@ -123,13 +123,14 @@ pub struct SeriesItem<'series> { pub closure: Box (bool)>, } -#[derive(Debug, Clone)] +#[derive(Debug)] pub enum LinkError { AlreadyLinked(String, String), DifferentLink(String, String), FileExists(String, String), BrokenSymlinkExists(String, String), FailedCreatingLink(String, String), + IoError(std::io::Error), } impl std::fmt::Display for LinkError { @@ -147,6 +148,7 @@ impl std::fmt::Display for LinkError { write!(f, "Linking {tx} -> {rx} failed: broken symlink") } LinkError::FailedCreatingLink(tx, rx) => write!(f, "Linking {tx} -> {rx} failed"), + LinkError::IoError(err) => write!(f, "IO Error: {err}"), } } } @@ -162,6 +164,12 @@ impl std::error::Error for LinkError { // } } +impl From for LinkError { + fn from(err: std::io::Error) -> LinkError { + LinkError::IoError(err) + } +} + fn handle_file_exists(selff: &Link, tx_path: &Path, rx_path: &Path) -> Result { match rx_path.read_link() { Ok(file) @@ -216,11 +224,8 @@ impl Link { )) } Ok(false) => { - symlink(&self.tx, &self.rx).expect("failed to create link"); - Err(LinkError::FailedCreatingLink( - tx_path.to_string_lossy().to_string(), - rx_path.to_string_lossy().to_string(), - )) + symlink(&self.tx, &self.rx)?; + Ok(true) } Err(error) => { error!("Linking {} -> {} failed: {}", &self.tx, &self.rx, error); @@ -634,6 +639,9 @@ impl Config { Err(e @ LinkError::FailedCreatingLink(_, _)) => { sp.stop_and_persist(failure_str(), format!("{e}")) } + Err(e @ LinkError::IoError(_)) => { + sp.stop_and_persist(failure_str(), format!("{e}")) + } Err(e) => sp.stop_and_persist(failure_str(), format!("{e}")), _ => sp.stop_and_persist( failure_str(),