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
use std::{
error,
ffi::NulError,
fmt::{self, Debug, Display},
};
pub trait CustomError: Display + Debug + Send + Sync + 'static {}
impl<T: Display + Debug + Send + Sync + 'static> CustomError for T {}
#[derive(Debug)]
pub enum Error {
UninitializedField(&'static str),
Initialization,
NulByte(NulError),
JsEvaluation,
CssInjection,
Dispatch,
Custom(Box<dyn CustomError>),
}
impl Error {
pub fn custom<E: CustomError>(error: E) -> Error {
Error::Custom(Box::new(error))
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::NulByte(ref cause) => Some(cause),
_ => None,
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::UninitializedField(field) => {
write!(f, "Required field uninitialized: {}.", field)
}
Error::Initialization => write!(f, "Webview failed to initialize."),
Error::NulByte(cause) => write!(f, "{}", cause),
Error::JsEvaluation => write!(f, "Failed to evaluate JavaScript."),
Error::CssInjection => write!(f, "Failed to inject CSS."),
Error::Dispatch => write!(
f,
"Closure could not be dispatched. WebView was likely dropped."
),
Error::Custom(e) => write!(f, "Error: {}", e),
}
}
}
pub type WVResult<T = ()> = Result<T, Error>;
impl From<NulError> for Error {
fn from(e: NulError) -> Error {
Error::NulByte(e)
}
}