Skip to content

Commit 45afb79

Browse files
committed
Add typed errors to ImmutableFile functions
1 parent 7ed9edb commit 45afb79

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

mithril-common/src/digesters/digester.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::digesters::immutable_file::ImmutableFileListingError;
12
use crate::entities::ImmutableFileNumber;
23
use std::io;
34
use thiserror::Error;
@@ -13,8 +14,8 @@ pub struct DigesterResult {
1314

1415
#[derive(Error, Debug)]
1516
pub enum DigesterError {
16-
#[error("Immutable files listing failed: `{0}`")]
17-
ListImmutablesError(String),
17+
#[error("Immutable files listing failed")]
18+
ListImmutablesError(#[from] ImmutableFileListingError),
1819

1920
#[error("At least two immutables chunk should exists")]
2021
NotEnoughImmutable(),

mithril-common/src/digesters/immutable_digester.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ impl ImmutableDigester {
4848

4949
impl Digester for ImmutableDigester {
5050
fn compute_digest(&self) -> Result<DigesterResult, DigesterError> {
51-
let immutables = ImmutableFile::list_completed_in_dir(&*self.db_directory)
52-
.map_err(DigesterError::ListImmutablesError)?;
51+
let immutables = ImmutableFile::list_completed_in_dir(&*self.db_directory)?;
5352
let last_immutable = immutables
5453
.last()
5554
.ok_or(DigesterError::NotEnoughImmutable())?;

mithril-common/src/digesters/immutable_file.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use crate::entities::ImmutableFileNumber;
22

33
use std::ffi::OsStr;
4+
use std::io;
5+
use std::num::ParseIntError;
46
use std::path::{Path, PathBuf};
5-
7+
use thiserror::Error;
68
use walkdir::WalkDir;
79

810
fn is_immutable(path: &Path) -> bool {
@@ -16,18 +18,33 @@ pub struct ImmutableFile {
1618
pub number: ImmutableFileNumber,
1719
}
1820

21+
#[derive(Error, Debug)]
22+
pub enum ImmutableFileCreationError {
23+
#[error("Couldn't extract the file stem for '{path:?}'")]
24+
FileStemExtraction { path: PathBuf },
25+
#[error("Couldn't extract the filename as string for '{path:?}'")]
26+
FileNameExtraction { path: PathBuf },
27+
#[error("Error while parsing immutable file number")]
28+
FileNumberParsing(#[from] ParseIntError),
29+
}
30+
31+
#[derive(Error, Debug)]
32+
pub enum ImmutableFileListingError {
33+
#[error("metadata parsing failed")]
34+
MetadataParsing(#[from] io::Error),
35+
#[error("immutable file creation error")]
36+
ImmutableFileCreation(#[from] ImmutableFileCreationError),
37+
}
38+
1939
impl ImmutableFile {
20-
pub fn new(path: PathBuf) -> Result<Self, String> {
40+
pub fn new(path: PathBuf) -> Result<Self, ImmutableFileCreationError> {
2141
let filename = path
2242
.file_stem()
23-
.ok_or(format!("Couldn't extract the file stem for '{:?}'", path))?;
24-
let filename = filename.to_str().ok_or(format!(
25-
"Couldn't extract the filename as string for '{:?}'",
26-
path
27-
))?;
28-
let immutable_file_number = filename
29-
.parse::<ImmutableFileNumber>()
30-
.map_err(|e| e.to_string())?;
43+
.ok_or(ImmutableFileCreationError::FileStemExtraction { path: path.clone() })?;
44+
let filename = filename
45+
.to_str()
46+
.ok_or(ImmutableFileCreationError::FileNameExtraction { path: path.clone() })?;
47+
let immutable_file_number = filename.parse::<ImmutableFileNumber>()?;
3148

3249
Ok(Self {
3350
path,
@@ -39,15 +56,17 @@ impl ImmutableFile {
3956
///
4057
/// Important Note: It will skip the last chunk / primary / secondary trio since they're not yet
4158
/// complete.
42-
pub fn list_completed_in_dir(dir: &Path) -> Result<Vec<ImmutableFile>, String> {
59+
pub fn list_completed_in_dir(
60+
dir: &Path,
61+
) -> Result<Vec<ImmutableFile>, ImmutableFileListingError> {
4362
let mut files: Vec<ImmutableFile> = vec![];
4463

4564
for path in WalkDir::new(dir)
4665
.into_iter()
4766
.filter_map(|file| file.ok())
4867
.map(|f| f.path().to_owned())
4968
{
50-
let metadata = path.metadata().map_err(|e| e.to_string())?;
69+
let metadata = path.metadata()?;
5170
if metadata.is_file() && is_immutable(&path) {
5271
let immutable_file = ImmutableFile::new(path)?;
5372
files.push(immutable_file);

mithril-common/src/digesters/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ mod immutable_file;
44

55
pub use digester::{Digester, DigesterError, DigesterResult};
66
pub use immutable_digester::ImmutableDigester;
7-
pub use immutable_file::ImmutableFile;
7+
pub use immutable_file::{ImmutableFile, ImmutableFileCreationError, ImmutableFileListingError};

0 commit comments

Comments
 (0)