Skip to main content

adam_web_ui/graph/
failure.rs

1//! Preserves graph error classification at the application boundary.
2//!
3//! Match `GraphFailure::Runtime` before choosing a shutdown policy.
4use crate::{Renderer, RuntimeFailure};
5
6/// Distinguishes malformed requests, channel errors, and fatal model propagation.
7///
8/// # Examples
9/// ```
10/// use adam_web_ui::graph::GraphFailure;
11/// let error = GraphFailure::InvalidInput("invalid viewport".into());
12/// assert!(error.to_string().contains("viewport"));
13/// ```
14#[derive(Debug)]
15pub enum GraphFailure {
16    /// An input rejected before mutation or an invalid computed view.
17    InvalidInput(String),
18    /// A failed or malformed browser channel.
19    Transport(String),
20    /// A failed mutating operation with independently owned diagnostics.
21    Runtime(Box<RuntimeFailure>),
22    /// A previous mutating error permanently discarded the candidate.
23    Unavailable,
24}
25
26impl GraphFailure {
27    /// Returns whether the channel must stop rather than accept another request.
28    pub(crate) fn stops_channel(&self) -> bool {
29        matches!(self, Self::Runtime(_) | Self::Unavailable)
30    }
31}
32
33impl std::fmt::Display for GraphFailure {
34    /// Renders independently owned diagnostics without consulting a failed sheet.
35    ///
36    /// - Complexity: O(rendered diagnostic bytes).
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        match self {
39            Self::InvalidInput(message) | Self::Transport(message) => f.write_str(message),
40            Self::Runtime(failure) => {
41                let source = failure.diagnostics.anonymous_source();
42                f.write_str(&crate::labels::format_adam_error(
43                    &failure.error,
44                    &failure.diagnostics,
45                    source.map_or("", adam_lang::SourceId::text),
46                    &failure.file_name,
47                    &Renderer::plain(),
48                ))
49            }
50            Self::Unavailable => f.write_str("graph model unavailable after a mutating failure"),
51        }
52    }
53}
54
55impl std::error::Error for GraphFailure {}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    /// Only discarded candidates stop the channel; malformed external data is recoverable.
62    #[test]
63    fn terminal_classification_preserves_runtime_cause() {
64        assert!(!GraphFailure::InvalidInput("bad bounds".into()).stops_channel());
65        assert!(!GraphFailure::Transport("bad wire".into()).stops_channel());
66        assert!(GraphFailure::Unavailable.stops_channel());
67        let error = crate::build_sheet(
68            "sheet s { cell divisor: i32 = 0; out result: i32 := 10 / divisor; }",
69            "graph-failure.adm2",
70        )
71        .err()
72        .unwrap();
73        let crate::BuildError::Runtime(failure) = error else {
74            panic!("runtime fixture");
75        };
76        assert!(GraphFailure::Runtime(failure).stops_channel());
77    }
78}