Skip to main content

virtualbox_rs/snapshot/
mod.rs

1pub mod implementation;
2
3use crate::utility::macros::macros::call_function;
4use crate::VboxError;
5use log::{debug, error};
6use std::collections::BTreeMap;
7use std::fmt::{Debug, Display, Formatter};
8use vbox_raw::sys_lib::ISnapshot;
9
10/// The Snapshot interface represents a snapshot of the virtual machine.
11///
12/// **Reference to the official documentation:**
13///
14/// [https://www.virtualbox.org/sdkref/interface_i_snapshot.html](https://www.virtualbox.org/sdkref/interface_i_snapshot.html)
15
16pub struct Snapshot {
17    pub(crate) object: *mut ISnapshot,
18}
19
20impl Snapshot {
21    pub(crate) fn new(object: *mut ISnapshot) -> Self {
22        Self { object }
23    }
24}
25
26impl Snapshot {
27    fn release(&self) -> Result<i32, VboxError> {
28        call_function!(self.object, Release)
29    }
30}
31
32impl Drop for Snapshot {
33    fn drop(&mut self) {
34        match self.release() {
35            Ok(count) => {
36                debug!("Snapshot refcount: {}", count)
37            }
38            Err(err) => {
39                error!("Failed drop Snapshot. Error: {:?}", err)
40            }
41        }
42    }
43}
44
45impl Display for Snapshot {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        let mut map = BTreeMap::new();
48        map.insert("id", self.get_id().unwrap_or("").to_string());
49        map.insert("name", self.get_name().unwrap_or("").to_string());
50        map.insert(
51            "description",
52            self.get_description().unwrap_or("").to_string(),
53        );
54        map.insert("time_stamp", self.get_time_stamp().unwrap_or(0).to_string());
55        map.insert("online", self.get_online().unwrap_or(false).to_string());
56        if f.alternate() {
57            write!(f, "{}", format!("{:#?}", map))
58        } else {
59            write!(f, "{}", format!("{:?}", map))
60        }
61    }
62}
63
64impl Debug for Snapshot {
65    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
66        Display::fmt(self, f)
67    }
68}