prime_data/data/
error.rs

1#![allow(missing_docs)]
2//! Error Handling
3
4use std::{fmt, ops::RangeInclusive};
5
6/// Result abstraction for public methods that return some result
7pub type PrimeResult<T> = Result<T, PrimeError>;
8
9/// This Error Struct is what will be returned with every function in this crate that yields an error
10pub 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
29/// * **NotEnoughData** - This error happens when someone tries to access some range of numbers that 
30/// PrimeData does not have information about. The range it stores is the difference between the range given
31/// and the data's range.
32/// 
33/// * **OutOfBounds** - Same concept, except it doesn't need to be some range. If you have some set of values
34/// {1, 3, 4} and try to access the number 2, it's in the range but not in the set's bounds.
35pub 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
49/// Some errors can happen in more than one context, such as trying to access some number outside of a
50/// range. This will give better context to what happened, for example, when unwrapping some function that
51/// could return an error, and reading the error message.
52pub 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}