refactor(link): add LinkError and propagate errors up to runner #14

Merged
cafkafk merged 6 commits from cafk-link-result-refactor into main 2023-10-17 07:09:30 +02:00
Showing only changes of commit 7b88057b3f - Show all commits

View file

@ -176,32 +176,19 @@ fn handle_file_exists(selff: &Link, tx_path: &Path, rx_path: &Path) -> Result<bo
if file.canonicalize().expect("failed to canonicalize file") if file.canonicalize().expect("failed to canonicalize file")
== tx_path.canonicalize().expect("failed to canonicalize path") => == tx_path.canonicalize().expect("failed to canonicalize path") =>
{ {
debug!(
"Linking {} -> {} failed: file already linked",
&selff.tx, &selff.rx
);
Err(LinkError::AlreadyLinked( Err(LinkError::AlreadyLinked(
tx_path.to_string_lossy().to_string(), tx_path.to_string_lossy().to_string(),
rx_path.to_string_lossy().to_string(), rx_path.to_string_lossy().to_string(),
)) ))
} }
Ok(file) => { Ok(file) => Err(LinkError::DifferentLink(
error!( tx_path.to_string_lossy().to_string(),
"Linking {} -> {} failed: link to different file exists", rx_path.to_string_lossy().to_string(),
&selff.tx, &selff.rx )),
); Err(error) => Err(LinkError::FileExists(
Err(LinkError::DifferentLink( tx_path.to_string_lossy().to_string(),
tx_path.to_string_lossy().to_string(), rx_path.to_string_lossy().to_string(),
rx_path.to_string_lossy().to_string(), )),
))
}
Err(error) => {
error!("Linking {} -> {} failed: file exists", &selff.tx, &selff.rx);
Err(LinkError::FileExists(
tx_path.to_string_lossy().to_string(),
rx_path.to_string_lossy().to_string(),
))
}
} }
} }
@ -213,27 +200,18 @@ impl Link {
match rx_path.try_exists() { match rx_path.try_exists() {
// TODO: unwrap defeats the purpose here. // TODO: unwrap defeats the purpose here.
Ok(true) => handle_file_exists(self, tx_path, rx_path), Ok(true) => handle_file_exists(self, tx_path, rx_path),
Ok(false) if rx_path.is_symlink() => { Ok(false) if rx_path.is_symlink() => Err(LinkError::FileExists(
error!( tx_path.to_string_lossy().to_string(),
"Linking {} -> {} failed: broken symlink", rx_path.to_string_lossy().to_string(),
&self.tx, &self.rx )),
);
Err(LinkError::FileExists(
tx_path.to_string_lossy().to_string(),
rx_path.to_string_lossy().to_string(),
))
}
Ok(false) => { Ok(false) => {
symlink(&self.tx, &self.rx)?; symlink(&self.tx, &self.rx)?;
Ok(true) Ok(true)
} }
Err(error) => { Err(error) => Err(LinkError::FailedCreatingLink(
error!("Linking {} -> {} failed: {}", &self.tx, &self.rx, error); tx_path.to_string_lossy().to_string(),
Err(LinkError::FailedCreatingLink( rx_path.to_string_lossy().to_string(),
tx_path.to_string_lossy().to_string(), )),
rx_path.to_string_lossy().to_string(),
))
}
} }
} }
} }