use super::*;
pub(crate) fn hooks_protect_try_healthy(boundary: ErrorBoundary) -> Option<Rc<dyn Fn(Event)>> {
Some(Rc::new(move |_: Event| {
let result: Result<u32, String> = boundary.try_with(|| 7_u32);
let _: Result<u32, String> = result;
boundary.reset();
}))
}
pub(crate) fn hooks_protect_try_panic(boundary: ErrorBoundary) -> Option<Rc<dyn Fn(Event)>> {
Some(Rc::new(move |_: Event| {
let _: Result<u32, String> = boundary.try_with(|| 7_u32);
boundary.report_error(HOOKS_PROTECT_DEMO_ERROR_MESSAGE);
}))
}
pub(crate) fn hooks_protect_reset(boundary: ErrorBoundary) -> Option<Rc<dyn Fn(Event)>> {
Some(Rc::new(move |_: Event| {
boundary.reset();
}))
}
pub(crate) fn hooks_protect_profile_slow(profiler: ProfilerHandle) -> Option<Rc<dyn Fn(Event)>> {
Some(Rc::new(move |_: Event| {
profiler.measure(HOOKS_PROTECT_PROFILER_LABEL_SLOW_OP, || {
let mut accumulator: u64 = 0_u64;
for index in 0_u64..1_000_000_u64 {
accumulator = accumulator.wrapping_add(index);
}
let _: u64 = accumulator;
});
}))
}
pub(crate) fn hooks_protect_profile_clear(profiler: ProfilerHandle) -> Option<Rc<dyn Fn(Event)>> {
Some(Rc::new(move |_: Event| {
profiler.clear();
}))
}
pub(crate) fn hooks_protect_entry_count(profiler: ProfilerHandle) -> usize {
profiler.get_entries().get().len()
}
pub(crate) fn hooks_protect_is_healthy(boundary: &ErrorBoundary) -> bool {
matches!(boundary.get_phase().get(), ErrorBoundaryPhase::Healthy)
}
pub(crate) fn hooks_protect_is_caught(boundary: &ErrorBoundary) -> bool {
matches!(boundary.get_phase().get(), ErrorBoundaryPhase::Caught(_))
}
pub(crate) fn hooks_protect_phase_label(boundary: &ErrorBoundary) -> String {
match boundary.get_phase().get() {
ErrorBoundaryPhase::Healthy => String::from(HOOKS_PROTECT_PHASE_LABEL_HEALTHY),
ErrorBoundaryPhase::Caught(message) => format!("Caught({message})"),
}
}