File size: 1,389 Bytes
bee6636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::cell::BorrowMutError;

use html::RewriterError as HtmlRewriterError;
use js::RewriterError as JsRewriterError;
use js_sys::Error;
use thiserror::Error;
use wasm_bindgen::{JsError, JsValue};

#[derive(Debug, Error)]
pub enum RewriterError {
	#[error("JS: {0}")]
	Js(String),
	#[error("JS Rewriter: {0}")]
	JsRewriter(#[from] JsRewriterError),
	#[error("HTML Rewriter: {0}")]
	HtmlRewriter(#[from] HtmlRewriterError),

	#[error("str fromutf8 error: {0}")]
	Str(#[from] std::str::Utf8Error),
	#[error("reflect set failed: {0}")]
	ReflectSetFail(String),
	#[error("Rewriter was already rewriting")]
	AlreadyRewriting(#[from] BorrowMutError),

	#[error("{0} was not {1}")]
	Not(&'static str, &'static str),
}

impl From<JsValue> for RewriterError {
	fn from(value: JsValue) -> Self {
		Self::Js(Error::from(value).to_string().into())
	}
}

impl From<RewriterError> for JsValue {
	fn from(value: RewriterError) -> Self {
		JsError::from(value).into()
	}
}

impl RewriterError {
	pub fn not_str(x: &'static str) -> Self {
		Self::Not(x, "string")
	}

	pub fn not_arr(x: &'static str) -> Self {
		Self::Not(x, "array")
	}

	pub fn not_fn(x: &'static str) -> Self {
		Self::Not(x, "function")
	}

	pub fn not_bool(x: &'static str) -> Self {
		Self::Not(x, "bool")
	}
}

pub type Result<T> = std::result::Result<T, RewriterError>;