1use comp_cat_rs::collapse::free_category::FreeCategoryError;
4
5#[derive(Debug)]
7pub enum Error {
8 FreeCategory(FreeCategoryError),
10 WireOutOfBounds {
12 wire_index: usize,
14 allocated: usize,
16 },
17 WireCountMismatch {
19 expected: usize,
21 actual: usize,
23 },
24 DivisionByZero,
26}
27
28impl core::fmt::Display for Error {
29 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
30 match self {
31 Self::FreeCategory(e) => write!(f, "free category error: {e}"),
32 Self::WireOutOfBounds {
33 wire_index,
34 allocated,
35 } => write!(
36 f,
37 "wire index {wire_index} out of bounds (allocated: {allocated})"
38 ),
39 Self::WireCountMismatch { expected, actual } => {
40 write!(f, "wire count mismatch: expected {expected}, got {actual}")
41 }
42 Self::DivisionByZero => write!(f, "division by zero"),
43 }
44 }
45}
46
47impl std::error::Error for Error {
48 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
49 match self {
50 Self::FreeCategory(e) => Some(e),
51 Self::WireOutOfBounds { .. }
52 | Self::WireCountMismatch { .. }
53 | Self::DivisionByZero => None,
54 }
55 }
56}
57
58impl From<FreeCategoryError> for Error {
59 fn from(e: FreeCategoryError) -> Self {
60 Self::FreeCategory(e)
61 }
62}