x_map/
error.rs

1//! Module containing code for various errors that may need to be handled.
2
3use std::error::Error;
4use std::fmt::{Debug, Display, Formatter, Write};
5
6pub enum MapErrorKind {
7    AllocationError,
8    AccessError
9}
10
11/// A struct handling error reporting for the `CIndexMap` type.
12///
13/// This error contains the kind of error that the map ran into, and the message to display
14/// when displaying the error.
15pub struct CIndexMapError {
16    /// A static string containing the message associated with the error.
17    message: &'static str,
18
19    /// A `MapErrorKind`, containing the type of error that the map encountered.
20    kind: MapErrorKind
21}
22
23impl CIndexMapError {
24    /// Constructs a new CIndexMapError.
25    ///
26    /// This is only used within `x-map`, and cannot be called externally.
27    pub(in crate) fn new(
28        kind: MapErrorKind,
29        message: &'static str
30    ) -> CIndexMapError {
31        CIndexMapError {
32            kind,
33            message
34        }
35    }
36}
37
38impl Debug for CIndexMapError {
39    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
40        f.write_str(self.message)
41    }
42}
43
44impl Display for CIndexMapError {
45    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46        f.write_str(self.message)
47    }
48}
49
50impl Error for CIndexMapError {}