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