Skip to content

Commit

Permalink
refactor(color-eyre): improve add_frame_filter example and code rea…
Browse files Browse the repository at this point in the history
…dability
  • Loading branch information
0xAdk committed Aug 3, 2024
1 parent e5d92c3 commit 41de1e6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
16 changes: 6 additions & 10 deletions color-eyre/examples/custom_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ fn main() -> Result<(), Report> {

color_eyre::config::HookBuilder::default()
.add_frame_filter(Box::new(|frames| {
let filters = &["custom_filter::main"];
let filters = ["custom_filter::main"];

frames.retain(|frame| {
!filters.iter().any(|f| {
let name = if let Some(name) = frame.name.as_ref() {
name.as_str()
} else {
return true;
};

name.starts_with(f)
})
let Some(name) = frame.name.as_deref() else {
return true;
};

!filters.iter().any(|f| name.starts_with(f))
});
}))
.install()
Expand Down
28 changes: 10 additions & 18 deletions color-eyre/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,20 +641,16 @@ impl HookBuilder {
/// ```rust
/// color_eyre::config::HookBuilder::default()
/// .add_frame_filter(Box::new(|frames| {
/// let filters = &[
/// let filters = [
/// "uninteresting_function",
/// ];
///
/// frames.retain(|frame| {
/// !filters.iter().any(|f| {
/// let name = if let Some(name) = frame.name.as_ref() {
/// name.as_str()
/// } else {
/// return true;
/// };
/// let Some(name) = frame.name.as_deref() else {
/// return true;
/// };
///
/// name.starts_with(f)
/// })
/// !filters.iter().any(|f| name.starts_with(f))
/// });
/// }))
/// .install()
Expand Down Expand Up @@ -768,22 +764,18 @@ fn default_frame_filter(frames: &mut Vec<&Frame>) {
}

fn eyre_frame_filters(frames: &mut Vec<&Frame>) {
let filters = &[
let filters = [
"<color_eyre::Handler as eyre::EyreHandler>::default",
"eyre::",
"color_eyre::",
];

frames.retain(|frame| {
!filters.iter().any(|f| {
let name = if let Some(name) = frame.name.as_ref() {
name.as_str()
} else {
return true;
};
let Some(name) = frame.name.as_deref() else {
return true;
};

name.starts_with(f)
})
!filters.iter().any(|filter| name.starts_with(filter))
});
}

Expand Down

0 comments on commit 41de1e6

Please sign in to comment.