dbus2 / zbus-old

Rust D-Bus crate.
https://gitlab.freedesktop.org/dbus/zbus
Other
49 stars 13 forks source link

DeserializeDict skipping rename #313

Closed zeenix closed 1 year ago

zeenix commented 1 year ago

In GitLab by @msandova on Jan 3, 2023, 23:17

I have

#[derive(DeserializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
pub struct WallpaperOptions {
    #[zvariant(rename = "show-preview")]
    pub show_preview: Option<bool>,
    #[zvariant(rename = "set-on")]
    pub set_on: Option<SetOn>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash, Type)]
#[zvariant(signature = "s")]
/// Where to set the wallpaper on.
pub enum SetOn {
    /// Set the wallpaper only on the lock-screen.
    Lockscreen,
    /// Set thewallpaper only on the background.
    Background,
    /// Set the wallpaper on both lock-screen and background.
    Both,
}

and when doing

#[dbus_interface(name = "org.freedesktop.impl.portal.Wallpaper")]
impl WallpaperInterface {
    #[dbus_interface(name = "SetWallpaperURI")]
    async fn set_wallpaper_uri(
        &mut self,
        handle: OwnedObjectPath,
        app_id: String,
        window_identifier: WindowIdentifierType,
        uri: url::Url,
        options: WallpaperOptions,
    ) -> Response<BasicResponse> {...}

I am getting None on the SetOn. Note that the portal correctly gets both

(objectpath '/org/freedesktop/portal/desktop/request/1_784/ashpd_rpCjajeCYO', 'com.belmoussaoui.ashpd.demo', 'wayland:]tKzS?"8q?{\\o7.ViQ[SqH9,KDO@:h|=', 'file:///run/user/1000/doc/3806238a/Screenshot%20from%202022-12-02%2018-13-06.png', {'show-preview': <true>, 'set-on': <'both'>})

see https://flatpak.github.io/xdg-desktop-portal/#gdbus-org.freedesktop.impl.portal.Wallpaper

zeenix commented 1 year ago

In GitLab by @msandova on Jan 4, 2023, 18:31

The following test fails

    #[test]
    fn kebap_case() {
        #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash, Type)]
        #[zvariant(signature = "s")]
        /// Where to set the wallpaper on.
        pub enum SetOn {
            /// Set the wallpaper only on the lock-screen.
            Lockscreen,
            /// Set the wallpaper only on the background.
            Background,
            /// Set the wallpaper on both lock-screen and background.
            Both,
        }
        #[derive(DeserializeDict, PartialEq, SerializeDict, Type, Debug)]
        #[zvariant(signature = "dict")]
        pub struct TestDeserializeDictKebabCase {
            #[zvariant(rename = "show-preview")]
            pub show_preview: Option<bool>,
            #[zvariant(rename = "set-on")]
            pub set_on: Option<SetOn>,
        }
        let test = TestDeserializeDictKebabCase {
            show_preview: None,
            set_on: Some(SetOn::Lockscreen)
        };
        let ctxt = Context::<LE>::new_dbus(0);
        let mut map = HashMap::new();
        map.insert("set-on", Value::from("lockscreen"));
        let encoded = to_bytes(ctxt, &map).unwrap();

        let decoded: Result<TestDeserializeDictKebabCase> = from_slice(&encoded, ctxt);
        assert_eq!(
            decoded.unwrap(),
            test
        );
    }

note that using "Lockscreen" instead of "lockstreen" works.

Note that using #[serde(rename_all = "kebab-case")] also fixed the issue, but idk if it should be needed in the first place.

zeenix commented 1 year ago

In GitLab by @msandova on Jan 4, 2023, 18:53

Closing as #[serde(rename_all = "kebab-case")] (or lowercase) fixes it.