qapi_qmp/
lib.rs

1#![allow(non_snake_case, non_camel_case_types)]
2#![doc(html_root_url = "https://docs.rs/qapi-qmp/0.15.0")]
3#![allow(deprecated)]
4
5use std::io;
6use std::string::String as StdString;
7use std::convert::TryFrom;
8use serde::{Deserialize, Serialize};
9
10include!(concat!(env!("OUT_DIR"), "/qmp.rs"));
11
12pub type QmpMessageAny = QmpMessage<qapi_spec::Any>;
13
14pub trait QmpCommand: qapi_spec::Command { }
15impl<'a, T: QmpCommand> QmpCommand for &'a T { }
16impl<'a, T: QmpCommand> QmpCommand for &'a mut T { }
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19#[serde(untagged)]
20pub enum QmpMessage<C> {
21    Event(Event),
22    Response(qapi_spec::Response<C>),
23}
24
25impl<C> TryFrom<QmpMessage<C>> for qapi_spec::Response<C> {
26    type Error = io::Error;
27
28    fn try_from(m: QmpMessage<C>) -> Result<Self, Self::Error> {
29        match m {
30            QmpMessage::Response(res) => Ok(res),
31            QmpMessage::Event(..) =>
32                Err(io::Error::new(io::ErrorKind::InvalidData, "QMP event where a response was expected")),
33        }
34    }
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct QMP {
39    pub version: VersionInfo,
40    pub capabilities: Vec<QmpCapability>,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize)]
44#[serde(untagged)]
45pub enum QmpCapability {
46    #[serde(rename = "oob")]
47    OutOfBand,
48    Unknown(qapi_spec::Any),
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct QapiCapabilities {
53    pub QMP: QMP,
54}
55
56impl QapiCapabilities {
57    pub fn supports_oob(&self) -> bool {
58        self.QMP.capabilities.iter().any(|c| match c {
59            QmpCapability::OutOfBand => true,
60            _ => false,
61        })
62    }
63
64    pub fn capabilities<'a>(&'a self) -> impl Iterator<Item=QMPCapability> + 'a {
65        self.QMP.capabilities.iter().filter_map(|c| match c {
66            QmpCapability::OutOfBand => Some(QMPCapability::oob),
67            QmpCapability::Unknown(..) => None,
68        })
69    }
70}
71
72impl device_add {
73    pub fn new<D: Into<StdString>, I: Into<Option<StdString>>, B: Into<Option<StdString>>, P: IntoIterator<Item=(StdString, qapi_spec::Any)>>(driver: D, id: I, bus: B, props: P) -> Self {
74        device_add {
75            driver: driver.into(),
76            id: id.into(),
77            bus: bus.into(),
78            arguments: props.into_iter().collect(),
79        }
80    }
81}