floating_ui/
error.rs

1use std::result::Result as StdResult;
2use thiserror::Error as ThisError;
3use wasm_bindgen::JsValue;
4
5#[derive(ThisError, Debug, Clone)]
6pub enum Error {
7    #[error("Failed to parse `{0}` for {1}")]
8    InvalidValue(String, &'static str),
9
10    #[error("Expected `{0}` to be a `{0}::{1}`")]
11    InvalidEnumConversion(&'static str, &'static str),
12
13    #[error("Expected `{0}` to be a `{1}`")]
14    InvalidJsValue(String, &'static str),
15
16    #[error("Failed to get key `{0}`. {1} is not an object.")]
17    ReflectGet(String, String),
18
19    #[error("Failed to set key `{0}`. {1}")]
20    ReflectSet(String, String),
21
22    #[error("An unknown error occurred")]
23    Unknown,
24}
25
26impl Error {
27    pub fn map_reflect_get(key: String) -> impl Fn(JsValue) -> Error {
28        move |js_error| {
29            Error::ReflectSet(
30                key.clone(),
31                js_error.as_string().expect("TypeError expected"),
32            )
33        }
34    }
35
36    pub fn map_reflect_set(key: String) -> impl Fn(JsValue) -> Error {
37        move |js_error| {
38            Error::ReflectSet(
39                key.clone(),
40                format!(
41                    "`{}` is not an object.",
42                    js_error.as_string().expect("TypeError expected")
43                ),
44            )
45        }
46    }
47}
48
49pub type Result<T> = StdResult<T, Error>;