Skip to main content

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;
20mod custom_element;
21mod datetime;
22mod debounce;
23mod hooks;
24mod number_format;
25mod pubsub;
26mod weak_scope;
27
28#[cfg(test)]
29mod tests;
30
31pub use browser::*;
32pub use custom_element::*;
33pub use datetime::*;
34pub use debounce::*;
35pub use hooks::*;
36pub use number_format::*;
37pub use perspective_client::clone;
38pub use pubsub::*;
39pub use weak_scope::*;
40
41/// An implementaiton of `try_blocks` feature in a non-nightly macro.
42#[macro_export]
43macro_rules! maybe {
44    ($($exp:stmt);*) => {{
45        let x = ({
46            #[inline(always)]
47            || {
48                $($exp)*
49            }
50        })();
51        x
52    }};
53}
54
55/// As `maybe!`, but returns `()` and just logs errors.
56#[macro_export]
57macro_rules! maybe_log {
58    ($($exp:tt)+) => {{
59        let x = ({
60            #[inline(always)]
61            || {
62                {
63                    $($exp)+
64                };
65                Ok(())
66            }
67        })();
68        x.unwrap_or_else(|e| web_sys::console::warn_1(&e))
69    }};
70}
71
72#[macro_export]
73macro_rules! maybe_log_or_default {
74    ($($exp:tt)+) => {{
75        let x = ({
76            #[inline(always)]
77            || {
78                $($exp)+
79            }
80        })();
81        x.unwrap_or_else(|e| {
82            web_sys::console::warn_1(&e);
83            Default::default()
84        })
85    }};
86}
87
88#[macro_export]
89macro_rules! maybe_or_default {
90    ($($exp:tt)+) => {{
91        let x = ({
92            #[inline(always)]
93            || {
94                $($exp)+
95            }
96        })();
97        x.unwrap_or_else(|| {
98            web_sys::console::warn_1("Unwrap on Noner");
99            Default::default()
100        })
101    }};
102}