1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::ArenaError;

/// Error type for the [`SkipSet`](crate::SkipSet).
///
/// [`SkipSet`]: crate::SkipSet
#[derive(Debug)]
pub enum Error {
  /// Indicates that the arena is full
  Full(ArenaError),

  /// Indicates that the key is too large to be stored in the [`SkipSet`](crate::SkipSet).
  KeyTooLarge(u64),

  /// Indicates that the entry is too large to be stored in the [`SkipSet`](crate::SkipSet).
  EntryTooLarge(u64),

  /// Readonly skipmap
  Readonly,
}

impl core::fmt::Display for Error {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    match self {
      Self::Full(e) => write!(f, "{e}"),
      Self::KeyTooLarge(size) => write!(f, "key size {} is too large", size),
      Self::EntryTooLarge(size) => write!(f, "entry size {size} is too large",),
      Self::Readonly => write!(f, "skipset is read only"),
    }
  }
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl From<ArenaError> for Error {
  fn from(e: ArenaError) -> Self {
    Self::Full(e)
  }
}

#[cfg(test)]
#[test]
fn test_fmt() {
  assert_eq!(
    std::format!("{}", Error::KeyTooLarge(10)),
    "key size 10 is too large"
  );
  assert_eq!(
    std::format!("{}", Error::EntryTooLarge(10)),
    "entry size 10 is too large"
  );
  assert_eq!(
    std::format!("{}", Error::Full(ArenaError)),
    "allocation failed because arena is full",
  );
  assert_eq!(std::format!("{}", Error::Readonly), "skipset is read only");
}