zvariant

Derive Macro DeserializeDict

source
#[derive(DeserializeDict)]
{
    // Attributes available to this derive:
    #[zvariant]
}
Expand description

Adds Deserialize implementation to structs to be deserialized from a{sv} type.

This macro deserializes a D-Bus dictionary type as a struct, where keys are strings and values are generic values. Such dictionary types are very commonly used with D-Bus and GVariant.

§Examples

For structs it works just like serde’s Deserialize macros:

use zvariant::{DeserializeDict, Type};

#[derive(DeserializeDict, Type)]
#[zvariant(signature = "a{sv}")]
#[allow(unused)]
struct Struct {
    field1: u16,
    #[zvariant(rename = "another-name")]
    field2: i64,
    optional_field: Option<String>,
}

The deserialized D-Bus dictionary {"field1": Value::U16(42), "another-name": Value::I64(77)} will be Struct {42, 77, None}.

§Auto renaming fields

The macro supports specifying a Serde-like #[zvariant(rename_all = "case")] attribute on structures. The attribute allows to rename all the fields from snake case to another case automatically:

use zvariant::{SerializeDict, Type};

#[derive(SerializeDict, Type)]
#[zvariant(signature = "a{sv}", rename_all = "PascalCase")]
struct Struct {
    field1: u16,
    #[zvariant(rename = "another-name")]
    field2: i64,
    optional_field: Option<String>,
}

It’s still possible to specify custom names for individual fields using the #[zvariant(rename = "another-name")] attribute even when the rename_all attribute is present.

Currently the macro supports the following values for case:

  • "lowercase"
  • "UPPERCASE"
  • "PascalCase"
  • "camelCase"
  • "snake_case"