refactor(link): add LinkError::IoError wrapping, fix happy path

Signed-off-by: Christina Sørensen <christina@cafkafk.com>
This commit is contained in:
Christina Sørensen 2023-10-17 06:59:06 +02:00
parent 7a333b53bd
commit 9c474d665e
Signed by: cafkafk
GPG key ID: 26C542FD97F965CE

View file

@ -123,13 +123,14 @@ pub struct SeriesItem<'series> {
pub closure: Box<dyn Fn(&Repo) -> (bool)>, pub closure: Box<dyn Fn(&Repo) -> (bool)>,
} }
#[derive(Debug, Clone)] #[derive(Debug)]
pub enum LinkError { pub enum LinkError {
AlreadyLinked(String, String), AlreadyLinked(String, String),
DifferentLink(String, String), DifferentLink(String, String),
FileExists(String, String), FileExists(String, String),
BrokenSymlinkExists(String, String), BrokenSymlinkExists(String, String),
FailedCreatingLink(String, String), FailedCreatingLink(String, String),
IoError(std::io::Error),
} }
impl std::fmt::Display for LinkError { impl std::fmt::Display for LinkError {
@ -147,6 +148,7 @@ impl std::fmt::Display for LinkError {
write!(f, "Linking {tx} -> {rx} failed: broken symlink") write!(f, "Linking {tx} -> {rx} failed: broken symlink")
} }
LinkError::FailedCreatingLink(tx, rx) => write!(f, "Linking {tx} -> {rx} failed"), 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<std::io::Error> 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<bool, LinkError> { fn handle_file_exists(selff: &Link, tx_path: &Path, rx_path: &Path) -> Result<bool, LinkError> {
match rx_path.read_link() { match rx_path.read_link() {
Ok(file) Ok(file)
@ -216,11 +224,8 @@ impl Link {
)) ))
} }
Ok(false) => { Ok(false) => {
symlink(&self.tx, &self.rx).expect("failed to create link"); symlink(&self.tx, &self.rx)?;
Err(LinkError::FailedCreatingLink( Ok(true)
tx_path.to_string_lossy().to_string(),
rx_path.to_string_lossy().to_string(),
))
} }
Err(error) => { Err(error) => {
error!("Linking {} -> {} failed: {}", &self.tx, &self.rx, error); error!("Linking {} -> {} failed: {}", &self.tx, &self.rx, error);
@ -634,6 +639,9 @@ impl Config {
Err(e @ LinkError::FailedCreatingLink(_, _)) => { Err(e @ LinkError::FailedCreatingLink(_, _)) => {
sp.stop_and_persist(failure_str(), format!("{e}")) 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}")), Err(e) => sp.stop_and_persist(failure_str(), format!("{e}")),
_ => sp.stop_and_persist( _ => sp.stop_and_persist(
failure_str(), failure_str(),