virtualbox_rs 0.4.2

A Rust library for interacting with VirtualBox, providing a safe and idiomatic interface to the VirtualBox API.
Documentation
use crate::event_detail::utility::cast_event;
use crate::event_detail::DetailEvent;
use crate::utility::macros::macros::{
    get_function_result_bool, get_function_result_number, get_function_result_pointer,
};
use crate::{GuestFile, GuestSession, VboxError};
use log::error;
use std::fmt::Display;
use vbox_raw::sys_lib::{
    IEvent, IGuestFile, IGuestFileWriteEvent, IGuestSession, IGUESTFILEWRITEEVENT_IID_STR,
};

/// Notification when a guest file changed its current offset via File::seek.
#[derive(Debug)]
pub struct GuestFileWriteEvent {
    /// Notification when data has been written to a guest file.
    pub session: GuestSession,
    /// Guest file object which is related to this event.
    pub file: GuestFile,
    /// Current offset (in bytes).
    pub offset: i64,
    /// Processed input or output (in bytes).
    pub processed: u32,
    pub midl_does_not_like_empty_interfaces: bool,
}

impl GuestFileWriteEvent {
    pub fn new(object: *mut IEvent) -> DetailEvent {
        match Self::create(object) {
            Ok(detail) => DetailEvent::GuestFileWriteEvent(detail),
            Err(err) => {
                error!("GuestFileWriteEvent error:{}", err);
                DetailEvent::Null
            }
        }
    }
    fn create(object: *mut IEvent) -> Result<Self, VboxError> {
        let obj1 = cast_event(object, IGUESTFILEWRITEEVENT_IID_STR)?;
        let session = Self::get_session(obj1)?;
        let file = Self::get_file(obj1)?;
        let offset = Self::get_offset(obj1)?;
        let processed = Self::get_processed(obj1)?;
        let midl_does_not_like_empty_interfaces =
            Self::get_midl_does_not_like_empty_interfaces(obj1)?;
        Ok(Self {
            session,
            file,
            offset,
            processed,
            midl_does_not_like_empty_interfaces,
        })
    }

    fn get_session(object: *mut IGuestFileWriteEvent) -> Result<GuestSession, VboxError> {
        let session = get_function_result_pointer!(object, GetSession, *mut IGuestSession)?;
        Ok(GuestSession::new(session))
    }
    fn get_file(object: *mut IGuestFileWriteEvent) -> Result<GuestFile, VboxError> {
        let file = get_function_result_pointer!(object, GetFile, *mut IGuestFile)?;
        Ok(GuestFile::new(file))
    }
    fn get_offset(object: *mut IGuestFileWriteEvent) -> Result<i64, VboxError> {
        get_function_result_number!(object, GetOffset, i64)
    }
    fn get_processed(object: *mut IGuestFileWriteEvent) -> Result<u32, VboxError> {
        get_function_result_number!(object, GetProcessed, u32)
    }
    fn get_midl_does_not_like_empty_interfaces(
        new_obj: *mut IGuestFileWriteEvent,
    ) -> Result<bool, VboxError> {
        get_function_result_bool!(new_obj, GetMidlDoesNotLikeEmptyInterfaces)
    }
}

impl Display for GuestFileWriteEvent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = format!("{:?}", self);
        write!(f, "{}", format!("{}", s))
    }
}