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
108
109
110
111
112
use std::cell::RefCell;
use std::ffi::{c_char, CString};
use std::{fmt, ptr};

#[derive(Debug, Clone)]
#[repr(C)]
pub enum ErrorKind {
    Generic,
    Pkgcraft,
    Config,
    Repo,
}

#[derive(Debug, Clone)]
pub struct Error {
    message: String,
    kind: ErrorKind,
}

impl Error {
    pub fn new<S: Into<String>>(s: S) -> Self {
        Error {
            message: s.into(),
            kind: ErrorKind::Generic,
        }
    }
}

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

impl std::error::Error for Error {}

impl From<pkgcraft::Error> for Error {
    fn from(e: pkgcraft::Error) -> Self {
        use pkgcraft::Error::*;
        let kind = match &e {
            Config(_) => ErrorKind::Config,
            InvalidRepo { .. } => ErrorKind::Repo,
            RepoInit(_) => ErrorKind::Repo,
            _ => ErrorKind::Pkgcraft,
        };

        Error {
            message: e.to_string(),
            kind,
        }
    }
}

impl From<std::str::Utf8Error> for Error {
    fn from(e: std::str::Utf8Error) -> Self {
        Error::new(e.to_string())
    }
}

#[repr(C)]
pub struct PkgcraftError {
    message: *mut c_char,
    kind: ErrorKind,
}

impl From<Error> for PkgcraftError {
    fn from(e: Error) -> Self {
        PkgcraftError {
            message: CString::new(e.message)
                .expect("invalid error message")
                .into_raw(),
            kind: e.kind,
        }
    }
}

impl Drop for PkgcraftError {
    fn drop(&mut self) {
        unsafe {
            drop(CString::from_raw(self.message));
        }
    }
}

thread_local! {
    static LAST_ERROR: RefCell<Option<Error>> = RefCell::new(None);
}

/// Update the most recent error, clearing the previous value.
pub(crate) fn update_last_error<E: Into<Error> + fmt::Debug>(err: E) {
    LAST_ERROR.with(|prev| *prev.borrow_mut() = Some(err.into()));
}

/// Get the most recent error, returns NULL if none exists.
#[no_mangle]
pub extern "C" fn pkgcraft_error_last() -> *mut PkgcraftError {
    match LAST_ERROR.with(|prev| prev.borrow_mut().take()) {
        Some(e) => Box::into_raw(Box::new(e.into())),
        None => ptr::null_mut(),
    }
}

/// Free an error.
///
/// # Safety
/// The argument must be a non-null PkgcraftError pointer or NULL.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_error_free(e: *mut PkgcraftError) {
    if !e.is_null() {
        unsafe { drop(Box::from_raw(e)) };
    }
}