use super::*;
pub(crate) const HOOKS_PROTECT_PROFILER_LABEL_TRIGGER: &str = "render-trigger";
pub(crate) const HOOKS_PROTECT_TRIGGER_RENDER_VALUE: &str = "ok";
pub(crate) const HOOKS_PROTECT_DEMO_ERROR_MESSAGE: &str = "simulated failure";
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;
boundary.reset();
}))
}
pub(crate) fn hooks_protect_try_panic(boundary: ErrorBoundary) -> Option<Rc<dyn Fn(Event)>> {
Some(Rc::new(move |_: Event| {
let _ = 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("slow-op", || {
let mut accumulator: u64 = 0_u64;
for index in 0_u64..1_000_000_u64 {
accumulator = accumulator.wrapping_add(index);
}
let _ = 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("Healthy"),
ErrorBoundaryPhase::Caught(message) => format!("Caught({message})"),
}
}