# HG changeset patch # User Georges Racinet <georges.racinet@octobus.net> # Date 1686782463 -7200 # Thu Jun 15 00:41:03 2023 +0200 # Branch stable # Node ID 29fc9a2b1b36c190de6ccecbd363249f6dd0a62e # Parent 6576937ea5a7c7e127de71c579251738487f5406 RHGitaly: mapping file not found errors to `None` When a file contains a list of values, it is a common pattern that the absence of the file is equivalent to the list being empty. This `io_error_not_found_as_none` will help keeping duplication low (even if fairly trivial). diff --git a/rust/rhgitaly/src/lib.rs b/rust/rhgitaly/src/lib.rs --- a/rust/rhgitaly/src/lib.rs +++ b/rust/rhgitaly/src/lib.rs @@ -21,6 +21,7 @@ pub mod repository; pub mod service; pub mod streaming; +pub mod util; pub use config::*; pub use gitaly::*; diff --git a/rust/rhgitaly/src/util.rs b/rust/rhgitaly/src/util.rs new file mode 100644 --- /dev/null +++ b/rust/rhgitaly/src/util.rs @@ -0,0 +1,23 @@ +// Copyright 2023 Georges Racinet <georges.racinet@octobus.net> +// +// This software may be used and distributed according to the terms of the +// GNU General Public License version 2 or any later version. +// SPDX-License-Identifier: GPL-2.0-or-later +//! Common utilities +use std::io; + +/// Convert `NotFound` I/O error to `None` +/// +/// To be used when a file missing is logically equivalent to empty high level contents. +pub fn io_error_not_found_as_none<T>(res: io::Result<T>) -> io::Result<Option<T>> { + match res { + Ok(t) => Ok(Some(t)), + Err(e) => { + if e.kind() == io::ErrorKind::NotFound { + Ok(None) + } else { + Err(e) + } + } + } +}