Skip to main content

docs_compiler/
error.rs

1//! Typed, exhaustive errors for the compile pipeline.
2
3/// Why a build failed. Every directive failure names its source.
4#[derive(Debug)]
5#[non_exhaustive]
6pub enum CompilerError {
7    /// An include-symbol fence failed (unknown file, unresolvable symbol,
8    /// parse error) — the inner error carries the details.
9    Symbol(docs_symbols::SymbolError),
10    /// A witness fence failed execution or its assertions did not hold.
11    Witness(docs_witness::WitnessError),
12    /// A witness fence's declared assertions did not match the run.
13    WitnessAssert(docs_witness::AssertionError),
14    /// A lean-check fence failed: the kernel rejected the file, or the
15    /// proof is not real (sorry), or the certificate went stale.
16    Lean {
17        /// The theorem named by the fence.
18        theorem: String,
19        /// What went wrong, for diagnosis.
20        detail: String,
21    },
22    /// A `d2` fence failed: the `d2` binary is missing, rejected the
23    /// diagram source, or could not be spawned.
24    Diagram {
25        /// What went wrong, for diagnosis (spawning, exit status, or
26        /// the compiler's diagnostics).
27        detail: String,
28    },
29    /// A fenced directive named a file that does not exist or could not
30    /// be read.
31    Io {
32        /// The path as named in the fence (relative to the build root).
33        path: String,
34        /// The underlying error.
35        source: std::io::Error,
36    },
37    /// Markdown rendering failed after directive resolution.
38    Render(docs_core::DocsError),
39    /// A fence attribute was missing or malformed.
40    Attribute {
41        /// The directive kind.
42        directive: &'static str,
43        /// What was wrong.
44        reason: String,
45    },
46}
47
48impl std::fmt::Display for CompilerError {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        match self {
51            Self::Symbol(e) => write!(f, "include-symbol failed: {e}"),
52            Self::Witness(e) => write!(f, "witness failed: {e}"),
53            Self::WitnessAssert(e) => write!(f, "witness assertion failed: {e}"),
54            Self::Lean { theorem, detail } => {
55                write!(f, "lean-check failed for `{theorem}`: {detail}")
56            }
57            Self::Diagram { detail } => write!(f, "d2 fence failed: {detail}"),
58            Self::Io { path, source } => write!(f, "cannot read `{path}`: {source}"),
59            Self::Attribute { directive, reason } => {
60                write!(f, "bad {directive} fence: {reason}")
61            }
62            Self::Render(e) => write!(f, "markdown render failed: {e}"),
63        }
64    }
65}
66
67impl std::error::Error for CompilerError {
68    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
69        match self {
70            Self::Symbol(e) => Some(e),
71            Self::Witness(e) => Some(e),
72            Self::WitnessAssert(e) => Some(e),
73            Self::Io { source, .. } => Some(source),
74            Self::Render(e) => Some(e),
75            _ => None,
76        }
77    }
78}