Skip to content

Commit

Permalink
Dictionary type
Browse files Browse the repository at this point in the history
  • Loading branch information
arcnmx committed Mar 13, 2018
1 parent 96c3eb9 commit 94c5d53
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
19 changes: 15 additions & 4 deletions codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ fn valuety(value: &spec::Value, pubvis: bool, super_name: &str) -> String {
// "ringbuf-write", "ringbuf-read" can't be done because weird enums
);

let dict = value.ty.name == "any" && (
(super_name == "object-add" && value.name == "props") ||
(super_name == "CpuModelInfo" && value.name == "props")
);

// TODO: handle optional Vec<>s specially?

let ty = typename(&value.ty);
Expand All @@ -89,6 +94,8 @@ fn valuety(value: &spec::Value, pubvis: bool, super_name: &str) -> String {
}
} else if boxed {
("", format!("Box<{}>", ty))
} else if dict {
("", "::qapi::Dictionary".into())
} else {
("", ty)
};
Expand Down Expand Up @@ -143,11 +150,15 @@ impl<W: Write> Context<W> {
pub struct {}", type_identifier(&v.id))?;
match v.data {
spec::DataOrType::Data(ref data) => {
writeln!(self.out, " {{")?;
for data in &data.fields {
writeln!(self.out, "\t{},", valuety(&data, true, &v.id))?;
if v.id == "device_add" || v.id == "netdev_add" {
writeln!(self.out, "(pub ::qapi::Dictionary);")?;
} else {
writeln!(self.out, " {{")?;
for data in &data.fields {
writeln!(self.out, "\t{},", valuety(&data, true, &v.id))?;
}
writeln!(self.out, "}}")?;
}
writeln!(self.out, "}}")?;
},
spec::DataOrType::Type(ref ty) => {
writeln!(self.out, "(pub {});", type_identifier(&ty.name))?;
Expand Down
1 change: 1 addition & 0 deletions spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::de::DeserializeOwned;
use serde::Serialize;

pub use serde_json::Value as Any;
pub type Dictionary = serde_json::Map<String, Any>;

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct Empty { }
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub extern crate qapi_qmp as qmp;
#[cfg(feature = "qapi-qga")]
pub extern crate qapi_qga as qga;

pub use spec::{Any, Empty, Command, Event, Error, Timestamp};
pub use spec::{Any, Dictionary, Empty, Command, Event, Error, Timestamp};

#[cfg(any(feature = "qapi-qmp", feature = "qapi-qga"))]
mod qapi {
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub extern crate qapi_qmp as qmp;
#[cfg(feature = "qapi-qga")]
pub extern crate qapi_qga as qga;

pub use spec::{Any, Empty, Command, Event, Error, Timestamp};
pub use spec::{Any, Dictionary, Empty, Command, Event, Error, Timestamp};

use std::mem::replace;
use std::{io, str};
Expand Down

0 comments on commit 94c5d53

Please sign in to comment.