summaryrefslogtreecommitdiffstats
path: root/src/error/error_type.rs
blob: 80dc4bcfad3e159793516b730ebcafb6b22e369f (plain)
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
use std::io;

use super::JoshutoErrorKind;

pub struct JoshutoError {
    _kind: JoshutoErrorKind,
    _cause: String,
}

#[allow(dead_code)]
impl JoshutoError {
    pub fn new(_kind: JoshutoErrorKind, _cause: String) -> Self {
        Self { _kind, _cause }
    }

    pub fn kind(&self) -> JoshutoErrorKind {
        self._kind
    }
}

impl std::fmt::Display for JoshutoError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self._cause)
    }
}

impl std::convert::From<io::Error> for JoshutoError {
    fn from(err: io::Error) -> Self {
        Self {
            _kind: JoshutoErrorKind::from(err.kind()),
            _cause: err.to_string(),
        }
    }
}

impl std::convert::From<globset::Error> for JoshutoError {
    fn from(err: globset::Error) -> Self {
        Self {
            _kind: JoshutoErrorKind::from(err.kind()),
            _cause: err.to_string(),
        }
    }
}

impl std::convert::From<trash::Error> for JoshutoError {
    fn from(err: trash::Error) -> Self {
        let err = match err {
            trash::Error::Unknown => {
                std::io::Error::new(std::io::ErrorKind::Other, "Unknown Error")
            }
            trash::Error::TargetedRoot => {
                std::io::Error::new(std::io::ErrorKind::Other, "Targeted Root")
            }
            trash::Error::CanonicalizePath { code: _ } => {
                std::io::Error::new(std::io::ErrorKind::NotFound, "Not found")
            }
            trash::Error::Remove { code: Some(1) } => std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Cannot move files to trash from mounted system",
            ),
            _ => std::io::Error::new(std::io::ErrorKind::Other, "Unknown Error"),
        };
        Self {
            _kind: JoshutoErrorKind::from(err.kind()),
            _cause: err.to_string(),
        }
    }
}