Skip to main content

m2_syn/
recovery.rs

1//! Values produced from incomplete source together with recoverable diagnostics.
2
3/// A successfully produced value accompanied by non-fatal diagnostics.
4///
5/// Recovery is explicit rather than hidden in ordinary `Result` values: strict
6/// entry points may reject the same source, while real-time callers can retain
7/// the usable syntax in [`Self::value`] and present [`Self::diagnostics`].
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct Recovered<T, D> {
10    /// The value retained from the incomplete input.
11    pub value: T,
12    /// Non-fatal problems encountered while producing the value.
13    pub diagnostics: Vec<D>,
14}
15
16impl<T, D> Recovered<T, D> {
17    /// Creates a recovered value and its diagnostics.
18    pub fn new(value: T, diagnostics: Vec<D>) -> Self {
19        Self { value, diagnostics }
20    }
21
22    /// Separates the recovered value from its diagnostics.
23    pub fn into_parts(self) -> (T, Vec<D>) {
24        (self.value, self.diagnostics)
25    }
26
27    /// Transforms the recovered value without changing its diagnostics.
28    pub fn map<U>(self, map: impl FnOnce(T) -> U) -> Recovered<U, D> {
29        Recovered::new(map(self.value), self.diagnostics)
30    }
31}