Skip to main content

zerodds_ast_core/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 ZeroDDS Contributors
3//! Error type for the AST core infrastructure. Spec §10.
4
5use core::fmt;
6
7/// Failure of a core infrastructure operation.
8///
9/// The public API returns these; it never panics. Spec §10.
10#[derive(Debug, Clone, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum AstCoreError {
13    /// A handle was used after its node was removed. The slot may since
14    /// have been reused by a different node; reading through the stale
15    /// handle would have addressed that unrelated node.
16    StaleHandle {
17        /// Arena slot the handle referred to.
18        slot: u32,
19        /// Generation the handle carried.
20        expected: u32,
21        /// Generation the slot carries now.
22        found: u32,
23    },
24    /// A handle referred to a slot the arena never allocated.
25    UnknownSlot {
26        /// Slot index out of range.
27        slot: u32,
28    },
29    /// The arena exhausted its slot or generation space, or an identity
30    /// allocator exhausted its counter.
31    Exhausted,
32    /// A snapshot could not be decoded. Spec §9.
33    MalformedSnapshot {
34        /// Byte offset at which decoding failed.
35        offset: usize,
36        /// What the decoder expected there.
37        expected: &'static str,
38    },
39    /// A commit was rejected by validation and rolled back. Spec §5.1.
40    CommitRejected {
41        /// Why the commit was rejected.
42        reason: &'static str,
43    },
44}
45
46impl fmt::Display for AstCoreError {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        match self {
49            Self::StaleHandle {
50                slot,
51                expected,
52                found,
53            } => write!(
54                f,
55                "stale handle: slot {slot} carried generation {expected}, arena is at {found}"
56            ),
57            Self::UnknownSlot { slot } => write!(f, "unknown arena slot {slot}"),
58            Self::Exhausted => write!(f, "arena or identity space exhausted"),
59            Self::MalformedSnapshot { offset, expected } => {
60                write!(
61                    f,
62                    "malformed snapshot at byte {offset}: expected {expected}"
63                )
64            }
65            Self::CommitRejected { reason } => write!(f, "commit rejected: {reason}"),
66        }
67    }
68}
69
70impl core::error::Error for AstCoreError {}