From 862fed11b358cf0d8747decb0c343cfe94784bad Mon Sep 17 00:00:00 2001 From: diogo464 Date: Fri, 9 Jul 2021 03:41:15 -0400 Subject: Rework/Changed some error types. --- dotup/src/depot.rs | 49 ++++++++++++++++++++++++++++++++----------------- dotup/src/error.rs | 15 +++------------ dotup/src/lib.rs | 2 +- dotup/src/utils.rs | 6 +++--- 4 files changed, 39 insertions(+), 33 deletions(-) diff --git a/dotup/src/depot.rs b/dotup/src/depot.rs index 9fb156f..0930835 100644 --- a/dotup/src/depot.rs +++ b/dotup/src/depot.rs @@ -9,6 +9,32 @@ use thiserror::Error; use crate::{internal_prelude::*, Archive, ArchiveLink}; +#[derive(Debug, Error)] +pub enum LinkCreateError { + #[error("Link origin is outside depot base\nDepot : {}\nLink : {}", .depot_base.display(), .origin.display())] + LinkOriginOutsideDepot { + depot_base: PathBuf, + origin: PathBuf, + }, + #[error("Link path is not relative : {}", .0.display())] + LinkPathIsNotRelative(PathBuf), + #[error("Link origin doesnt exist : {}", .0.display())] + LinkOriginDoesntExist(PathBuf), + #[error(transparent)] + IOError(#[from] std::io::Error), +} + +#[derive(Debug, Error)] +pub enum LinkInstallError { + #[error(transparent)] + IOError(#[from] std::io::Error), + #[error("File already exists at {}", .0.display())] + FileExists(PathBuf, Metadata), + /// .0 = LinkPath , .1 = LinkDestination + #[error("Link already exists {} -> {}", .0.display(), .1.display())] + LinkExists(PathBuf, PathBuf), +} + #[derive(Debug)] pub struct DepotConfig { /// The archive used to initialize the depot. @@ -132,7 +158,7 @@ impl Depot { /// Creates a new link from the description. /// The origin path must exist. - pub fn create_link(&mut self, link_desc: LinkDesc) -> Result { + pub fn create_link(&mut self, link_desc: LinkDesc) -> Result { let link = depot_create_link(self, link_desc)?; let link_id = depot_insert_link(self, link); Ok(link_id) @@ -222,7 +248,7 @@ fn depot_archive(depot: &Depot) -> Archive { /// Create a valid link for that given Depot using the given link desc. /// The link id is corrected when the link is inserted in the depot. -fn depot_create_link(depot: &Depot, link_desc: LinkDesc) -> Result { +fn depot_create_link(depot: &Depot, link_desc: LinkDesc) -> Result { // link_ensure_relative_path(&link_desc.origin)?; link_ensure_relative_path(&link_desc.destination)?; debug_assert!(utils::is_canonical(&depot.base_path())?); @@ -233,14 +259,14 @@ fn depot_create_link(depot: &Depot, link_desc: LinkDesc) -> Result { Ok(canonical) => canonical, Err(e) => match e.kind() { std::io::ErrorKind::NotFound => { - return Err(Error::LinkOriginDoesntExist(origin_joined)) + return Err(LinkCreateError::LinkOriginDoesntExist(origin_joined)) } _ => return Err(e.into()), }, }; if !origin_canonical.starts_with(depot.base_path()) { - return Err(Error::LinkOriginOutsideDepot { + return Err(LinkCreateError::LinkOriginOutsideDepot { depot_base: depot.base_path().to_path_buf(), origin: origin_canonical, }); @@ -270,17 +296,6 @@ fn depot_remove_link(depot: &mut Depot, link_id: LinkID) { depot.links.remove(link_id); } -#[derive(Debug, Error)] -pub enum LinkInstallError { - #[error(transparent)] - IOError(#[from] std::io::Error), - #[error("File already exists at {}", .0.display())] - FileExists(PathBuf, Metadata), - /// .0 = LinkPath , .1 = LinkDestination - #[error("Link already exists {} -> {}", .0.display(), .1.display())] - LinkExists(PathBuf, PathBuf), -} - fn depot_install_link( _depot: &Depot, link: &Link, @@ -365,9 +380,9 @@ fn depot_links(depot: &Depot) -> impl Iterator { depot.links.values() } -fn link_ensure_relative_path(path: &Path) -> Result<()> { +fn link_ensure_relative_path(path: &Path) -> Result<(), LinkCreateError> { if !path.is_relative() { - return Err(Error::LinkPathIsNotRelative(path.to_path_buf())); + return Err(LinkCreateError::LinkPathIsNotRelative(path.to_path_buf())); } Ok(()) } diff --git a/dotup/src/error.rs b/dotup/src/error.rs index 2a6f241..8ad801d 100644 --- a/dotup/src/error.rs +++ b/dotup/src/error.rs @@ -1,23 +1,14 @@ -use crate::LinkInstallError; +use crate::{LinkCreateError, LinkInstallError}; use std::path::PathBuf; use thiserror::Error; #[derive(Debug, Error)] pub enum Error { - #[error("Link origin is outside depot base\nDepot : {}\nLink : {}", .depot_base.display(), .origin.display())] - LinkOriginOutsideDepot { - depot_base: PathBuf, - origin: PathBuf, - }, #[error("Link install error : {0}")] LinkInstallError(#[from] LinkInstallError), - #[error("Link path is not relative : {}", .0.display())] - LinkPathIsNotRelative(PathBuf), - #[error("Link origin is not a file exist : {}", .0.display())] - LinkOriginIsNotFile(PathBuf), + #[error("Link create error : {0}")] + LinkCreateError(#[from] LinkCreateError), #[error("Link origin doesnt exist : {}", .0.display())] - LinkOriginDoesntExist(PathBuf), - #[error("The archive path is not a file. It many not exist or there could be a permission's problem.")] ArchivePathNotFile(PathBuf), #[error("The archive path did not exist : {}\n{}", .0.display(), .1)] ArchiveMissing(PathBuf, std::io::Error), diff --git a/dotup/src/lib.rs b/dotup/src/lib.rs index 6ef59e6..49639ce 100644 --- a/dotup/src/lib.rs +++ b/dotup/src/lib.rs @@ -8,7 +8,7 @@ pub use archive::{ archive_deserialize, archive_exists, archive_read, archive_serialize, archive_write, Archive, ArchiveLink, }; -pub use depot::{Depot, DepotConfig, Link, LinkDesc, LinkID, LinkInstallError}; +pub use depot::{Depot, DepotConfig, Link, LinkCreateError, LinkDesc, LinkID, LinkInstallError}; pub use error::{Error, Result}; pub(crate) mod internal_prelude { diff --git a/dotup/src/utils.rs b/dotup/src/utils.rs index cfcab0f..b961ec3 100644 --- a/dotup/src/utils.rs +++ b/dotup/src/utils.rs @@ -2,7 +2,7 @@ use std::path::{Component, Path, PathBuf}; use crate::internal_prelude::*; -pub fn is_file(path: impl AsRef) -> Result { +pub fn is_file(path: impl AsRef) -> std::io::Result { let metadata = match std::fs::metadata(path) { Ok(metadata) => metadata, Err(e) => match e.kind() { @@ -13,7 +13,7 @@ pub fn is_file(path: impl AsRef) -> Result { Ok(metadata.is_file()) } -pub fn is_directory(path: impl AsRef) -> Result { +pub fn is_directory(path: impl AsRef) -> std::io::Result { let metadata = match std::fs::metadata(path) { Ok(metadata) => metadata, Err(e) => match e.kind() { @@ -24,7 +24,7 @@ pub fn is_directory(path: impl AsRef) -> Result { Ok(metadata.is_dir()) } -pub fn is_canonical(path: &Path) -> Result { +pub fn is_canonical(path: &Path) -> std::io::Result { Ok(path == path.canonicalize()?.as_path()) } -- cgit