evolution_common/error.rs
1//
2// MIT License
3//
4// Copyright (c) 2024 Firelink Data
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23//
24// File created: 2024-02-05
25// Last updated: 2024-05-28
26//
27
28use std::error;
29use std::fmt;
30use std::result;
31
32///
33pub type Result<T> = result::Result<T, Box<dyn error::Error>>;
34
35/// An error representing that something went wrong when setting up all resources.
36/// Most likely caused by some I/O error due to incorrect paths or write/read modes.
37#[derive(Debug)]
38pub struct SetupError {
39 details: String,
40}
41
42impl SetupError {
43 /// Create a new [`SetupError`] which will display the provided message.
44 pub fn new(msg: &str) -> Self {
45 Self {
46 details: msg.to_string(),
47 }
48 }
49}
50
51impl fmt::Display for SetupError {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 write!(f, "{}", self.details)
54 }
55}
56
57impl error::Error for SetupError {
58 fn description(&self) -> &str {
59 &self.details
60 }
61}
62
63/// An error representing that something went wrong during execution.
64#[derive(Debug)]
65pub struct ExecutionError {
66 details: String,
67}
68
69impl ExecutionError {
70 /// Create a new [`ExecutionError`] which will display the provided message.
71 pub fn new(msg: &str) -> Self {
72 Self {
73 details: msg.to_string(),
74 }
75 }
76}
77
78impl fmt::Display for ExecutionError {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 write!(f, "{}", self.details)
81 }
82}
83
84impl error::Error for ExecutionError {
85 fn description(&self) -> &str {
86 &self.details
87 }
88}
89
90#[cfg(test)]
91mod tests_error {
92 use super::*;
93
94 #[test]
95 fn test_setup_error() {
96 assert_eq!(
97 "uh oh stinky something went wrong!",
98 SetupError::new("uh oh stinky something went wrong!")
99 .to_string()
100 .as_str(),
101 );
102 }
103
104 #[test]
105 fn test_execution_error() {
106 assert_eq!(
107 "uh oh stinky something went wrong!",
108 ExecutionError::new("uh oh stinky something went wrong!")
109 .to_string()
110 .as_str(),
111 );
112 }
113}