summaryrefslogtreecommitdiff
path: root/rola-vcs/src/err.rs
blob: 71e0a34795e8550a2aba16137c6ba3595807cc27 (plain) (blame)
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
//! Error
//!
//! This module is used to create and log Rorolala standard errors

use std::{
    fmt::{Display, Formatter},
    io::Error as IoError,
};

mod io;

/// Rorolala standard error
#[derive(Default)]
pub struct RolaError {
    pub module: RolaModule,
    pub data: RolaErrorData,
    pub message: String,
}

/// Rorolala module, used to locate the source of an error
#[derive(Debug, Eq, Default)]
#[repr(u8)]
pub enum RolaModule {
    #[default]
    Empty,

    Bucket,
    Workdraft,
}

/// Error data
#[derive(Debug, Default)]
pub enum RolaErrorData {
    #[default]
    Empty,

    /// IO error
    IO(IoError),
}

impl RolaError {
    /// Create a new RolaError
    pub fn new(module: RolaModule, data: RolaErrorData, message: String) -> Self {
        Self {
            module,
            data,
            message,
        }
    }

    /// Create an empty RolaError
    pub fn empty() -> Self {
        Self {
            module: RolaModule::Empty,
            data: RolaErrorData::Empty,
            message: String::new(),
        }
    }

    /// Set the module
    pub fn with_module(mut self, module: RolaModule) -> Self {
        self.module = module;
        self
    }

    /// Set the message
    pub fn with_message(mut self, message: String) -> Self {
        self.message = message;
        self
    }

    /// Set the error data
    pub fn with_data(mut self, data: RolaErrorData) -> Self {
        self.data = data;
        self
    }
}

impl Display for RolaError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl PartialEq for RolaError {
    fn eq(&self, other: &Self) -> bool {
        self.message == other.message && self.module == other.module
    }
}

impl Display for RolaModule {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl PartialEq for RolaModule {
    fn eq(&self, other: &Self) -> bool {
        matches!(
            (self, other),
            (RolaModule::Bucket, RolaModule::Bucket)
                | (RolaModule::Workdraft, RolaModule::Workdraft)
        )
    }
}