use crate::element::Element;
use crate::error::RosaceError;
pub struct ErrorBoundary {
fallback: Box<dyn Fn(&RosaceError) -> Element + Send + Sync>,
child: Element,
}
impl ErrorBoundary {
pub fn new() -> Self {
ErrorBoundary {
fallback: Box::new(|e| Element::text(format!("Error: {e}"))),
child: Element::Empty,
}
}
pub fn fallback(
mut self,
f: impl Fn(&RosaceError) -> Element + Send + Sync + 'static,
) -> Self {
self.fallback = Box::new(f);
self
}
pub fn child(mut self, element: impl Into<Element>) -> Self {
self.child = element.into();
self
}
pub fn render(&self) -> Element {
self.child.clone()
}
}
impl Default for ErrorBoundary {
fn default() -> Self {
ErrorBoundary::new()
}
}