keramics_core/
macros.rs

1/* Copyright 2024-2025 Joachim Metz <joachim.metz@gmail.com>
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may
5 * obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 * License for the specific language governing permissions and limitations
11 * under the License.
12 */
13
14/// Determines the name of the current function.
15#[macro_export]
16macro_rules! error_trace_function {
17    () => {{
18        fn f() {}
19        fn type_name_of<T>(_: T) -> &'static str {
20            std::any::type_name::<T>()
21        }
22        let name = type_name_of(f);
23        name.strip_suffix("::f").unwrap()
24    }};
25}
26
27/// Creates a new [`ErrorTrace`].
28#[macro_export]
29macro_rules! error_trace_new {
30    ( $message:expr ) => {
31        $crate::ErrorTrace::new(format!("{}: {}", $crate::error_trace_function!(), $message,))
32    };
33}
34
35/// Creates a new [`ErrorTrace`] based on an existing error.
36#[macro_export]
37macro_rules! error_trace_new_with_error {
38    ( $message:expr, $error:expr ) => {
39        $crate::ErrorTrace::new(format!(
40            "{}: {} with error: {}",
41            $crate::error_trace_function!(),
42            $message,
43            $error.to_string(),
44        ))
45    };
46}
47
48/// Adds a frame to an existing [`ErrorTrace`].
49#[macro_export]
50macro_rules! error_trace_add_frame {
51    ( $error:expr, $message:expr ) => {
52        $error.add_frame(format!("{}: {}", $crate::error_trace_function!(), $message,))
53    };
54}
55
56/// Retrieves the size of a [`DataStreamReference`].
57#[macro_export]
58macro_rules! data_stream_get_size {
59    ( $data_stream:expr ) => {
60        match $data_stream.write() {
61            Ok(mut data_stream) => match data_stream.get_size() {
62                Ok(size) => size,
63                Err(mut error) => {
64                    $crate::error_trace_add_frame!(
65                        error,
66                        "Unable to determine size of data stream"
67                    );
68                    return Err(error);
69                }
70            },
71            Err(error) => {
72                return Err($crate::error_trace_new_with_error!(
73                    "Unable to obtain write lock on data stream",
74                    error
75                ));
76            }
77        };
78    };
79}
80
81/// Reads data at a specific position from a [`DataStreamReference`].
82#[macro_export]
83macro_rules! data_stream_read_at_position {
84    ( $data_stream:expr, $buf:expr, $pos:expr ) => {
85        match $data_stream.write() {
86            Ok(mut data_stream) => match data_stream.read_at_position($buf, $pos) {
87                Ok(read_count) => read_count,
88                Err(mut error) => {
89                    $crate::error_trace_add_frame!(error, "Unable to read from data stream");
90                    return Err(error);
91                }
92            },
93            Err(error) => {
94                return Err($crate::error_trace_new_with_error!(
95                    "Unable to obtain write lock on data stream",
96                    error
97                ));
98            }
99        };
100    };
101}
102
103/// Reads an exact amount of data at a specific position from a [`DataStreamReference`].
104#[macro_export]
105macro_rules! data_stream_read_exact_at_position {
106    ( $data_stream:expr, $buf:expr, $pos:expr ) => {
107        match $data_stream.write() {
108            Ok(mut data_stream) => match data_stream.read_exact_at_position($buf, $pos) {
109                Ok(offset) => offset,
110                Err(mut error) => {
111                    $crate::error_trace_add_frame!(error, "Unable to read from data stream");
112                    return Err(error);
113                }
114            },
115            Err(error) => {
116                return Err($crate::error_trace_new_with_error!(
117                    "Unable to obtain write lock on data stream",
118                    error
119                ));
120            }
121        };
122    };
123}