rustre_core/engine/
error.rs

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
use ecow::{eco_format, EcoString};
use std::io;
use std::str::Utf8Error;
use std::string::FromUtf8Error;

/// Error that may occur while trying to read a file
#[derive(Debug, Clone, Hash, thiserror::Error)]
pub enum FileError {
    #[error("file not found")]
    NotFound,

    #[error("wrong file encoding")]
    Encoding,

    #[error("access denied")]
    AccessDenied,

    #[error("file is a directory")]
    IsDirectory,

    #[error("file is special")]
    IsSpecial,

    #[error("{0}")]
    Other(EcoString),
}

impl From<io::Error> for FileError {
    fn from(err: io::Error) -> Self {
        match err.kind() {
            io::ErrorKind::NotFound => Self::NotFound,
            io::ErrorKind::PermissionDenied => Self::AccessDenied,
            // Extract io::ErrorKind::IsADirectory when stabilized
            other => {
                let err = eco_format!("{other}");
                if err.contains("is a directory") {
                    FileError::IsDirectory
                } else {
                    FileError::Other(err)
                }
            }
        }
    }
}

impl From<Utf8Error> for FileError {
    fn from(_value: Utf8Error) -> Self {
        Self::Encoding
    }
}

impl From<FromUtf8Error> for FileError {
    fn from(_value: FromUtf8Error) -> Self {
        Self::Encoding
    }
}