Skip to main content

zrx_scheduler/scheduler/action/report/
convert.rs

1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the 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//! Report conversions.
27
28use zrx_diagnostic::report::Report;
29
30use crate::scheduler::action::error::IntoError;
31use crate::scheduler::action::Error;
32use crate::scheduler::value::Value;
33
34// ----------------------------------------------------------------------------
35// Traits
36// ----------------------------------------------------------------------------
37
38/// Conversion into [`Report`]
39///
40/// This trait provides a focused implementation of the [`IntoReport`][] trait,
41/// as defined in the diagnostics crate, adding the appropriate trait bounds to
42/// the implementations, so that anything that implements [`Value`] can be used
43/// as a report value, providing a more ergonomic API.
44///
45/// For more information on why this is necessary, see [`IntoReport`][].
46///
47/// [`IntoReport`]: zrx_diagnostic::report::IntoReport
48pub trait IntoReport<T = ()> {
49    /// Converts into a report.
50    ///
51    /// # Errors
52    ///
53    /// Note that this method never returns an error by itself, as this trait is
54    /// primarily provided for wrapping values returned from infallible methods.
55    /// Consequentially, this trait isn't named `TryIntoReport`, as it's not
56    /// falling under Rust's try-semantics.
57    fn into_report(self) -> Result<Report<T>, Error>;
58}
59
60// ----------------------------------------------------------------------------
61// Blanket implementations
62// ----------------------------------------------------------------------------
63
64impl<T> IntoReport<T> for T
65where
66    T: Value,
67{
68    /// Creates a report from a value `T` and wraps it in a result.
69    #[inline]
70    fn into_report(self) -> Result<Report<T>, Error> {
71        Ok(Report::new(self))
72    }
73}
74
75impl<T> IntoReport<T> for Report<T>
76where
77    T: Value,
78{
79    /// Returns the report as is and wraps it in a result.
80    #[inline]
81    fn into_report(self) -> Result<Report<T>, Error> {
82        Ok(self)
83    }
84}
85
86impl<T, E> IntoReport<T> for Result<T, E>
87where
88    T: Value,
89    E: IntoError,
90{
91    /// Creates a report from a value `T` in a result.
92    #[inline]
93    fn into_report(self) -> Result<Report<T>, Error> {
94        self.map_err(IntoError::into_error).map(Report::new)
95    }
96}
97
98impl<T, E> IntoReport<T> for Result<Report<T>, E>
99where
100    T: Value,
101    E: IntoError,
102{
103    /// Returns the report in a result as is.
104    #[inline]
105    fn into_report(self) -> Result<Report<T>, Error> {
106        self.map_err(IntoError::into_error)
107    }
108}