perspective_viewer/utils/
mod.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13//! A catch all for project-wide macros and general-purpose functions that are
14//! not directly related to Perspective.
15//!
16//! Modules below `crate::utils` strive to be single-responsibility, but some
17//! reference other `crate::utils` modules when it helps reduce boiler-plate.
18
19mod browser;
20
21mod custom_element;
22mod datetime;
23mod debounce;
24mod hooks;
25mod number_format;
26mod pubsub;
27mod scope;
28mod tee;
29mod wasm_abi;
30mod weak_scope;
31
32#[cfg(test)]
33mod tests;
34
35pub use browser::*;
36pub use custom_element::*;
37pub use datetime::*;
38pub use debounce::*;
39pub use hooks::*;
40pub use number_format::*;
41pub use perspective_client::clone;
42pub use pubsub::*;
43pub use scope::*;
44pub use tee::*;
45pub use weak_scope::*;
46
47#[macro_export]
48macro_rules! maybe {
49    ($($exp:stmt);*) => {{
50        let x = ({
51            #[inline(always)]
52            || {
53                $($exp)*
54            }
55        })();
56        x
57    }};
58}
59
60#[macro_export]
61macro_rules! js_log_maybe {
62    ($($exp:tt)+) => {{
63        let x = ({
64            #[inline(always)]
65            || {
66                {
67                    $($exp)+
68                };
69                Ok(())
70            }
71        })();
72        x.unwrap_or_else(|e| web_sys::console::warn_1(&e))
73    }};
74}
75
76#[macro_export]
77macro_rules! max {
78    ($x:expr) => ($x);
79    ($x:expr, $($z:expr),+ $(,)?) => {{
80        let x = $x;
81        let y = max!($($z),*);
82        if x > y {
83            x
84        } else {
85            y
86        }
87    }}
88}
89
90#[macro_export]
91macro_rules! min {
92    ($x:expr) => ($x);
93    ($x:expr, $($z:expr),+ $(,)?) => {{
94        let x = $x;
95        let y = min!($($z),*);
96        if x < y {
97            x
98        } else {
99            y
100        }
101    }}
102}
103
104#[macro_export]
105macro_rules! js_log {
106    ($x:expr) => {{
107        const DEBUG_ONLY_WARNING: &str = $x;
108        web_sys::console::log_1(&wasm_bindgen::JsValue::from($x));
109    }};
110    ($x:expr $(, $y:expr)*) => {{
111        const DEBUG_ONLY_WARNING: &str = $x;
112        web_sys::console::log_1(&format!($x, $($y),*).into());
113    }};
114}