Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/ostree-ext/src/container/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::generic_decompress::Decompressor;
use crate::logging::system_repo_journal_print;
use crate::refescape;
use crate::sysroot::SysrootLock;
use crate::utils::ResultExt;
use anyhow::{anyhow, Context};
use bootc_utils::ResultExt;
use camino::{Utf8Path, Utf8PathBuf};
use canon_json::CanonJsonSerialize;
use cap_std_ext::cap_std;
Expand Down
2 changes: 1 addition & 1 deletion crates/ostree-ext/src/ostree_prepareroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use ostree::{gio, glib};

use crate::keyfileext::KeyFileExt;
use crate::ostree_manual;
use crate::utils::ResultExt;
use bootc_utils::ResultExt;

/// The relative path to ostree-prepare-root's config.
pub const CONF_PATH: &str = "ostree/prepare-root.conf";
Expand Down
33 changes: 0 additions & 33 deletions crates/ostree-ext/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1 @@
pub(crate) trait ResultExt<T, E: std::fmt::Display> {
/// Return the Ok value unchanged. In the err case, log it, and call the closure to compute the default
fn log_err_or_else<F>(self, default: F) -> T
where
F: FnOnce() -> T;
/// Return the Ok value unchanged. In the err case, log it, and return the default value
fn log_err_default(self) -> T
where
T: Default;
}

impl<T, E: std::fmt::Display> ResultExt<T, E> for Result<T, E> {
#[track_caller]
fn log_err_or_else<F>(self, default: F) -> T
where
F: FnOnce() -> T,
{
match self {
Ok(r) => r,
Err(e) => {
tracing::debug!("{e}");
default()
}
}
}

#[track_caller]
fn log_err_default(self) -> T
where
T: Default,
{
self.log_err_or_else(|| Default::default())
}
}
2 changes: 2 additions & 0 deletions crates/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ mod tracing_util;
pub use tracing_util::*;
/// Re-execute the current process
pub mod reexec;
mod result_ext;
pub use result_ext::*;
35 changes: 35 additions & 0 deletions crates/utils/src/result_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// Extension trait for Result types that provides logging capabilities
pub trait ResultExt<T, E: std::fmt::Display> {
/// Return the Ok value unchanged. In the err case, log it, and call the closure to compute the default
fn log_err_or_else<F>(self, default: F) -> T
where
F: FnOnce() -> T;
/// Return the Ok value unchanged. In the err case, log it, and return the default value
fn log_err_default(self) -> T
where
T: Default;
}

impl<T, E: std::fmt::Display> ResultExt<T, E> for Result<T, E> {
#[track_caller]
fn log_err_or_else<F>(self, default: F) -> T
where
F: FnOnce() -> T,
{
match self {
Ok(r) => r,
Err(e) => {
tracing::debug!("{e}");
default()
}
}
}

#[track_caller]
fn log_err_default(self) -> T
where
T: Default,
{
self.log_err_or_else(|| Default::default())
}
}