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
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
use std::fmt::{Display, Formatter};
use std::fmt;
use crate::types::NumberLike;
use crate::constants::{MAGIC_HEADER, MAX_MAX_DEPTH, MAX_ENTRIES};
use std::error::Error;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MaxEntriesError {
  pub n: usize,
}

impl Display for MaxEntriesError {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(
      f,
      "number of elements {} exceeded max number of elements {}",
      self.n,
      MAX_ENTRIES,
    )
  }
}

impl Error for MaxEntriesError {}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MaxDepthError {
  pub max_depth: u32,
}

impl Display for MaxDepthError {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(
      f,
      "max depth {} exceeded max max depth of {}",
      self.max_depth,
      MAX_MAX_DEPTH,
    )
  }
}

impl Error for MaxDepthError {}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OutOfRangeError<T> where T: NumberLike {
  pub num: T,
}

impl<T> Display for OutOfRangeError<T> where T: NumberLike{
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(
      f,
      "number {} was not found in any range",
      self.num,
    )
  }
}

impl<T> Error for OutOfRangeError<T> where T: NumberLike {}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MisalignedBitReaderError {}

impl Display for MisalignedBitReaderError {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(
      f,
      "cannot read_bytes on misaligned bit reader"
    )
  }
}

impl Error for MisalignedBitReaderError {}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MagicHeaderError {
  pub header: Vec<u8>,
}

impl Display for MagicHeaderError {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(
      f,
      "header {:?} did not match qco expected header {:?}",
      self.header,
      MAGIC_HEADER,
    )
  }
}

impl Error for MagicHeaderError {}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HeaderDtypeError {
  pub dtype_byte: u8,
  pub expected_byte: u8,
}

impl Error for HeaderDtypeError {}

impl Display for HeaderDtypeError {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(
      f,
      "data type byte {} did not match expected data type byte {}",
      self.dtype_byte,
      self.expected_byte,
    )
  }
}