diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-02-03 09:11:49 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-02-04 09:14:01 +0200 |
commit | a2adcb5a90c15b473a2fcf114555443fba8a20ce (patch) | |
tree | 7ec36f244daa105b0da774d6705ef736f9135f64 /src/fsentry.rs | |
parent | bf08ea67ca035fc0e78364450599cefff7cd9bc6 (diff) | |
download | obnam2-a2adcb5a90c15b473a2fcf114555443fba8a20ce.tar.gz |
refactor: have per-module error enums
This means that a function that parses step bindings can't return an
error that the document is missing a title. Such an error return would
be nonsensical, and we use the Rust type system to prevent it, at a
small cost of being a bit verbose.
Additional benefit is that the library portion of Obnam doesn't return
anyhow::Result values anymore.
Diffstat (limited to 'src/fsentry.rs')
-rw-r--r-- | src/fsentry.rs | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/src/fsentry.rs b/src/fsentry.rs index eae11b4..89c1cc0 100644 --- a/src/fsentry.rs +++ b/src/fsentry.rs @@ -37,9 +37,20 @@ pub struct FilesystemEntry { symlink_target: Option<PathBuf>, } +#[derive(Debug, thiserror::Error)] +pub enum FsEntryError { + #[error("Unknown file kind {0}")] + UnknownFileKindCode(u8), + + #[error(transparent)] + IoError(#[from] std::io::Error), +} + +pub type FsEntryResult<T> = Result<T, FsEntryError>; + #[allow(clippy::len_without_is_empty)] impl FilesystemEntry { - pub fn from_metadata(path: &Path, meta: &Metadata) -> anyhow::Result<Self> { + pub fn from_metadata(path: &Path, meta: &Metadata) -> FsEntryResult<Self> { let kind = FilesystemKind::from_file_type(meta.file_type()); Ok(Self { path: path.to_path_buf().into_os_string().into_vec(), @@ -129,12 +140,12 @@ impl FilesystemKind { } } - pub fn from_code(code: u8) -> anyhow::Result<Self> { + pub fn from_code(code: u8) -> FsEntryResult<Self> { match code { 0 => Ok(FilesystemKind::Regular), 1 => Ok(FilesystemKind::Directory), 2 => Ok(FilesystemKind::Symlink), - _ => Err(Error::UnknownFileKindCode(code).into()), + _ => Err(FsEntryError::UnknownFileKindCode(code).into()), } } } |