1#![allow(missing_docs)]
2use std::{fmt, ops::RangeInclusive};
5
6pub type PrimeResult<T> = Result<T, PrimeError>;
8
9pub struct PrimeError {
11 pub context: ErrorContext,
12 pub error: ErrorType,
13}
14
15impl std::error::Error for PrimeError {}
16
17impl fmt::Display for PrimeError {
18 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19 write!(f, "Error: {}\n -> {}", self.context, self.error)
20 }
21}
22
23impl fmt::Debug for PrimeError {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 fmt::Display::fmt(self, f)
26 }
27}
28
29pub enum ErrorType {
36 NotEnoughData(RangeInclusive<u64>),
37 OutOfBounds(u64),
38}
39
40impl fmt::Display for ErrorType {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 match self {
43 Self::NotEnoughData(range) => write!(f, "Cannot access any data in the given range: {:?}", range),
44 Self::OutOfBounds(num) => write!(f, "Cannot access the given number: {}", num),
45 }
46 }
47}
48
49pub struct ErrorContext {
53 pub action: ErrorAction,
54 pub source: ErrorSource,
55}
56
57pub enum ErrorAction {
58 Reading,
59 Modifying,
60 Generating,
61}
62pub enum ErrorSource {
63 PrimeByte,
64 PrimeData,
65}
66
67impl fmt::Display for ErrorContext {
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 write!(f, "An error occurred when trying to {} {}", self.action, self.source)
70 }
71}
72
73impl fmt::Display for ErrorAction {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 match self {
76 Self::Reading => write!(f, "read"),
77 Self::Modifying => write!(f, "modify"),
78 Self::Generating => write!(f, "generate"),
79 }
80 }
81}
82
83impl fmt::Display for ErrorSource {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 match self {
86 Self::PrimeByte => write!(f, "PrimeByte"),
87 Self::PrimeData => write!(f, "PrimeData"),
88 }
89 }
90}