[PATCH v2 15/16] scripts/qapi: add serde attributes

Paolo Bonzini
2026-01-08T13:12:52Z

From: Marc-André Lureau <marcandre.lureau@redhat.com>

Generate serde attributes to match the serialization format to QAPI's:

- for enums, map Rust enum variants to original QAPI names

- for structs, rejects JSON with extra fields and omit optional fields
(as opposed to serializing them as null)

- for union variants:
- use tagged union format matching QAPI's discriminator,
- map variant names to original QAPI names
- flatten union data into parent struct

- for alternates, use type-based discrimination

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
scripts/qapi/rs_types.py | 39 ++++++++++++++++++++++++++++++---------
1 file changed, 30 insertions(+), 9 deletions(-)

diff --git a/scripts/qapi/rs_types.py b/scripts/qapi/rs_types.py
index 64702eb54ae..fb3362ad601 100644
--- a/scripts/qapi/rs_types.py
+++ b/scripts/qapi/rs_types.py
@@ -33,6 +33,7 @@

objects_seen = set()
+SERDE_SKIP_NONE = '#[serde(skip_serializing_if = "Option::is_none")]'

def gen_rs_variants_to_tag(name: str,
@@ -79,11 +80,13 @@ def gen_rs_variants(name: str,
ret = mcgen('''

%(cfg)s
-#[derive(Clone, Debug, PartialEq)]
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+#[serde(tag = "%(tag)s")]
pub enum %(rs_name)sVariant {
''',
cfg=ifcond.rsgen(),
- rs_name=rs_name(name))
+ rs_name=rs_name(name),
+ tag=variants.tag_member.name)

for var in variants.variants:
type_name = var.type.name
@@ -91,18 +94,22 @@ def gen_rs_variants(name: str,
if type_name == 'q_empty':
ret += mcgen('''
%(cfg)s
+ #[serde(rename = "%(rename)s")]
%(var_name)s,
''',
cfg=var.ifcond.rsgen(),
- var_name=var_name)
+ var_name=var_name,
+ rename=var.name)
else:
ret += mcgen('''
%(cfg)s
+ #[serde(rename = "%(rename)s")]
%(var_name)s(%(rs_type)s),
''',
cfg=var.ifcond.rsgen(),
var_name=var_name,
- rs_type=var.type.rs_type())
+ rs_type=var.type.rs_type(),
+ rename=var.name)

ret += mcgen('''
}
@@ -158,9 +165,11 @@ def gen_struct_members(members: List[QAPISchemaObjectTypeMember],
typ = 'Option<%s>' % typ
ret += mcgen('''
%(cfg)s
+ %(serde_skip_if)s
pub %(rs_name)s: %(rs_type)s,
''',
cfg=memb.ifcond.rsgen(),
+ serde_skip_if=SERDE_SKIP_NONE if memb.optional else '',
rs_type=typ,
rs_name=rs_name(camel_to_lower(memb.name)))
return ret
@@ -181,17 +190,23 @@ def gen_rs_object(name: str,
ret = ''
objects_seen.add(name)

+ serde_deny_unknown_fields = "#[serde(deny_unknown_fields)]"
if variants:
ret += gen_rs_variants(name, ifcond, variants)
+ # we can't use because of the flatten unions
+ # serde FlatMapAccess should consume the fields?
+ serde_deny_unknown_fields = ""

ret += mcgen('''

%(cfg)s
-#[derive(Clone, Debug, PartialEq)]
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+%(serde_deny_unknown_fields)s
pub struct %(rs_name)s {
''',
cfg=ifcond.rsgen(),
- rs_name=rs_name(name))
+ rs_name=rs_name(name),
+ serde_deny_unknown_fields=serde_deny_unknown_fields)

if base:
if not base.is_implicit():
@@ -213,6 +228,7 @@ def gen_rs_object(name: str,

if variants:
ret += mcgen('''
+ #[serde(flatten)]
pub u: %(rs_type)sVariant,
''', rs_type=rs_name(name))
ret += mcgen('''
@@ -227,7 +243,7 @@ def gen_rs_enum(name: str,
ret = mcgen('''

%(cfg)s
-#[derive(Copy, Clone, Debug, PartialEq)]
+#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
''',
cfg=ifcond.rsgen())

@@ -243,10 +259,12 @@ def gen_rs_enum(name: str,
for member in members:
ret += mcgen('''
%(cfg)s
+ #[serde(rename = "%(member_name)s")]
%(c_enum)s,
''',
cfg=member.ifcond.rsgen(),
- c_enum=rs_name(camel_to_upper(member.name)))
+ c_enum=rs_name(camel_to_upper(member.name)),
+ member_name=member.name)
ret += '''}

'''
@@ -280,7 +298,8 @@ def gen_rs_alternate(name: str,

ret += mcgen('''
%(cfg)s
-#[derive(Clone, Debug, PartialEq)]
+#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+#[serde(untagged)]
pub enum %(rs_name)s {
''',
cfg=ifcond.rsgen(),
@@ -330,6 +349,8 @@ def visit_begin(self, schema: QAPISchema) -> None:
// that *could* be Eq too.
#![allow(clippy::derive_partial_eq_without_eq)]

+use serde_derive::{Serialize, Deserialize};
+
use util::qobject::QObject;
'''))

--
2.52.0