Skip to main content

zrx_scheduler/scheduler/value/
ext.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//! Value extensions.
27
28use zrx_diagnostic::report::Report;
29use zrx_diagnostic::IntoDiagnostic;
30
31use super::Value;
32
33// ----------------------------------------------------------------------------
34// Traits
35// ----------------------------------------------------------------------------
36
37/// Extension of [`Value`].
38///
39/// This trait provides methods to attach diagnostics to a value, converting
40/// it to a [`Report`], which allows for a more ergonomic API.
41pub trait ValueExt: Sized {
42    /// Adds diagnostics to the value.
43    ///
44    /// This method attaches the given diagnostics to the value, converting it
45    /// into a [`Report`]. Anything that implements [`IntoDiagnostic`] can be
46    /// provided, so the recommended way is to implement [`IntoDiagnostic`]
47    /// for custom types, allowing for ergonomic conversions.
48    ///
49    /// # Errors
50    ///
51    /// Note that this method never returns an error by itself, as this trait is
52    /// primarily provided to wrap values in- or outside of results in reports.
53    /// Consequentially, this trait isn't named `TryValueExt`, as it's not
54    /// falling under Rust's try-semantics.
55    ///
56    /// # Examples
57    ///
58    /// ```
59    /// use zrx_diagnostic::hint;
60    /// use zrx_scheduler::ValueExt;
61    ///
62    /// // Create report
63    /// let report = 42.with_diagnostics([
64    ///     hint!("Insufficient data for meaningful answer"),
65    /// ]);
66    /// ```
67    #[inline]
68    fn with_diagnostics<D>(self, diagnostics: D) -> Report<Self>
69    where
70        D: IntoIterator,
71        D::Item: IntoDiagnostic,
72    {
73        Report::new(self).with(diagnostics)
74    }
75}
76
77// ----------------------------------------------------------------------------
78// Blanket implementations
79// ----------------------------------------------------------------------------
80
81impl<T> ValueExt for T where T: Value {}