diff options
| author | diogo464 <[email protected]> | 2021-07-09 03:41:15 -0400 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2021-07-09 03:41:15 -0400 |
| commit | 862fed11b358cf0d8747decb0c343cfe94784bad (patch) | |
| tree | 68c351a766174ce5aa12debd6c65abdeee00a209 | |
| parent | 29a6a6a836e5966ab2a57a5ea97e455a27a11acf (diff) | |
Rework/Changed some error types.
| -rw-r--r-- | dotup/src/depot.rs | 49 | ||||
| -rw-r--r-- | dotup/src/error.rs | 15 | ||||
| -rw-r--r-- | dotup/src/lib.rs | 2 | ||||
| -rw-r--r-- | 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; | |||
| 9 | 9 | ||
| 10 | use crate::{internal_prelude::*, Archive, ArchiveLink}; | 10 | use crate::{internal_prelude::*, Archive, ArchiveLink}; |
| 11 | 11 | ||
| 12 | #[derive(Debug, Error)] | ||
| 13 | pub enum LinkCreateError { | ||
| 14 | #[error("Link origin is outside depot base\nDepot : {}\nLink : {}", .depot_base.display(), .origin.display())] | ||
| 15 | LinkOriginOutsideDepot { | ||
| 16 | depot_base: PathBuf, | ||
| 17 | origin: PathBuf, | ||
| 18 | }, | ||
| 19 | #[error("Link path is not relative : {}", .0.display())] | ||
| 20 | LinkPathIsNotRelative(PathBuf), | ||
| 21 | #[error("Link origin doesnt exist : {}", .0.display())] | ||
| 22 | LinkOriginDoesntExist(PathBuf), | ||
| 23 | #[error(transparent)] | ||
| 24 | IOError(#[from] std::io::Error), | ||
| 25 | } | ||
| 26 | |||
| 27 | #[derive(Debug, Error)] | ||
| 28 | pub enum LinkInstallError { | ||
| 29 | #[error(transparent)] | ||
| 30 | IOError(#[from] std::io::Error), | ||
| 31 | #[error("File already exists at {}", .0.display())] | ||
| 32 | FileExists(PathBuf, Metadata), | ||
| 33 | /// .0 = LinkPath , .1 = LinkDestination | ||
| 34 | #[error("Link already exists {} -> {}", .0.display(), .1.display())] | ||
| 35 | LinkExists(PathBuf, PathBuf), | ||
| 36 | } | ||
| 37 | |||
| 12 | #[derive(Debug)] | 38 | #[derive(Debug)] |
| 13 | pub struct DepotConfig { | 39 | pub struct DepotConfig { |
| 14 | /// The archive used to initialize the depot. | 40 | /// The archive used to initialize the depot. |
| @@ -132,7 +158,7 @@ impl Depot { | |||
| 132 | 158 | ||
| 133 | /// Creates a new link from the description. | 159 | /// Creates a new link from the description. |
| 134 | /// The origin path must exist. | 160 | /// The origin path must exist. |
| 135 | pub fn create_link(&mut self, link_desc: LinkDesc) -> Result<LinkID> { | 161 | pub fn create_link(&mut self, link_desc: LinkDesc) -> Result<LinkID, LinkCreateError> { |
| 136 | let link = depot_create_link(self, link_desc)?; | 162 | let link = depot_create_link(self, link_desc)?; |
| 137 | let link_id = depot_insert_link(self, link); | 163 | let link_id = depot_insert_link(self, link); |
| 138 | Ok(link_id) | 164 | Ok(link_id) |
| @@ -222,7 +248,7 @@ fn depot_archive(depot: &Depot) -> Archive { | |||
| 222 | 248 | ||
| 223 | /// Create a valid link for that given Depot using the given link desc. | 249 | /// Create a valid link for that given Depot using the given link desc. |
| 224 | /// The link id is corrected when the link is inserted in the depot. | 250 | /// The link id is corrected when the link is inserted in the depot. |
| 225 | fn depot_create_link(depot: &Depot, link_desc: LinkDesc) -> Result<Link> { | 251 | fn depot_create_link(depot: &Depot, link_desc: LinkDesc) -> Result<Link, LinkCreateError> { |
| 226 | // link_ensure_relative_path(&link_desc.origin)?; | 252 | // link_ensure_relative_path(&link_desc.origin)?; |
| 227 | link_ensure_relative_path(&link_desc.destination)?; | 253 | link_ensure_relative_path(&link_desc.destination)?; |
| 228 | debug_assert!(utils::is_canonical(&depot.base_path())?); | 254 | debug_assert!(utils::is_canonical(&depot.base_path())?); |
| @@ -233,14 +259,14 @@ fn depot_create_link(depot: &Depot, link_desc: LinkDesc) -> Result<Link> { | |||
| 233 | Ok(canonical) => canonical, | 259 | Ok(canonical) => canonical, |
| 234 | Err(e) => match e.kind() { | 260 | Err(e) => match e.kind() { |
| 235 | std::io::ErrorKind::NotFound => { | 261 | std::io::ErrorKind::NotFound => { |
| 236 | return Err(Error::LinkOriginDoesntExist(origin_joined)) | 262 | return Err(LinkCreateError::LinkOriginDoesntExist(origin_joined)) |
| 237 | } | 263 | } |
| 238 | _ => return Err(e.into()), | 264 | _ => return Err(e.into()), |
| 239 | }, | 265 | }, |
| 240 | }; | 266 | }; |
| 241 | 267 | ||
| 242 | if !origin_canonical.starts_with(depot.base_path()) { | 268 | if !origin_canonical.starts_with(depot.base_path()) { |
| 243 | return Err(Error::LinkOriginOutsideDepot { | 269 | return Err(LinkCreateError::LinkOriginOutsideDepot { |
| 244 | depot_base: depot.base_path().to_path_buf(), | 270 | depot_base: depot.base_path().to_path_buf(), |
| 245 | origin: origin_canonical, | 271 | origin: origin_canonical, |
| 246 | }); | 272 | }); |
| @@ -270,17 +296,6 @@ fn depot_remove_link(depot: &mut Depot, link_id: LinkID) { | |||
| 270 | depot.links.remove(link_id); | 296 | depot.links.remove(link_id); |
| 271 | } | 297 | } |
| 272 | 298 | ||
| 273 | #[derive(Debug, Error)] | ||
| 274 | pub enum LinkInstallError { | ||
| 275 | #[error(transparent)] | ||
| 276 | IOError(#[from] std::io::Error), | ||
| 277 | #[error("File already exists at {}", .0.display())] | ||
| 278 | FileExists(PathBuf, Metadata), | ||
| 279 | /// .0 = LinkPath , .1 = LinkDestination | ||
| 280 | #[error("Link already exists {} -> {}", .0.display(), .1.display())] | ||
| 281 | LinkExists(PathBuf, PathBuf), | ||
| 282 | } | ||
| 283 | |||
| 284 | fn depot_install_link( | 299 | fn depot_install_link( |
| 285 | _depot: &Depot, | 300 | _depot: &Depot, |
| 286 | link: &Link, | 301 | link: &Link, |
| @@ -365,9 +380,9 @@ fn depot_links(depot: &Depot) -> impl Iterator<Item = &Link> { | |||
| 365 | depot.links.values() | 380 | depot.links.values() |
| 366 | } | 381 | } |
| 367 | 382 | ||
| 368 | fn link_ensure_relative_path(path: &Path) -> Result<()> { | 383 | fn link_ensure_relative_path(path: &Path) -> Result<(), LinkCreateError> { |
| 369 | if !path.is_relative() { | 384 | if !path.is_relative() { |
| 370 | return Err(Error::LinkPathIsNotRelative(path.to_path_buf())); | 385 | return Err(LinkCreateError::LinkPathIsNotRelative(path.to_path_buf())); |
| 371 | } | 386 | } |
| 372 | Ok(()) | 387 | Ok(()) |
| 373 | } | 388 | } |
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 @@ | |||
| 1 | use crate::LinkInstallError; | 1 | use crate::{LinkCreateError, LinkInstallError}; |
| 2 | use std::path::PathBuf; | 2 | use std::path::PathBuf; |
| 3 | use thiserror::Error; | 3 | use thiserror::Error; |
| 4 | 4 | ||
| 5 | #[derive(Debug, Error)] | 5 | #[derive(Debug, Error)] |
| 6 | pub enum Error { | 6 | pub enum Error { |
| 7 | #[error("Link origin is outside depot base\nDepot : {}\nLink : {}", .depot_base.display(), .origin.display())] | ||
| 8 | LinkOriginOutsideDepot { | ||
| 9 | depot_base: PathBuf, | ||
| 10 | origin: PathBuf, | ||
| 11 | }, | ||
| 12 | #[error("Link install error : {0}")] | 7 | #[error("Link install error : {0}")] |
| 13 | LinkInstallError(#[from] LinkInstallError), | 8 | LinkInstallError(#[from] LinkInstallError), |
| 14 | #[error("Link path is not relative : {}", .0.display())] | 9 | #[error("Link create error : {0}")] |
| 15 | LinkPathIsNotRelative(PathBuf), | 10 | LinkCreateError(#[from] LinkCreateError), |
| 16 | #[error("Link origin is not a file exist : {}", .0.display())] | ||
| 17 | LinkOriginIsNotFile(PathBuf), | ||
| 18 | #[error("Link origin doesnt exist : {}", .0.display())] | 11 | #[error("Link origin doesnt exist : {}", .0.display())] |
| 19 | LinkOriginDoesntExist(PathBuf), | ||
| 20 | #[error("The archive path is not a file. It many not exist or there could be a permission's problem.")] | ||
| 21 | ArchivePathNotFile(PathBuf), | 12 | ArchivePathNotFile(PathBuf), |
| 22 | #[error("The archive path did not exist : {}\n{}", .0.display(), .1)] | 13 | #[error("The archive path did not exist : {}\n{}", .0.display(), .1)] |
| 23 | ArchiveMissing(PathBuf, std::io::Error), | 14 | 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::{ | |||
| 8 | archive_deserialize, archive_exists, archive_read, archive_serialize, archive_write, Archive, | 8 | archive_deserialize, archive_exists, archive_read, archive_serialize, archive_write, Archive, |
| 9 | ArchiveLink, | 9 | ArchiveLink, |
| 10 | }; | 10 | }; |
| 11 | pub use depot::{Depot, DepotConfig, Link, LinkDesc, LinkID, LinkInstallError}; | 11 | pub use depot::{Depot, DepotConfig, Link, LinkCreateError, LinkDesc, LinkID, LinkInstallError}; |
| 12 | pub use error::{Error, Result}; | 12 | pub use error::{Error, Result}; |
| 13 | 13 | ||
| 14 | pub(crate) mod internal_prelude { | 14 | 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}; | |||
| 2 | 2 | ||
| 3 | use crate::internal_prelude::*; | 3 | use crate::internal_prelude::*; |
| 4 | 4 | ||
| 5 | pub fn is_file(path: impl AsRef<Path>) -> Result<bool> { | 5 | pub fn is_file(path: impl AsRef<Path>) -> std::io::Result<bool> { |
| 6 | let metadata = match std::fs::metadata(path) { | 6 | let metadata = match std::fs::metadata(path) { |
| 7 | Ok(metadata) => metadata, | 7 | Ok(metadata) => metadata, |
| 8 | Err(e) => match e.kind() { | 8 | Err(e) => match e.kind() { |
| @@ -13,7 +13,7 @@ pub fn is_file(path: impl AsRef<Path>) -> Result<bool> { | |||
| 13 | Ok(metadata.is_file()) | 13 | Ok(metadata.is_file()) |
| 14 | } | 14 | } |
| 15 | 15 | ||
| 16 | pub fn is_directory(path: impl AsRef<Path>) -> Result<bool> { | 16 | pub fn is_directory(path: impl AsRef<Path>) -> std::io::Result<bool> { |
| 17 | let metadata = match std::fs::metadata(path) { | 17 | let metadata = match std::fs::metadata(path) { |
| 18 | Ok(metadata) => metadata, | 18 | Ok(metadata) => metadata, |
| 19 | Err(e) => match e.kind() { | 19 | Err(e) => match e.kind() { |
| @@ -24,7 +24,7 @@ pub fn is_directory(path: impl AsRef<Path>) -> Result<bool> { | |||
| 24 | Ok(metadata.is_dir()) | 24 | Ok(metadata.is_dir()) |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | pub fn is_canonical(path: &Path) -> Result<bool> { | 27 | pub fn is_canonical(path: &Path) -> std::io::Result<bool> { |
| 28 | Ok(path == path.canonicalize()?.as_path()) | 28 | Ok(path == path.canonicalize()?.as_path()) |
| 29 | } | 29 | } |
| 30 | 30 | ||
