qapi_qga/
lib.rs

1#![allow(non_snake_case, non_camel_case_types)]
2#![doc(html_root_url = "https://docs.rs/qapi-qga/0.13.0")]
3
4include!(concat!(env!("OUT_DIR"), "/qga.rs"));
5
6use std::{io, str, fmt, error};
7use serde::{Deserialize, Serialize};
8
9pub trait QgaCommand: qapi_spec::Command { }
10impl<'a, T: QgaCommand> QgaCommand for &'a T { }
11impl<'a, T: QgaCommand> QgaCommand for &'a mut T { }
12
13#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
14#[serde(rename_all = "kebab-case")]
15pub enum GuestShutdownMode {
16    Halt,
17    Powerdown,
18    Reboot,
19}
20
21impl GuestExecStatus {
22    pub fn result(self) -> Result<Self, Self> {
23        if self.exited {
24            if self.exitcode != Some(0) || self.signal.is_some() {
25                Err(self)
26            } else {
27                Ok(self)
28            }
29        } else {
30            Ok(self)
31        }
32    }
33
34    fn message(&self) -> String {
35        let (err0, err1) = if let Some(Ok(data)) = self.err_data.as_ref().map(|s| str::from_utf8(s)) {
36            (": ", data)
37        } else {
38            ("", "")
39        };
40
41        let sig = if let Some(signal) = self.signal {
42            format!(" (terminated by signal {})", signal)
43        } else {
44            Default::default()
45        };
46
47        if let Some(code) = self.exitcode {
48            format!("guest process exited with code {}{}{}{}", code, sig, err0, err1)
49        } else if self.exited {
50            format!("guest process exited{}{}{}", sig, err0, err1)
51        } else {
52            format!("guest process is still running")
53        }
54    }
55}
56
57impl fmt::Display for GuestExecStatus {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        write!(f, "{}", self.message())
60    }
61}
62
63impl error::Error for GuestExecStatus {
64    fn description(&self) -> &str {
65        "guest process exit status"
66    }
67}
68
69impl From<GuestExecStatus> for io::Error {
70    fn from(s: GuestExecStatus) -> Self {
71        io::Error::new(io::ErrorKind::Other, s.to_string())
72    }
73}