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
58
59
60
pub type Error = Box<dyn std::error::Error+Send+Sync>;
use std::backtrace::Backtrace;
use crate::TreeId;

#[derive(Debug)]
pub struct EyrosError {
  kind: EyrosErrorKind,
  backtrace: Backtrace,
}

#[derive(Debug)]
pub enum EyrosErrorKind {
  MetaBitfieldInsufficientBytes {},
  ScalarInBounds {},
  IntervalSides { dimension: usize, min: String, max: String },
  TreeRemoved { id: TreeId },
  TreeEmpty { id: TreeId, file: String },
  RemoveIdsMissing { ids: Vec<String> }
}

impl EyrosErrorKind {
  pub fn raise<T>(self) -> Result<T,Error> {
    Err(Box::new(EyrosError {
      kind: self,
      backtrace: Backtrace::capture(),
    }))
  }
}

impl std::error::Error for EyrosError {
  fn backtrace<'a>(&'a self) -> Option<&'a Backtrace> {
    Some(&self.backtrace)
  }
}

impl std::fmt::Display for EyrosError {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match &self.kind {
      EyrosErrorKind::MetaBitfieldInsufficientBytes {} => {
        write![f, "not enough bytes to construct roots bitfield for Meta"]
      },
      EyrosErrorKind::ScalarInBounds {} => {
        write![f, "scalar found in bounds"]
      },
      EyrosErrorKind::IntervalSides { dimension, min, max } => {
        write![f, "!(min <= max) dimension {} for Coord::Interval({:?},{:?})",
          dimension, min, max]
      },
      EyrosErrorKind::TreeRemoved { id } => {
        write![f, "attempted to load tree scheduled for removal with id={}", id]
      },
      EyrosErrorKind::TreeEmpty { id, file } => {
        write![f, "tree with id={} located at file={} is empty", id, file]
      },
      EyrosErrorKind::RemoveIdsMissing { ids } => {
        write![f, "ids not found during remove(): {}", ids.join(", ")]
      },
    }
  }
}