use crate::base;
use crate::utils;
use std::convert;
use std::ffi;
use std::os::raw::{c_char, c_int};
use std::str;
#[link(name = "guestfs")]
extern "C" {
fn guestfs_last_error(g: *mut base::guestfs_h) -> *const c_char;
fn guestfs_last_errno(g: *mut base::guestfs_h) -> c_int;
}
#[derive(Debug)]
pub struct APIError {
operation: &'static str,
message: String,
errno: i32,
}
#[derive(Debug)]
pub enum Error {
API(APIError),
IllegalString(ffi::NulError),
Utf8Error(str::Utf8Error),
Create,
}
impl convert::From<ffi::NulError> for Error {
fn from(error: ffi::NulError) -> Self {
Error::IllegalString(error)
}
}
impl convert::From<str::Utf8Error> for Error {
fn from(error: str::Utf8Error) -> Self {
Error::Utf8Error(error)
}
}
impl base::Handle {
pub(crate) fn get_error_from_handle(&self, operation: &'static str) -> Error {
let c_msg = unsafe { guestfs_last_error(self.g) };
let message = unsafe { utils::char_ptr_to_string(c_msg).unwrap() };
let errno = unsafe { guestfs_last_errno(self.g) };
Error::API(APIError {
operation,
message,
errno,
})
}
}