Skip to content

Commit e89d134

Browse files
authored
Merge pull request #1464 from cgwalters/utilstracing-log-err
utils: Move ResultExt to bootc_utils
2 parents 6c3bba5 + bdd600e commit e89d134

File tree

5 files changed

+39
-35
lines changed

5 files changed

+39
-35
lines changed

crates/ostree-ext/src/container/store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::generic_decompress::Decompressor;
1111
use crate::logging::system_repo_journal_print;
1212
use crate::refescape;
1313
use crate::sysroot::SysrootLock;
14-
use crate::utils::ResultExt;
1514
use anyhow::{anyhow, Context};
15+
use bootc_utils::ResultExt;
1616
use camino::{Utf8Path, Utf8PathBuf};
1717
use canon_json::CanonJsonSerialize;
1818
use cap_std_ext::cap_std;

crates/ostree-ext/src/ostree_prepareroot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use ostree::{gio, glib};
1717

1818
use crate::keyfileext::KeyFileExt;
1919
use crate::ostree_manual;
20-
use crate::utils::ResultExt;
20+
use bootc_utils::ResultExt;
2121

2222
/// The relative path to ostree-prepare-root's config.
2323
pub const CONF_PATH: &str = "ostree/prepare-root.conf";

crates/ostree-ext/src/utils.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1 @@
1-
pub(crate) trait ResultExt<T, E: std::fmt::Display> {
2-
/// Return the Ok value unchanged. In the err case, log it, and call the closure to compute the default
3-
fn log_err_or_else<F>(self, default: F) -> T
4-
where
5-
F: FnOnce() -> T;
6-
/// Return the Ok value unchanged. In the err case, log it, and return the default value
7-
fn log_err_default(self) -> T
8-
where
9-
T: Default;
10-
}
111

12-
impl<T, E: std::fmt::Display> ResultExt<T, E> for Result<T, E> {
13-
#[track_caller]
14-
fn log_err_or_else<F>(self, default: F) -> T
15-
where
16-
F: FnOnce() -> T,
17-
{
18-
match self {
19-
Ok(r) => r,
20-
Err(e) => {
21-
tracing::debug!("{e}");
22-
default()
23-
}
24-
}
25-
}
26-
27-
#[track_caller]
28-
fn log_err_default(self) -> T
29-
where
30-
T: Default,
31-
{
32-
self.log_err_or_else(|| Default::default())
33-
}
34-
}

crates/utils/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ mod tracing_util;
1414
pub use tracing_util::*;
1515
/// Re-execute the current process
1616
pub mod reexec;
17+
mod result_ext;
18+
pub use result_ext::*;

crates/utils/src/result_ext.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// Extension trait for Result types that provides logging capabilities
2+
pub trait ResultExt<T, E: std::fmt::Display> {
3+
/// Return the Ok value unchanged. In the err case, log it, and call the closure to compute the default
4+
fn log_err_or_else<F>(self, default: F) -> T
5+
where
6+
F: FnOnce() -> T;
7+
/// Return the Ok value unchanged. In the err case, log it, and return the default value
8+
fn log_err_default(self) -> T
9+
where
10+
T: Default;
11+
}
12+
13+
impl<T, E: std::fmt::Display> ResultExt<T, E> for Result<T, E> {
14+
#[track_caller]
15+
fn log_err_or_else<F>(self, default: F) -> T
16+
where
17+
F: FnOnce() -> T,
18+
{
19+
match self {
20+
Ok(r) => r,
21+
Err(e) => {
22+
tracing::debug!("{e}");
23+
default()
24+
}
25+
}
26+
}
27+
28+
#[track_caller]
29+
fn log_err_default(self) -> T
30+
where
31+
T: Default,
32+
{
33+
self.log_err_or_else(|| Default::default())
34+
}
35+
}

0 commit comments

Comments
 (0)