forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.rs
More file actions
67 lines (61 loc) · 1.96 KB
/
error.rs
File metadata and controls
67 lines (61 loc) · 1.96 KB
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
pub enum CompileError<'i> {
ParseError(cssparser::ParseError<'i, ()>),
PrinterError,
SourceMapError(parcel_sourcemap::SourceMapError)
}
impl<'i> CompileError<'i> {
pub fn reason(&self) -> String {
match self {
CompileError::ParseError(e) => {
match &e.kind {
cssparser::ParseErrorKind::Basic(b) => {
use cssparser::BasicParseErrorKind::*;
match b {
AtRuleBodyInvalid => "Invalid at rule body".into(),
EndOfInput => "Unexpected end of input".into(),
AtRuleInvalid(name) => format!("Unknown at rule: @{}", name),
QualifiedRuleInvalid => "Invalid qualified rule".into(),
UnexpectedToken(token) => format!("Unexpected token {:?}", token)
}
},
_ => "Unknown error".into()
}
}
CompileError::PrinterError => "Printer error".into(),
_ => "Unknown error".into()
}
}
}
impl<'i> From<cssparser::ParseError<'i, ()>> for CompileError<'i> {
fn from(e: cssparser::ParseError<'i, ()>) -> CompileError<'i> {
CompileError::ParseError(e)
}
}
impl<'i> From<std::fmt::Error> for CompileError<'i> {
fn from(_: std::fmt::Error) -> CompileError<'i> {
CompileError::PrinterError
}
}
impl<'i> From<parcel_sourcemap::SourceMapError> for CompileError<'i> {
fn from(e: parcel_sourcemap::SourceMapError) -> CompileError<'i> {
CompileError::SourceMapError(e)
}
}
#[cfg(not(target_arch = "wasm32"))]
impl<'i> From<CompileError<'i>> for napi::Error {
fn from(e: CompileError) -> napi::Error {
match e {
CompileError::SourceMapError(e) => e.into(),
_ => napi::Error::new(napi::Status::GenericFailure, e.reason())
}
}
}
#[cfg(target_arch = "wasm32")]
impl<'i> From<CompileError<'i>> for wasm_bindgen::JsValue {
fn from(e: CompileError) -> wasm_bindgen::JsValue {
match e {
CompileError::SourceMapError(e) => e.into(),
_ => js_sys::Error::new(&e.reason()).into()
}
}
}