Skip to main content

molframe_core/limits/
storage.rs

1//! Typed failures at compact-storage representation boundaries.
2
3use std::fmt;
4
5/// A collection cannot represent another row without losing information.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub struct CapacityError {
8    resource: &'static str,
9}
10
11impl CapacityError {
12    /// Creates a capacity error for a named collection.
13    #[must_use]
14    pub const fn new(resource: &'static str) -> Self {
15        Self { resource }
16    }
17
18    /// The affected collection.
19    #[must_use]
20    pub const fn resource(self) -> &'static str {
21        self.resource
22    }
23}
24
25impl fmt::Display for CapacityError {
26    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
27        write!(
28            formatter,
29            "{} exceeds its supported capacity",
30            self.resource
31        )
32    }
33}
34
35impl std::error::Error for CapacityError {}
36
37/// A hierarchy table rejected an append operation.
38#[derive(Clone, Copy, Debug, PartialEq, Eq)]
39pub enum TableError {
40    /// The compact table representation is full.
41    Capacity(CapacityError),
42    /// The supplied child range ends before it starts.
43    ReversedRange {
44        /// The affected hierarchy table.
45        table: &'static str,
46    },
47    /// The requested table position does not exist.
48    MissingIndex {
49        /// The affected hierarchy table.
50        table: &'static str,
51    },
52}
53
54impl fmt::Display for TableError {
55    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
56        match self {
57            Self::Capacity(error) => error.fmt(formatter),
58            Self::ReversedRange { table } => write!(formatter, "{table} child range is reversed"),
59            Self::MissingIndex { table } => write!(formatter, "{table} index does not exist"),
60        }
61    }
62}
63
64impl std::error::Error for TableError {}