Skip to content

Commit 0583d0a

Browse files
committed
refactoring to option instead of result
Signed-off-by: simonsan <[email protected]>
1 parent 95c675f commit 0583d0a

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

crates/backend/src/local.rs

+28-23
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,10 @@ impl LocalBackend {
179179
///
180180
/// # Returns
181181
///
182-
/// The parent path of the file.
183-
///
184-
/// # Errors
185-
///
186-
/// * [`LocalBackendErrorKind::FileDoesNotHaveParent`] - If the file does not have a parent.
187-
///
188-
/// [`LocalBackendErrorKind::FileDoesNotHaveParent`]: LocalBackendErrorKind::FileDoesNotHaveParent
189-
fn parent_path(&self, tpe: FileType, id: &Id) -> Result<PathBuf> {
182+
/// The parent path of the file or `None` if the file does not have a parent.
183+
fn parent_path(&self, tpe: FileType, id: &Id) -> Option<PathBuf> {
190184
let path = self.path(tpe, id);
191-
path.parent().map_or(
192-
Err(LocalBackendErrorKind::FileDoesNotHaveParent(path.clone()).into()),
193-
|path| Ok(path.to_path_buf()),
194-
)
185+
path.parent().map(Path::to_path_buf)
195186
}
196187
}
197188

@@ -380,7 +371,7 @@ impl ReadBackend for LocalBackend {
380371
) -> RusticResult<Bytes> {
381372
trace!("reading tpe: {tpe:?}, id: {id}, offset: {offset}, length: {length}");
382373
let filename = self.path(tpe, id);
383-
let mut file = File::open(filename).map_err(|err| {
374+
let mut file = File::open(filename.clone()).map_err(|err| {
384375
RusticError::with_source(
385376
ErrorKind::Backend,
386377
"Failed to open the file `{path}`. Please check the file and try again.",
@@ -484,6 +475,10 @@ impl WriteBackend for LocalBackend {
484475
/// * If the length of the file could not be set.
485476
/// * If the bytes could not be written to the file.
486477
/// * If the OS Metadata could not be synced to disk.
478+
/// * If the file does not have a parent directory.
479+
/// * If the parent directory could not be created.
480+
/// * If the file cannot be opened, due to missing permissions.
481+
/// * If the file cannot be written to, due to lack of space on the disk.
487482
fn write_bytes(
488483
&self,
489484
tpe: FileType,
@@ -494,17 +489,27 @@ impl WriteBackend for LocalBackend {
494489
trace!("writing tpe: {:?}, id: {}", &tpe, &id);
495490
let filename = self.path(tpe, id);
496491

497-
// create parent directory if it does not exist
498-
if let Some(parent) = filename.parent() {
499-
fs::create_dir_all(parent).map_err(|err| {
500-
RusticError::with_source(
501-
ErrorKind::InputOutput,
502-
"Failed to create directories `{path}`. Please check the file and try again.",
503-
err,
492+
let Some(parent) = self.parent_path(tpe, id) else {
493+
return Err(
494+
RusticError::new(
495+
ErrorKind::Backend,
496+
"The file `{path}` does not have a parent directory. This may be empty or a root path. Please check the file and try again.",
504497
)
505-
.attach_context("path", parent.to_string_lossy())
506-
})?;
507-
}
498+
.attach_context("path", filename.display().to_string())
499+
.ask_report()
500+
);
501+
};
502+
503+
// create parent directory if it does not exist
504+
fs::create_dir_all(parent.clone()).map_err(|err| {
505+
RusticError::with_source(
506+
ErrorKind::InputOutput,
507+
"Failed to create directories `{path}`. Does the directory already exist? Please check the file and try again.",
508+
err,
509+
)
510+
.attach_context("path", parent.display().to_string())
511+
.ask_report()
512+
})?;
508513

509514
let mut file = fs::OpenOptions::new()
510515
.create(true)

0 commit comments

Comments
 (0)