1#![cfg_attr(not(feature = "std"), no_std)]
33#![warn(missing_docs)]
34
35#[macro_use]
36extern crate alloc;
37#[macro_use]
38extern crate log;
39
40#[allow(unused_imports)]
41use alloc::{boxed::Box, vec::Vec};
42
43pub use fixed;
44pub use num_traits;
45#[cfg(feature = "wasm-entrance")]
46use wasm_bindgen::prelude::*;
47
48#[cfg(debug_assertions)]
49mod check_trait;
50mod group;
51mod path;
52pub use group::{StyleSheetGroup, StyleSheetImportIndex, StyleSheetResource, TEMP_SHEET_INDEX};
53pub mod sheet;
54pub use sheet::{LinkedStyleSheet, StyleSheet};
55pub mod property;
56pub mod query;
57mod resolve_font_size;
58pub mod typing;
59mod typing_stringify;
60pub use query::{EnvValues, MediaQueryStatus, StyleQuery};
61pub mod ffi;
63pub mod length_num;
64pub mod parser;
65
66#[cfg(debug_assertions)]
67use check_trait::CompatibilityCheck;
68
69#[cfg(all(target_arch = "wasm32", feature = "nodejs-package"))]
70fn init_logger() {
71 use std::sync::Once;
72 static INIT: Once = Once::new();
73 INIT.call_once(|| {
74 console_log::init_with_level(log::Level::Debug).unwrap();
75 });
76}
77
78#[doc(hidden)]
79#[cfg(all(target_arch = "wasm32", feature = "nodejs-package"))]
80#[wasm_bindgen(start)]
81pub fn wasm_main() {
82 init_logger();
83 console_error_panic_hook::set_once();
84}
85
86#[doc(hidden)]
87#[cfg(all(feature = "serialize", feature = "serialize_json"))]
88#[cfg_attr(
89 feature = "wasm-entrance",
90 wasm_bindgen(js_name = "compileStyleSheetToJson")
91)]
92pub fn compile_style_sheet_to_json(filename: &str, style_text: &str) -> String {
93 let (style_sheet, warnings) = parser::parse_style_sheet(filename, style_text);
94 for w in warnings {
95 warn!(
96 "{} (at {:?}, from line {:?} column {:?} to line {:?} column {:?})",
97 w.message.as_str(),
98 filename,
99 w.start_line,
100 w.start_col,
101 w.end_line,
102 w.end_col,
103 );
104 }
105 style_sheet.serialize_json()
106}
107
108#[cfg(feature = "serialize")]
110#[cfg_attr(
111 feature = "wasm-entrance",
112 wasm_bindgen(js_name = "compileStyleSheetToBincode")
113)]
114pub fn compile_style_sheet_to_bincode(filename: &str, style_text: &str) -> Vec<u8> {
115 let (style_sheet, warnings) = parser::parse_style_sheet(filename, style_text);
116 for w in warnings {
117 warn!(
118 "{} (at {:?}, from line {:?} column {:?} to line {:?} column {:?})",
119 w.message.as_str(),
120 filename,
121 w.start_line,
122 w.start_col,
123 w.end_line,
124 w.end_col,
125 );
126 }
127 style_sheet.serialize_bincode()
128}
129
130#[cfg(feature = "deserialize")]
132#[cfg_attr(
133 feature = "wasm-entrance",
134 wasm_bindgen(js_name = "styleSheetFromBincode")
135)]
136pub fn style_sheet_from_bincode(bincode: Vec<u8>) -> StyleSheetGroup {
137 let ptr = Box::into_raw(bincode.into_boxed_slice());
138 let mut ssg = StyleSheetGroup::new();
139 let mut resource = StyleSheetResource::new();
140 unsafe {
141 resource.add_bincode_zero_copy("", ptr, move || drop(Box::from_raw(ptr)));
142 }
143 ssg.append_from_resource(&resource, "", None);
144 ssg
145}