dia_time/
error.rs

1/*
2==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
3
4Dia-Time
5
6Copyright (C) 2018-2022, 2024  Anonymous
7
8There are several releases over multiple years,
9they are listed as ranges, such as: "2018-2022".
10
11This program is free software: you can redistribute it and/or modify
12it under the terms of the GNU Lesser General Public License as published by
13the Free Software Foundation, either version 3 of the License, or
14(at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19GNU Lesser General Public License for more details.
20
21You should have received a copy of the GNU Lesser General Public License
22along with this program.  If not, see <https://www.gnu.org/licenses/>.
23
24::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
25*/
26
27//! # Error
28
29use {
30    alloc::{
31        borrow::Cow,
32        fmt::{self, Display, Formatter},
33    },
34};
35
36#[cfg(feature="std")]
37use {
38    alloc::string::ToString,
39    std::io,
40};
41
42/// # Error
43#[derive(Debug)]
44pub struct Error {
45    line: u32,
46    module_path: &'static str,
47    msg: Option<Cow<'static, str>>,
48}
49
50impl Error {
51
52    /// # Makes new instance
53    pub (crate) const fn new(line: u32, module_path: &'static str, msg: Option<Cow<'static, str>>) -> Self {
54        Self {
55            line,
56            module_path,
57            msg,
58        }
59    }
60
61    /// # Line
62    pub const fn line(&self) -> u32 {
63        self.line
64    }
65
66    /// # Module path
67    pub const fn module_path(&self) -> &str {
68        self.module_path
69    }
70
71    /// # Error message
72    pub fn msg(&self) -> Option<&str> {
73        self.msg.as_deref()
74    }
75
76}
77
78impl Display for Error {
79
80    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
81        match self.msg.as_ref() {
82            Some(msg) => write!(
83                f, "[{tag}][{module_path}-{line}] {msg}", tag=crate::TAG, line=self.line, module_path=self.module_path, msg=msg,
84            ),
85            None => write!(f, "[{tag}][{module_path}-{line}]", tag=crate::TAG, line=self.line, module_path=self.module_path),
86        }
87    }
88
89}
90
91#[cfg(feature="std")]
92#[doc(cfg(feature="std"))]
93impl From<Error> for io::Error {
94
95    fn from(err: Error) -> Self {
96        io::Error::new(io::ErrorKind::Other, err.to_string())
97    }
98
99}