aboutsummaryrefslogtreecommitdiff
path: root/nix-rust/src/error.rs
blob: 9d6fa4c517dc5d848e77a84eec1cf4ac0f3bdc02 (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
#[derive(Debug)]
pub enum Error {
    InvalidPath(crate::store::StorePath),
    BadStorePath(std::path::PathBuf),
    BadNarInfo,
    BadBase32,
    IOError(std::io::Error),
    HttpError(reqwest::Error),
    Misc(String),
    Foreign(CppException),
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Self {
        Error::IOError(err)
    }
}

impl From<reqwest::Error> for Error {
    fn from(err: reqwest::Error) -> Self {
        Error::HttpError(err)
    }
}

impl From<Error> for CppException {
    fn from(err: Error) -> Self {
        match err {
            Error::InvalidPath(_) => unsafe { make_error("invalid path") }, // FIXME
            Error::BadNarInfo => unsafe { make_error(".narinfo file is corrupt") }, // FIXME
            Error::BadStorePath(path) => unsafe {
                make_error(&format!("path '{}' is not a store path", path.display()))
            }, // FIXME
            Error::BadBase32 => unsafe { make_error("invalid base32 string") }, // FIXME
            Error::IOError(err) => unsafe { make_error(&err.to_string()) },
            Error::HttpError(err) => unsafe { make_error(&err.to_string()) },
            Error::Foreign(ex) => ex,
            Error::Misc(s) => unsafe { make_error(&s) },
        }
    }
}

#[repr(C)]
#[derive(Debug)]
pub struct CppException(*const libc::c_void); // == std::exception_ptr*

extern "C" {
    #[allow(improper_ctypes)] // YOLO
    fn make_error(s: &str) -> CppException;
}