aboutsummaryrefslogtreecommitdiff
path: root/nix-rust/src/error.rs
blob: 85ac926e915da1241cead139846f0b4bff0746b2 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::fmt;

#[derive(Debug)]
pub enum Error {
    InvalidPath(crate::store::StorePath),
    BadStorePath(std::path::PathBuf),
    BadNarInfo,
    BadBase32,
    StorePathNameEmpty,
    StorePathNameTooLong,
    BadStorePathName,
    NarSizeFieldTooBig,
    BadNarString,
    BadNarPadding,
    BadNarVersionMagic,
    MissingNarOpenTag,
    MissingNarCloseTag,
    MissingNarField,
    BadNarField(String),
    BadExecutableField,
    IOError(std::io::Error),
    #[cfg(unused)]
    HttpError(hyper::error::Error),
    Misc(String),
    Foreign(CppException),
    BadTarFileMemberName(String),
}

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

#[cfg(unused)]
impl From<hyper::error::Error> for Error {
    fn from(err: hyper::error::Error) -> Self {
        Error::HttpError(err)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::InvalidPath(_) => write!(f, "invalid path"),
            Error::BadNarInfo => write!(f, ".narinfo file is corrupt"),
            Error::BadStorePath(path) => write!(f, "path '{}' is not a store path", path.display()),
            Error::BadBase32 => write!(f, "invalid base32 string"),
            Error::StorePathNameEmpty => write!(f, "store path name is empty"),
            Error::StorePathNameTooLong => {
                write!(f, "store path name is longer than 211 characters")
            }
            Error::BadStorePathName => write!(f, "store path name contains forbidden character"),
            Error::NarSizeFieldTooBig => write!(f, "size field in NAR is too big"),
            Error::BadNarString => write!(f, "NAR string is not valid UTF-8"),
            Error::BadNarPadding => write!(f, "NAR padding is not zero"),
            Error::BadNarVersionMagic => write!(f, "unsupported NAR version"),
            Error::MissingNarOpenTag => write!(f, "NAR open tag is missing"),
            Error::MissingNarCloseTag => write!(f, "NAR close tag is missing"),
            Error::MissingNarField => write!(f, "expected NAR field is missing"),
            Error::BadNarField(s) => write!(f, "unrecognized NAR field '{}'", s),
            Error::BadExecutableField => write!(f, "bad 'executable' field in NAR"),
            Error::IOError(err) => write!(f, "I/O error: {}", err),
            #[cfg(unused)]
            Error::HttpError(err) => write!(f, "HTTP error: {}", err),
            Error::Foreign(_) => write!(f, "<C++ exception>"), // FIXME
            Error::Misc(s) => write!(f, "{}", s),
            Error::BadTarFileMemberName(s) => {
                write!(f, "tar archive contains illegal file name '{}'", s)
            }
        }
    }
}

impl From<Error> for CppException {
    fn from(err: Error) -> Self {
        match err {
            Error::Foreign(ex) => ex,
            _ => CppException::new(&err.to_string()),
        }
    }
}

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

impl CppException {
    fn new(s: &str) -> Self {
        Self(unsafe { make_error(s) })
    }
}

impl Drop for CppException {
    fn drop(&mut self) {
        unsafe {
            destroy_error(self.0);
        }
    }
}

extern "C" {
    #[allow(improper_ctypes)] // YOLO
    fn make_error(s: &str) -> *const libc::c_void;

    fn destroy_error(exc: *const libc::c_void);
}