Skip to main content

zrx_scheduler/scheduler/step/error/
convert.rs

1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// Third-party contributions licensed under DCO
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Step error conversions.
27
28use crate::scheduler::signal::Value;
29
30use super::Error;
31
32// ----------------------------------------------------------------------------
33// Traits
34// ----------------------------------------------------------------------------
35
36/// Conversion into [`Result`].
37pub trait IntoResult<T = ()>: Sized {
38    /// Converts into a step result.
39    ///
40    /// # Errors
41    ///
42    /// In case conversion fails, an error should be returned.
43    fn into_result(self) -> Result<T, Error>;
44}
45
46// ----------------------------------------------------------------------------
47// Blanket implementations
48// ----------------------------------------------------------------------------
49
50impl<T, E> IntoResult<T> for Result<T, E>
51where
52    T: Value,
53    E: Into<anyhow::Error>,
54{
55    /// Converts any result into a step result.
56    #[inline]
57    fn into_result(self) -> Result<T, Error> {
58        self.map_err(into_error)
59    }
60}
61
62impl<T> IntoResult<T> for T
63where
64    T: Value,
65{
66    /// Converts any value into a step result.
67    #[inline]
68    fn into_result(self) -> Result<T, Error> {
69        Ok(self)
70    }
71}
72
73// ----------------------------------------------------------------------------
74// Macros
75// ----------------------------------------------------------------------------
76
77// Implements step error conversion for concrete types.
78macro_rules! impl_into_result {
79    ($($T:ty),+ $(,)?) => {
80        $(
81            impl<E> IntoResult<$T> for Result<$T, E>
82            where
83                E: Into<anyhow::Error>,
84            {
85                #[inline]
86                fn into_result(self) -> Result<$T, Error> {
87                    self.map_err(into_error)
88                }
89            }
90
91            impl IntoResult<$T> for $T {
92                #[inline]
93                fn into_result(self) -> Result<$T, Error> {
94                    Ok(self)
95                }
96            }
97        )+
98    };
99}
100
101// ----------------------------------------------------------------------------
102
103impl_into_result!(bool);
104
105// ----------------------------------------------------------------------------
106// Functions
107// ----------------------------------------------------------------------------
108
109/// Converts any error into a step error.
110///
111/// This implementation attempts to downcast the error into one of the known
112/// variants, and falls back to the [`Error::Other`] variant which will convert
113/// into [`anyhow::Error`] if the error to allow for flexible integration with
114/// external error types.
115fn into_error<E>(err: E) -> Error
116where
117    E: Into<anyhow::Error>,
118{
119    let mut err = err.into();
120
121    // Implements downcast attempt of error
122    macro_rules! downcast {
123        ($variant:path) => {
124            err = match err.downcast() {
125                Ok(err) => return $variant(err),
126                Err(err) => err,
127            };
128        };
129    }
130
131    // Downcast known error variants
132    downcast!(Error::Session);
133    downcast!(Error::Key);
134
135    // Handle unknown error variants
136    Error::Other(err)
137}