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

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

use zip::result::ZipError;

use crate::error_kind::{SERIALIZATION_FAILURE, REQUEST_FAILURE, COMPRESSION_FAILURE};

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

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

    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(),
        }
    }
}

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

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