Skip to main content

open_timeline_gui/
macros.rs

1// SPDX-License-Identifier: GPL-3.0-or-later
2
3//!
4//! All macros
5//!
6
7// TODO: I used (unbounded, ...) on a bounded channel and got no errors or warnings etc (catch this)
8/// Run a code block that requires a database connection in a context in which
9/// one is made available.  The transaction is not committed and thus this
10/// should only be used in contexts where that is desired (i.e. read-only).
11#[macro_export]
12macro_rules! spawn_transaction_no_commit_send_result {
13    ($shared_config:ident, bounded, $tx:ident, $fetch_fn:expr) => {
14        tokio::spawn(async move {
15            // Get database path
16            let db_pool = $shared_config.read().await.db_pool.clone();
17
18            // TODO
19            // If testing, vary the seconds
20            // tokio::time::sleep(std::time::Duration::from_secs(0)).await;
21
22            let result = async {
23                let mut transaction = db_pool.begin().await?;
24                $fetch_fn(&mut transaction).await
25            }
26            .await;
27            let _ = $tx.send(result).await;
28        });
29    };
30
31    ($shared_config:ident, unbounded, $tx:ident, $fetch_fn:expr) => {
32        tokio::spawn(async move {
33            // Get database path
34            let db_pool = $shared_config.read().await.db_pool.clone();
35
36            // If testing
37            tokio::time::sleep(std::time::Duration::from_secs(0)).await;
38
39            let result = async {
40                let mut transaction = db_pool.begin().await?;
41                $fetch_fn(&mut transaction).await
42            }
43            .await;
44            let _ = $tx.send(result);
45        });
46    };
47}
48
49/// Helper macro that implements [`open_timeline_gui_core::ValidAsynchronous`] for some type
50/// for which it should never be called (all methods panic)
51#[macro_export]
52macro_rules! impl_valid_asynchronous_macro_never_called {
53    ($type:ty) => {
54        impl open_timeline_gui_core::ValidAsynchronous for $type {
55            type Error = open_timeline_crud::CrudError;
56
57            fn is_valid_asynchronous(&self) -> Option<Result<(), Self::Error>> {
58                // Do nothing.  Components update their validity themselves.
59                panic!()
60            }
61
62            fn check_for_asynchronous_validity_response(&mut self) {
63                // Do nothing.  Components update their validity themselves.
64                panic!()
65            }
66
67            fn trigger_asynchronous_validity_update(&mut self) {
68                // Do nothing.  Components update their validity themselves.
69                panic!()
70            }
71        }
72    };
73}
74
75/// Helper macro that implements [`open_timeline_gui_core::ValidSynchronous`] for some type
76/// for which it should never be called (all methods panic)
77#[macro_export]
78macro_rules! impl_valid_synchronous_macro_never_called {
79    ($type:ty) => {
80        impl open_timeline_gui_core::ValidSynchronous for $type {
81            fn is_valid_synchronous(&self) -> bool {
82                // Do nothing.  Components update their validity themselves.
83                panic!()
84            }
85
86            fn update_validity_synchronous(&mut self) {
87                // Do nothing.  Components update their validity themselves.
88                panic!()
89            }
90
91            fn validity_synchronous(&self) -> open_timeline_gui_core::ValiditySynchronous {
92                // Do nothing.  Components update their validity themselves.
93                panic!()
94            }
95        }
96    };
97}
98
99/// Helper macro that returns the [`open_timeline_gui_core::ValidityAsynchronous`] of some
100/// iterable
101#[macro_export]
102macro_rules! impl_is_valid_method_for_iterable {
103    ($iterable:expr) => {{
104        for validity in $iterable {
105            match validity {
106                open_timeline_gui_core::ValidityAsynchronous::Invalid(error) => {
107                    return open_timeline_gui_core::ValidityAsynchronous::Invalid(error);
108                }
109                open_timeline_gui_core::ValidityAsynchronous::Waiting => {
110                    return open_timeline_gui_core::ValidityAsynchronous::Waiting;
111                }
112                open_timeline_gui_core::ValidityAsynchronous::Valid => continue,
113            }
114        }
115        open_timeline_gui_core::ValidityAsynchronous::Valid
116    }};
117}