Skip to main content

stikk_model/
error.rs

1//! The single stikk error type and its seven presentation classes.
2//!
3//! Design `stikk-04` ER-01/ER-02, threat model `stikk-03` OP-03/CT-04. Two lessons from the 2026
4//! prikk audit are applied here to stikk's own error type: it implements [`std::error::Error::source`]
5//! so causes are inspectable, and it is `#[non_exhaustive]` so adding a class is not a breaking
6//! change. The load-bearing rule is [`StikkError::Refusal`] and friends **preserve prikk's message
7//! verbatim** — no layer above rewrites it (NFR-I03); a localized gloss is added beside it, never in
8//! place of it.
9
10use std::fmt;
11
12/// Convenience alias for a fallible stikk operation.
13pub type Result<T> = std::result::Result<T, StikkError>;
14
15/// Every error stikk surfaces, tagged with the class that decides how it is presented (design
16/// `stikk-04` CT-04). The class is the whole point: a refusal opens the explanation overlay, a lock
17/// conflict is a banner, a not-ready is inline guidance, and so on — one mapping, both frontends.
18///
19/// `#[non_exhaustive]`: adding a class later is not a breaking change to downstream matchers.
20#[derive(Debug)]
21#[non_exhaustive]
22pub enum StikkError {
23    /// prikk semantically refused an operation (a non-confluent merge, a TOFU key conflict, a
24    /// path/ref validation refusal). `message` is prikk's own wording, preserved verbatim.
25    Refusal {
26        /// prikk's verbatim refusal message. Never rewritten by a layer above.
27        message: String,
28    },
29    /// Another writer holds a lock, or a CAS precondition failed. Presented as "another writer is
30    /// active", never as corruption (design FR-106). prikk distinguishes a genuine lock from a
31    /// ref-CAS mismatch; both arrive here and the message carries which.
32    LockConflict {
33        /// prikk's verbatim conflict message.
34        message: String,
35    },
36    /// A signing or trust prerequisite is absent — no AUTHOR/MAINTAINER readiness, or the maintainer
37    /// key is not adopted. Presented as inline guidance toward the trust/keys surface (FR-104).
38    NotReady {
39        /// What is missing, in stikk's own words (this class is stikk-originated, not prikk's).
40        detail: String,
41    },
42    /// A verify/doctor finding — repository-content diagnostics. Routed into the verify/doctor views,
43    /// never a popup (design OP-03).
44    IntegrityFinding {
45        /// prikk's verbatim finding text.
46        message: String,
47    },
48    /// An input ceiling was (or would be) exceeded — a bundle/exchange/summary limit. Surfaced in the
49    /// pre-execution confirmation, not after failure (threat model C-D2a).
50    Limits {
51        /// prikk's verbatim limit message.
52        message: String,
53    },
54    /// An environment problem: I/O, permissions, a missing or wrong `prikk` binary, version skew, or
55    /// an unparseable prikk output shape. Carries a stikk-authored description plus any underlying
56    /// cause via [`std::error::Error::source`].
57    Environment {
58        /// What went wrong, in stikk's own words.
59        detail: String,
60        /// The underlying cause, if any (e.g. a `std::io::Error`), preserved for `source()`.
61        source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
62    },
63    /// A stikk-internal fault (a bug). The repository was not touched — only the seam writes, and only
64    /// on an explicit confirmed mutation — so a fault here can never be a repository write (design
65    /// ER-04). Session state is already flushed; the user may continue read-only.
66    Internal {
67        /// A description of the internal invariant that was violated.
68        detail: String,
69    },
70}
71
72impl StikkError {
73    /// Construct an [`StikkError::Environment`] from a description and an underlying cause.
74    #[must_use]
75    pub fn environment(
76        detail: impl Into<String>,
77        source: impl std::error::Error + Send + Sync + 'static,
78    ) -> Self {
79        Self::Environment {
80            detail: detail.into(),
81            source: Some(Box::new(source)),
82        }
83    }
84
85    /// Construct an [`StikkError::Environment`] with no underlying cause.
86    #[must_use]
87    pub fn environment_msg(detail: impl Into<String>) -> Self {
88        Self::Environment {
89            detail: detail.into(),
90            source: None,
91        }
92    }
93
94    /// The stable machine-readable class name, for the report format (design CL-07) and for tests.
95    #[must_use]
96    pub const fn class(&self) -> &'static str {
97        match self {
98            Self::Refusal { .. } => "refusal",
99            Self::LockConflict { .. } => "lock-conflict",
100            Self::NotReady { .. } => "not-ready",
101            Self::IntegrityFinding { .. } => "integrity-finding",
102            Self::Limits { .. } => "limits",
103            Self::Environment { .. } => "environment",
104            Self::Internal { .. } => "stikk-internal",
105        }
106    }
107
108    /// True when this class must never be auto-retried (design NFR-S04): a refusal, a lock conflict,
109    /// or a not-ready condition is the user's to resolve, never stikk's to retry silently.
110    #[must_use]
111    pub const fn is_user_resolved(&self) -> bool {
112        matches!(
113            self,
114            Self::Refusal { .. } | Self::LockConflict { .. } | Self::NotReady { .. }
115        )
116    }
117}
118
119impl fmt::Display for StikkError {
120    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121        match self {
122            Self::Refusal { message }
123            | Self::LockConflict { message }
124            | Self::IntegrityFinding { message }
125            | Self::Limits { message } => write!(f, "{}: {message}", self.class()),
126            Self::NotReady { detail } | Self::Internal { detail } => {
127                write!(f, "{}: {detail}", self.class())
128            }
129            Self::Environment { detail, .. } => write!(f, "environment: {detail}"),
130        }
131    }
132}
133
134impl std::error::Error for StikkError {
135    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
136        match self {
137            Self::Environment {
138                source: Some(cause),
139                ..
140            } => Some(cause.as_ref()),
141            _ => None,
142        }
143    }
144}
145
146#[cfg(test)]
147mod tests;