glass_easel_stylesheet_compiler/
js_bindings.rs1#![cfg(feature = "js_bindings")]
4
5use serde::{Deserialize, Serialize};
6use wasm_bindgen::prelude::*;
7
8use super::*;
9use crate::error::{ParseError, ParseErrorLevel};
10
11#[derive(Serialize, Deserialize)]
12#[serde(rename_all = "camelCase")]
13pub struct StyleSheetParseError {
14 is_error: bool,
15 level: error::ParseErrorLevel,
16 code: u32,
17 message: String,
18 path: String,
19 start_line: u32,
20 start_column: u32,
21 end_line: u32,
22 end_column: u32,
23}
24
25impl From<ParseError> for StyleSheetParseError {
26 fn from(value: ParseError) -> Self {
27 Self {
28 is_error: value.kind.level() >= ParseErrorLevel::Error,
29 level: value.kind.level(),
30 code: value.code() as u32,
31 message: value.kind.to_string(),
32 path: value.path.to_string(),
33 start_line: value.location.start.line,
34 start_column: value.location.start.utf16_col,
35 end_line: value.location.end.line,
36 end_column: value.location.end.utf16_col,
37 }
38 }
39}
40
41#[wasm_bindgen]
42pub struct StyleSheetTransformer {
43 warnings: Vec<ParseError>,
44 normal_content: String,
45 normal_source_map: String,
46 low_priority_content: String,
47 low_priority_source_map: String,
48}
49
50#[wasm_bindgen]
51impl StyleSheetTransformer {
52 #[wasm_bindgen(constructor)]
53 pub fn new(
54 name: &str,
55 s: &str,
56 class_prefix: Option<String>,
57 rpx_ratio: f32,
58 convert_host: bool,
59 ) -> Self {
60 let mut sst = crate::StyleSheetTransformer::from_css(
61 name,
62 s,
63 StyleSheetOptions {
64 class_prefix,
65 rpx_ratio,
66 convert_host,
67 ..Default::default()
68 },
69 );
70 let warnings = sst.take_warnings();
71 let (normal, low_priority) = sst.output_and_low_priority_output();
72
73 let mut normal_content = String::new();
74 normal.write_str(&mut normal_content).unwrap();
75 let mut normal_source_map = Vec::new();
76 normal.write_source_map(&mut normal_source_map).unwrap();
77
78 let mut low_priority_content = String::new();
79 low_priority.write_str(&mut low_priority_content).unwrap();
80 let mut low_priority_source_map = Vec::new();
81 low_priority
82 .write_source_map(&mut low_priority_source_map)
83 .unwrap();
84
85 Self {
86 warnings,
87 normal_content,
88 normal_source_map: String::from_utf8(normal_source_map).unwrap(),
89 low_priority_content,
90 low_priority_source_map: String::from_utf8(low_priority_source_map).unwrap(),
91 }
92 }
93
94 #[wasm_bindgen(js_name = extractWarnings)]
95 pub fn extrace_warnings(&mut self) -> JsValue {
96 let ret: Vec<_> = self
97 .warnings
98 .drain(..)
99 .map(|x| StyleSheetParseError::from(x))
100 .collect();
101 serde_wasm_bindgen::to_value(&ret).unwrap()
102 }
103
104 #[wasm_bindgen(js_name = getContent)]
105 pub fn get_content(&self) -> String {
106 self.normal_content.clone()
107 }
108
109 #[wasm_bindgen(js_name = getSourceMap)]
110 pub fn get_source_map(&self) -> String {
111 self.normal_source_map.clone()
112 }
113
114 #[wasm_bindgen(js_name = getLowPriorityContent)]
115 pub fn get_low_priority_content(&self) -> String {
116 self.low_priority_content.clone()
117 }
118
119 #[wasm_bindgen(js_name = getLowPrioritySourceMap)]
120 pub fn get_low_priority_source_map(&self) -> String {
121 self.low_priority_source_map.clone()
122 }
123}
124
125#[wasm_bindgen]
126pub fn enable_console_log() {
127 console_log::init_with_level(log::Level::Debug).unwrap();
128}