Skip to main content

murk_space/
error.rs

1//! Error types for space operations.
2
3use murk_core::Coord;
4use std::fmt;
5
6/// Errors arising from space construction or spatial queries.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum SpaceError {
9    /// A coordinate is outside the bounds of the space.
10    CoordOutOfBounds {
11        /// The offending coordinate.
12        coord: Coord,
13        /// Human-readable description of the valid range.
14        bounds: String,
15    },
16    /// A region specification is invalid for this space.
17    InvalidRegion {
18        /// What went wrong.
19        reason: String,
20    },
21    /// Attempted to construct a space with zero cells.
22    EmptySpace,
23    /// A dimension exceeds the representable coordinate range.
24    DimensionTooLarge {
25        /// Which dimension is too large (e.g. "len", "rows", "cols").
26        name: &'static str,
27        /// The value that was provided.
28        value: u32,
29        /// The maximum allowed value.
30        max: u32,
31    },
32    /// A space composition is invalid (e.g. empty component list, overflow).
33    InvalidComposition {
34        /// What went wrong.
35        reason: String,
36    },
37}
38
39impl fmt::Display for SpaceError {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            Self::CoordOutOfBounds { coord, bounds } => {
43                write!(f, "coordinate {coord:?} out of bounds: {bounds}")
44            }
45            Self::InvalidRegion { reason } => {
46                write!(f, "invalid region: {reason}")
47            }
48            Self::EmptySpace => write!(f, "space must have at least one cell"),
49            Self::DimensionTooLarge { name, value, max } => {
50                write!(f, "{name} ({value}) exceeds maximum ({max})")
51            }
52            Self::InvalidComposition { reason } => {
53                write!(f, "invalid composition: {reason}")
54            }
55        }
56    }
57}
58
59impl std::error::Error for SpaceError {}