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
/*
 * Copyright (c) Gabriel Amihalachioaie, SimpleG 2023.
 */

use std::fmt::{Display, Formatter};

use crate::error_kind::SERIALIZATION_FAILURE;

#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub struct Error {
    error_kind: String,
    message: String,
}

impl Error {
    pub fn new(error_kind: String, message: String) -> Self {
        Self {
            error_kind,
            message,
        }
    }

    pub fn error_kind(&self) -> &str {
        self.error_kind.as_str()
    }

    pub fn message(&self) -> &str {
        self.message.as_str()
    }
}

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

impl From<serde_yaml::Error> for Error {
    fn from(value: serde_yaml::Error) -> Self {
        Self {
            error_kind: SERIALIZATION_FAILURE.to_string(),
            message: value.to_string(),
        }
    }
}

impl From<serde_json::Error> for Error {
    fn from(value: serde_json::Error) -> Self {
        Self {
            error_kind: SERIALIZATION_FAILURE.to_string(),
            message: value.to_string(),
        }
    }
}

impl From<std::io::Error> for Error {
    fn from(value: std::io::Error) -> Self {
        Self {
            error_kind: value.kind().to_string(),
            message: value.to_string(),
        }
    }
}