1use crate::base;
20use crate::utils;
21use std::convert;
22use std::ffi;
23use std::os::raw::{c_char, c_int};
24use std::str;
25
26#[link(name = "guestfs")]
27extern "C" {
28 fn guestfs_last_error(g: *mut base::guestfs_h) -> *const c_char;
29 fn guestfs_last_errno(g: *mut base::guestfs_h) -> c_int;
30}
31
32#[derive(Debug)]
33pub struct APIError {
34 operation: &'static str,
35 message: String,
36 errno: i32,
37}
38
39#[derive(Debug)]
40pub enum Error {
41 API(APIError),
42 IllegalString(ffi::NulError),
43 Utf8Error(str::Utf8Error),
44 Create,
45}
46
47impl convert::From<ffi::NulError> for Error {
48 fn from(error: ffi::NulError) -> Self {
49 Error::IllegalString(error)
50 }
51}
52
53impl convert::From<str::Utf8Error> for Error {
54 fn from(error: str::Utf8Error) -> Self {
55 Error::Utf8Error(error)
56 }
57}
58
59impl base::Handle {
60 pub(crate) fn get_error_from_handle(&self, operation: &'static str) -> Error {
61 let c_msg = unsafe { guestfs_last_error(self.g) };
62 let message = unsafe { utils::char_ptr_to_string(c_msg).unwrap() };
63 let errno = unsafe { guestfs_last_errno(self.g) };
64 Error::API(APIError {
65 operation,
66 message,
67 errno,
68 })
69 }
70}