flams_web_utils/lib.rs
1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![recursion_limit = "256"]
3
4pub mod components;
5pub mod mathml;
6
7
8#[cfg(feature = "ssr")]
9/// #### Errors
10pub async fn blocking_server_fn<T: Send + 'static>(
11 f: impl FnOnce() -> Result<T, String> + Send + 'static,
12) -> Result<T, leptos::prelude::ServerFnError<String>> {
13 tokio::task::spawn_blocking(f)
14 .await
15 .map_err(|e| e.to_string())?
16 .map_err(Into::into)
17}
18
19/*
20pub fn do_css(css: Css) {
21 match css {
22 CSS::Inline(s) => {
23 let id = hashstr("id_", &s);
24 #[cfg(not(target_family = "wasm"))]
25 let s = String::from(s);
26 do_inject_css(id.into(), s.into());
27 }
28 CSS::Class { name, css } => {
29 #[cfg(not(target_family = "wasm"))]
30 let name = String::from(name);
31 #[cfg(not(target_family = "wasm"))]
32 let css = String::from(css);
33 do_inject_css(name.into(), css.into());
34 }
35 CSS::Link(s) => {
36 let id = hashstr("id_", &s);
37 #[cfg(feature = "ssr")]
38 {
39 use leptos::prelude::expect_context;
40 use leptos_meta::Stylesheet;
41 let ids = expect_context::<CssIds>();
42 let mut ids = ids.0.lock();
43 if !ids.0.contains(&std::borrow::Cow::Borrowed(&id)) {
44 ids.insert(id.clone().into());
45 let _ = leptos::view! {
46 <Stylesheet id=id href=s.to_string()/>
47 };
48 }
49 drop(ids);
50 }
51 #[cfg(all(any(feature = "hydrate", feature = "csr"), not(feature = "ssr")))]
52 {
53 use leptos::prelude::document;
54 let Some(head) = document().head() else {
55 leptos::logging::log!("ERROR: head does not exist");
56 return;
57 };
58 match head.query_selector(&format!("link#{id}")) {
59 Ok(Some(_)) => return,
60 Err(e) => {
61 leptos::logging::log!("ERROR: query link element error: {e:?}");
62 return;
63 }
64 Ok(None) => (),
65 };
66 let Ok(style) = document().create_element("link") else {
67 leptos::logging::log!("ERROR: error creating style element");
68 return;
69 };
70 _ = style.set_attribute("id", &id);
71 _ = style.set_attribute("rel", "stylesheet");
72 _ = style.set_attribute("href", &s);
73 _ = head.prepend_with_node_1(&style);
74 }
75 }
76 }
77}
78
79#[inline]
80pub fn inject_css(id: &'static str, content: &'static str) {
81 do_inject_css(Cow::Borrowed(id), Cow::Borrowed(content));
82}
83 */
84
85#[macro_export]
86macro_rules! console_log {
87 () => {};
88 ($arg:expr) => {
89 ::web_sys::console::log_1(&::web_sys::js_sys::JsValue::from($l))
90 };
91 ($arg1:expr,$arg2:expr) => {
92 ::web_sys::console::log_2(
93 &::web_sys::js_sys::JsValue::from($l),
94 &::web_sys::js_sys::JsValue::from($l),
95 )
96 };
97}
98
99//#[cfg(any(feature = "csr", feature = "ssr"))]
100/// # Errors
101pub fn try_catch<R>(run: impl FnOnce() -> R) -> Result<R, leptos::wasm_bindgen::JsError> {
102 std::panic::catch_unwind(std::panic::AssertUnwindSafe(run)).map_err(|e| {
103 if let Some(s) = e.downcast_ref::<&str>() {
104 return leptos::wasm_bindgen::JsError::new(*s);
105 }
106 if let Some(s) = e.downcast_ref::<String>() {
107 return leptos::wasm_bindgen::JsError::new(s);
108 }
109 leptos::wasm_bindgen::JsError::new("Box<dyn Error>")
110 })
111}
112
113#[cfg(feature = "ssr")]
114pub use http;
115#[cfg(feature = "ssr")]
116pub use leptos_axum;
117
118#[cfg(feature = "ssr")]
119#[macro_export]
120macro_rules! not_found{
121 () => {
122 let response = expect_context::<$crate::leptos_axum::ResponseOptions>();
123 response.set_status($crate::http::StatusCode::NOT_FOUND);
124 };
125 (! $($e:tt)*) => { {
126 let response = expect_context::<$crate::leptos_axum::ResponseOptions>();
127 response.set_status($crate::http::StatusCode::NOT_FOUND);
128 format!($($e)*).into()
129 }};
130 ($($e:tt)*) => { return Err(not_found!(! $($e)*))};
131}