logging_rs/errors.rs
1#![doc = include_str!("../.github/errors.md")]
2// logging-rs errors
3// Version: 1.1.0
4
5// Copyright (c) 2023-present ElBe Development.
6
7// Permission is hereby granted, free of charge, to any person obtaining a
8// copy of this software and associated documentation files (the 'Software'),
9// to deal in the Software without restriction, including without limitation
10// the rights to use, copy, modify, merge, publish, distribute, sublicense,
11// and/or sell copies of the Software, and to permit persons to whom the
12// Software is furnished to do so, subject to the following conditions:
13
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16
17// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS
18// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23// DEALINGS IN THE SOFTWARE.
24
25////////////////////////////////
26// IMPORTS AND USE STATEMENTS //
27////////////////////////////////
28
29use std::fmt;
30
31
32///////////
33// ERROR //
34///////////
35
36/// Error object.
37///
38/// Use [`Error::new()`] to create error objects instead of using this struct.
39///
40/// # Parameters
41///
42/// - `name`: The errors name.
43/// - `description`: The error description.
44/// - `exit_code`: The errors exit code.
45///
46/// # Returns
47///
48/// A new `Error` object with the specified name and description.
49///
50/// # Examples
51///
52/// ```rust
53/// # use logging_rs;
54/// logging_rs::errors::Error {
55/// name: "name".to_owned(),
56/// description: "description".to_owned(),
57/// exit_code: 1
58/// };
59/// ```
60#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
61pub struct Error {
62 /// The errors name.
63 pub name: String,
64 /// The error description.
65 pub description: String,
66 /// The errors exit code.
67 pub exit_code: i32,
68}
69
70/// Display implementation for the error object.
71impl fmt::Display for Error {
72 /// Format implementation for the error object.
73 ///
74 /// # Parameters
75 ///
76 /// - `self`: The error object.
77 /// - `f`: The [`fmt::Formatter`] to use.
78 ///
79 /// # Returns
80 ///
81 /// A [`fmt::Result`] containing the formatted error message.
82 ///
83 /// # Examples
84 ///
85 /// ```rust
86 /// # use logging_rs;
87 /// # let error = logging_rs::errors::Error::new("name", "description", 1);
88 /// println!("{}", error);
89 /// ```
90 ///
91 /// # See also
92 ///
93 /// - [`fmt::Display`]
94 /// - [`Error`]
95 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96 write!(f, "\x1b[31;1m{}\x1b[0m: {}", self.name, self.description)
97 }
98}
99
100impl Error {
101 /// Creates a new error object.
102 ///
103 /// # Parameters
104 ///
105 /// - `name`: The errors name.
106 /// - `description`: The error description.
107 /// - `exit_code`: The errors exit code.
108 ///
109 /// # Returns
110 ///
111 /// A new `Error` object with the specified name and description.
112 ///
113 /// # Examples
114 ///
115 /// ```rust
116 /// # use logging_rs;
117 /// logging_rs::errors::Error::new("name", "description", 1);
118 /// ```
119 ///
120 /// # See also
121 ///
122 /// - [`Error`]
123 pub fn new(name: &str, description: &str, exit_code: i32) -> Error {
124 return Error {
125 name: name.to_owned(),
126 description: description.to_owned(),
127 exit_code: exit_code,
128 };
129 }
130
131 /// Raises the error and exits with the specified exit code.
132 ///
133 /// # Parameters
134 ///
135 /// - `self`: The error object.
136 /// - `details`: The error details.
137 ///
138 /// # Aborts
139 ///
140 /// Exits with the specified exit code.
141 ///
142 /// # Examples
143 ///
144 /// ```should_panic
145 /// # use logging_rs;
146 /// # let error: logging_rs::errors::Error = logging_rs::errors::Error::new("name", "description", 1);
147 /// error.raise("Something went very wrong");
148 /// ```
149 ///
150 /// # See also
151 ///
152 /// - [`Error`]
153 pub fn raise(&self, details: &str) {
154 eprintln!("{}", self);
155 eprintln!("{}", details);
156
157 std::process::exit(self.exit_code);
158 }
159}