Skip to main content

handy/
errors.rs

1use std::path::{Path, PathBuf};
2use thiserror::Error;
3
4/// Errors that can occur when using the concurrent collections from the collections module.
5#[derive(Debug, Error, PartialEq, Eq)]
6pub enum ConcurrentCollectionError {
7    #[error("lock is poisoned")]
8    Poison,
9}
10
11/// Errors that can occur when parsing numbers.
12#[derive(Debug, Error, PartialEq, Eq)]
13pub enum ParseError {
14    #[error("parse error: invalid number: {0}")]
15    InvalidNumber(String),
16}
17
18/// Errors that can occur when working with the [filesystem](`crate::fs`) module.
19#[derive(Debug, Error, PartialEq, Eq)]
20pub enum FsError {
21    #[error("path does not exist: {0}")]
22    PathDoesNotExist(PathBuf),
23
24    #[error("path is not a directory: {0}")]
25    PathIsNotDirectory(PathBuf),
26
27    #[error("failed to read directory entry")]
28    DirEntry,
29
30    #[error("failed to read directory: {0}")]
31    DirRead(PathBuf),
32
33    #[error("failed to get file type for `{0}`")]
34    FileType(PathBuf),
35
36    #[error("skipping non-file/non-directory entry: {0}")]
37    NonFileNonDir(PathBuf),
38}
39
40impl FsError {
41    /// Create a new [`FsError::PathDoesNotExist`]
42    pub fn path_does_not_exist<P>(path: P) -> Self
43    where
44        P: AsRef<Path>,
45    {
46        Self::PathDoesNotExist(path.as_ref().to_path_buf())
47    }
48
49    /// Create a new [`FsError::PathIsNotDirectory`]
50    pub fn path_is_not_directory<P>(path: P) -> Self
51    where
52        P: AsRef<Path>,
53    {
54        Self::PathIsNotDirectory(path.as_ref().to_path_buf())
55    }
56
57    /// Create a new [`FsError::DirRead`]
58    pub fn dir_read<P>(path: P) -> Self
59    where
60        P: AsRef<Path>,
61    {
62        Self::DirRead(path.as_ref().to_path_buf())
63    }
64
65    /// Create a new [`FsError::FileType`]
66    pub fn file_type<P>(path: P) -> Self
67    where
68        P: AsRef<Path>,
69    {
70        Self::FileType(path.as_ref().to_path_buf())
71    }
72
73    /// Create a new [`FsError::NonFileNonDir`]
74    pub fn non_file_non_dir<P>(path: P) -> Self
75    where
76        P: AsRef<Path>,
77    {
78        Self::NonFileNonDir(path.as_ref().to_path_buf())
79    }
80}