Skip to main content

grit_lib/error/
summary.rs

1//! [`ErrorSummary`]: a snapshot of a failure carried as the cause of another error.
2
3/// Classification and text of a failure recorded inside another error.
4///
5/// Composite failures (a rollback that failed after a checkout stopped, one remote's failure
6/// among several promisor remotes) keep each inner failure as a summary instead of nesting
7/// another [`Error`](enum@super::Error): the outer variant names what happened, and the summary
8/// keeps the inner failure's stable [`kind`](Self::kind), its [`area`](Self::area), its
9/// [`message`](Self::message), and whether it was a [cancellation](Self::is_cancelled).
10///
11/// The summary is one pointer wide so composite variants stay small.
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct ErrorSummary {
14    inner: Box<SummaryInner>,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq)]
18struct SummaryInner {
19    kind: &'static str,
20    area: &'static str,
21    message: String,
22    cancelled: bool,
23}
24
25impl ErrorSummary {
26    /// The inner failure's [`Error::kind_name`](super::Error::kind_name).
27    #[must_use]
28    pub fn kind(&self) -> &'static str {
29        self.inner.kind
30    }
31
32    /// The inner failure's [`Error::area`](super::Error::area).
33    #[must_use]
34    pub fn area(&self) -> &'static str {
35        self.inner.area
36    }
37
38    /// The inner failure's display text.
39    #[must_use]
40    pub fn message(&self) -> &str {
41        &self.inner.message
42    }
43
44    /// Whether the inner failure was a cancellation
45    /// ([`Error::is_cancelled`](super::Error::is_cancelled)).
46    #[must_use]
47    pub fn is_cancelled(&self) -> bool {
48        self.inner.cancelled
49    }
50}
51
52impl From<&super::Error> for ErrorSummary {
53    fn from(err: &super::Error) -> Self {
54        Self {
55            inner: Box::new(SummaryInner {
56                kind: err.kind_name(),
57                area: err.area(),
58                message: err.to_string(),
59                cancelled: err.is_cancelled(),
60            }),
61        }
62    }
63}
64
65impl From<super::Error> for ErrorSummary {
66    fn from(err: super::Error) -> Self {
67        Self::from(&err)
68    }
69}
70
71impl std::fmt::Display for ErrorSummary {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        f.write_str(&self.inner.message)
74    }
75}
76
77impl std::error::Error for ErrorSummary {}