1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use crate::{ScopeId, ScopeState};
use std::{
    any::{Any, TypeId},
    cell::RefCell,
    fmt::Debug,
};

/// A boundary that will capture any errors from child components
pub struct ErrorBoundary {
    error: RefCell<Option<CapturedError>>,
    _id: ScopeId,
}

/// An instance of an error captured by a descendant component.
pub struct CapturedError {
    /// The error captured by the error boundary
    pub error: Box<dyn Debug + 'static>,

    /// The scope that threw the error
    pub scope: ScopeId,
}

impl CapturedError {
    /// Downcast the error type into a concrete error type
    pub fn downcast<T: 'static>(&self) -> Option<&T> {
        if TypeId::of::<T>() == self.error.type_id() {
            let raw = self.error.as_ref() as *const _ as *const T;
            Some(unsafe { &*raw })
        } else {
            None
        }
    }
}

impl ErrorBoundary {
    pub fn new(id: ScopeId) -> Self {
        Self {
            error: RefCell::new(None),
            _id: id,
        }
    }

    /// Push an error into this Error Boundary
    pub fn insert_error(&self, scope: ScopeId, error: Box<dyn Debug + 'static>) {
        self.error.replace(Some(CapturedError { error, scope }));
    }
}

/// A trait to allow results to be thrown upwards to the nearest Error Boundary
///
/// The canonical way of using this trait is to throw results from hooks, aborting rendering
/// through question mark syntax. The throw method returns an option that evaluates to None
/// if there is an error, injecting the error to the nearest error boundary.
///
/// If the value is `Ok`, then throw returns the value, not aborting the rendering process.
///
/// The call stack is saved for this component and provided to the error boundary
///
/// ```rust, ignore
/// #[component]
/// fn App(cx: Scope, count: String) -> Element {
///     let id: i32 = count.parse().throw(cx)?;
///
///     cx.render(rsx! {
///         div { "Count {}" }
///     })
/// }
/// ```
pub trait Throw<S = ()>: Sized {
    /// The value that will be returned in if the given value is `Ok`.
    type Out;

    /// Returns an option that evaluates to None if there is an error, injecting the error to the nearest error boundary.
    ///
    /// If the value is `Ok`, then throw returns the value, not aborting the rendering process.
    ///
    /// The call stack is saved for this component and provided to the error boundary
    ///
    ///
    /// Note that you can also manually throw errors using the throw method on `ScopeState` directly,
    /// which is what this trait shells out to.
    ///
    ///
    /// ```rust, ignore
    /// #[component]
    /// fn App(cx: Scope, count: String) -> Element {
    ///     let id: i32 = count.parse().throw(cx)?;
    ///
    ///     cx.render(rsx! {
    ///         div { "Count {}" }
    ///     })
    /// }
    /// ```
    fn throw(self, cx: &ScopeState) -> Option<Self::Out>;

    /// Returns an option that evaluates to None if there is an error, injecting the error to the nearest error boundary.
    ///
    /// If the value is `Ok`, then throw returns the value, not aborting the rendering process.
    ///
    /// The call stack is saved for this component and provided to the error boundary
    ///
    ///
    /// Note that you can also manually throw errors using the throw method on `ScopeState` directly,
    /// which is what this trait shells out to.
    ///
    ///
    /// ```rust, ignore
    /// #[component]
    /// fn App(cx: Scope, count: String) -> Element {
    ///     let id: i32 = count.parse().throw(cx)?;
    ///
    ///     cx.render(rsx! {
    ///         div { "Count {}" }
    ///     })
    /// }
    /// ```
    fn throw_with<D: Debug + 'static>(
        self,
        cx: &ScopeState,
        e: impl FnOnce() -> D,
    ) -> Option<Self::Out>;
}

/// We call clone on any errors that can be owned out of a reference
impl<'a, T, O: Debug + 'static, E: ToOwned<Owned = O>> Throw for &'a Result<T, E> {
    type Out = &'a T;

    fn throw(self, cx: &ScopeState) -> Option<Self::Out> {
        match self {
            Ok(t) => Some(t),
            Err(e) => {
                cx.throw(e.to_owned());
                None
            }
        }
    }

    fn throw_with<D: Debug + 'static>(
        self,
        cx: &ScopeState,
        err: impl FnOnce() -> D,
    ) -> Option<Self::Out> {
        match self {
            Ok(t) => Some(t),
            Err(_e) => {
                cx.throw(err());
                None
            }
        }
    }
}

/// Or just throw errors we know about
impl<T, E: Debug + 'static> Throw for Result<T, E> {
    type Out = T;

    fn throw(self, cx: &ScopeState) -> Option<T> {
        match self {
            Ok(t) => Some(t),
            Err(e) => {
                cx.throw(e);
                None
            }
        }
    }

    fn throw_with<D: Debug + 'static>(
        self,
        cx: &ScopeState,
        error: impl FnOnce() -> D,
    ) -> Option<Self::Out> {
        self.ok().or_else(|| {
            cx.throw(error());
            None
        })
    }
}

/// Or just throw errors we know about
impl<T> Throw for Option<T> {
    type Out = T;

    fn throw(self, cx: &ScopeState) -> Option<T> {
        self.or_else(|| {
            cx.throw("None error.");
            None
        })
    }

    fn throw_with<D: Debug + 'static>(
        self,
        cx: &ScopeState,
        error: impl FnOnce() -> D,
    ) -> Option<Self::Out> {
        self.or_else(|| {
            cx.throw(error());
            None
        })
    }
}