Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*Dict macros don't support nested dictionaries 'a{sa{sv}}' #312

Open
Tracked by #746
zeenix opened this issue Feb 1, 2023 · 8 comments
Open
Tracked by #746

*Dict macros don't support nested dictionaries 'a{sa{sv}}' #312

zeenix opened this issue Feb 1, 2023 · 8 comments
Labels
bug Something isn't working zvariant Issues/PRs related to zvariant crate

Comments

@zeenix
Copy link
Contributor

zeenix commented Feb 1, 2023

In GitLab by @seiwill on Feb 1, 2023, 17:58

Dictionary of Dictionaries is not supported and returns an Error::PaddingNot0 when decoded by Message::body().

This code attempts to get the ActiveConnection Settings from NetworkManager. It reads the a{sa{sv}} ConnectionSettings which Optionally contains other Dict Structs.

use zbus::{blocking::{Connection, Proxy}, zvariant::{DeserializeDict, Type, OwnedObjectPath}};

#[derive(Debug, Clone, PartialEq, Default, Type, DeserializeDict)]
#[zvariant(signature = "dict")]
pub struct SettingsConnection {
    pub autoconnect: Option<bool>,
    pub id: Option<String>,
    pub interface_name: Option<String>,
    pub read_only: Option<bool>,
    pub timestamp: Option<u64>,
    #[zvariant(rename = "type")]
    pub connection_type: Option<String>,
    pub uuid: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Default, Type, DeserializeDict)]
#[zvariant(signature = "dict")]
pub struct SettingsIp {
    pub method: Option<String>,
    pub addresses: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Default, Type, DeserializeDict)]
#[zvariant(signature = "a{sa{sv}}")]
pub struct ConnectionSettings {
    pub connection: Option<SettingsConnection>,
    pub ipv4: Option<SettingsIp>,
}

fn main() {
    let connection = Connection::system().unwrap();
    let proxy = Proxy::new(
        &connection,
        "org.freedesktop.NetworkManager",
        "/org/freedesktop/NetworkManager",
        "org.freedesktop.NetworkManager"
    ).unwrap();

    let active_connections: Vec<OwnedObjectPath> = proxy.get_property("ActiveConnections").unwrap();
    for ac in active_connections {
        println!("Looking up {ac:?}");

        let ac_proxy = Proxy::new(
            &connection,
            "org.freedesktop.NetworkManager",
            &ac,
            "org.freedesktop.NetworkManager.Connection.Active"
        ).unwrap();
        let conn_path: OwnedObjectPath = ac_proxy.get_property("Connection").unwrap();
        println!("  Connection Settings {conn_path:?}");

        let settings_proxy = Proxy::new(
            &connection,
            "org.freedesktop.NetworkManager",
            &conn_path,
            "org.freedesktop.NetworkManager.Settings.Connection"
        ).unwrap();

        let message = settings_proxy.call_method("GetSettings", &()).unwrap();
        println!("  Reply: {message:?}");

        // let settings_map : HashMap<String, HashMap<String, zvariant::OwnedValue>> = message.body().unwrap();
        let settings = message.body::<ConnectionSettings>().unwrap();
        println!("  {settings:?}\n");
    }
}

Running panics on the message.body().unwrap():

Looking up OwnedObjectPath(ObjectPath("/org/freedesktop/NetworkManager/ActiveConnection/5"))
  Connection Settings OwnedObjectPath(ObjectPath("/org/freedesktop/NetworkManager/Settings/23"))
  Reply: Msg { type: MethodReturn, sender: UniqueName(Str(Borrowed(":1.5"))), reply-serial: 13, body: Signature("a{sa{sv}}") }
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Variant(PaddingNot0(15))', src/main.rs:102:61
stack backtrace:
   0: rust_begin_unwind
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/panicking.rs:142:14
   2: core::result::unwrap_failed
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/result.rs:1814:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/result.rs:1107:23
   4: nm_test::main
             at ./src/main.rs:102:24
   5: core::ops::function::FnOnce::call_once
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/ops/function.rs:248:5
@zeenix zeenix added bug Something isn't working zvariant Issues/PRs related to zvariant crate labels Apr 12, 2024
@jonathanhood

This comment was marked as outdated.

@zeenix

This comment was marked as outdated.

@jonathanhood

This comment was marked as outdated.

@zeenix

This comment was marked as outdated.

@jonathanhood

This comment was marked as outdated.

@zeenix

This comment was marked as outdated.

@jonathanhood

This comment was marked as outdated.

@zeenix

This comment was marked as outdated.

@zeenix zeenix changed the title Nested Dict types 'a{sa{sv}}' are not supported *Dict macros don't support nested dictionaries 'a{sa{sv}}' Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working zvariant Issues/PRs related to zvariant crate
Projects
None yet
Development

No branches or pull requests

2 participants