MCPcopy Create free account
hub / github.com/Stranger6667/css-inline / try_from

Method try_from

bindings/javascript/src/options.rs:76–148  ·  view source on GitHub ↗
(value: Options)

Source from the content-addressed store, hash-verified

74 type Error = JsError;
75
76 fn try_from(value: Options) -> std::result::Result<Self, Self::Error> {
77 Ok(css_inline::InlineOptions {
78 inline_style_tags: value.inline_style_tags.unwrap_or(true),
79 keep_style_tags: value.keep_style_tags.unwrap_or(false),
80 keep_link_tags: value.keep_link_tags.unwrap_or(false),
81 keep_at_rules: value.keep_at_rules.unwrap_or(false),
82 minify_css: value.minify_css.unwrap_or(false),
83 base_url: parse_url(value.base_url)?,
84 load_remote_stylesheets: value.load_remote_stylesheets.unwrap_or(true),
85 extra_css: value.extra_css.map(Cow::Owned),
86 #[cfg(not(target_arch = "wasm32"))]
87 cache: {
88 if let Some(cache) = value.cache {
89 let size =
90 std::num::NonZeroUsize::new(cache.size as usize).ok_or_else(|| {
91 let reason =
92 "Cache size must be an integer greater than zero".to_string();
93 napi::Error::from_reason(reason)
94 })?;
95 Some(std::sync::Mutex::new(css_inline::StylesheetCache::new(
96 size,
97 )))
98 } else {
99 None
100 }
101 },
102 preallocate_node_capacity: if let Some(capacity) = value.preallocate_node_capacity {
103 usize::try_from(capacity).map_err(|_| {
104 let reason = "Invalid capacity".to_string();
105 #[cfg(target_arch = "wasm32")]
106 {
107 JsValue::from_str(reason.as_str())
108 }
109 #[cfg(not(target_arch = "wasm32"))]
110 {
111 napi::Error::from_reason(reason)
112 }
113 })?
114 } else {
115 32
116 },
117 resolver: {
118 #[cfg(target_arch = "wasm32")]
119 {
120 #[derive(Debug, Default)]
121 pub struct UnsupportedResolver;
122
123 impl css_inline::StylesheetResolver for UnsupportedResolver {
124 fn retrieve(&self, location: &str) -> css_inline::Result<String> {
125 let message = if location.starts_with("https")
126 || location.starts_with("http")
127 {
128 format!(
129 "Loading remote stylesheets is not supported on WASM: {location}"
130 )
131 } else {
132 format!("Loading local files is not supported on WASM: {location}")
133 };

Callers

nothing calls this directly

Calls 1

parse_urlFunction · 0.70

Tested by

no test coverage detected