@@ -5,7 +5,7 @@ use std::collections::HashMap;
55use serde:: {
66 Deserialize , Serialize ,
77 de:: { DeserializeSeed , Error } ,
8- ser:: { Error as _, SerializeTuple } ,
8+ ser:: Error as _,
99} ;
1010use zvariant:: {
1111 self , Array , DeserializeDict , DynamicDeserialize , LE , Optional , OwnedValue , SerializeDict ,
@@ -27,25 +27,8 @@ impl Serialize for BackgroundEvent {
2727 where
2828 S : serde:: Serializer ,
2929 {
30- let mut tuple = serializer. serialize_tuple ( 3 ) ?;
31- match self {
32- Self :: UsbStateChanged ( state) => {
33- tuple. serialize_element ( & 0x01_u8 ) ?;
34-
35- let structure: Structure < ' _ > = state. into ( ) ;
36- tuple. serialize_element ( & Value :: Structure ( structure) ) ?;
37- }
38- Self :: HybridQrStateChanged ( state) => {
39- tuple. serialize_element ( & 0x02_u8 ) ?;
40- let structure: Structure < ' _ > = state. try_into ( ) . map_err ( |err| {
41- S :: Error :: custom ( format ! (
42- "could not convert HybridState to a structure: {err}"
43- ) )
44- } ) ?;
45- tuple. serialize_element ( & Value :: Structure ( structure) ) ?;
46- }
47- } ;
48- tuple. end ( )
30+ let structure: Structure = self . into ( ) ;
31+ structure. serialize ( serializer)
4932 }
5033}
5134
@@ -60,37 +43,45 @@ impl<'de> Deserialize<'de> for BackgroundEvent {
6043 ) )
6144 } ) ?;
6245 let structure = d. deserialize ( deserializer) ?;
63- let ( tag, value) = parse_tag_value_struct ( & structure) . map_err ( |err| {
64- D :: Error :: custom ( format ! ( "could not parse structure as tag-value: {err}" ) )
65- } ) ?;
46+ ( & structure) . try_into ( ) . map_err ( |err| {
47+ D :: Error :: custom ( format ! (
48+ "could not deserialize structure into BackgroundEvent: {err}"
49+ ) )
50+ } )
51+ }
52+ }
53+
54+ impl From < & BackgroundEvent > for Structure < ' _ > {
55+ fn from ( value : & BackgroundEvent ) -> Self {
56+ match value {
57+ BackgroundEvent :: UsbStateChanged ( state) => {
58+ tag_value_to_struct ( 0x01 , Some ( Value :: Structure ( state. into ( ) ) ) )
59+ }
60+ BackgroundEvent :: HybridQrStateChanged ( state) => {
61+ tag_value_to_struct ( 0x02 , Some ( Value :: Structure ( state. into ( ) ) ) )
62+ }
63+ }
64+ }
65+ }
66+
67+ impl TryFrom < & Structure < ' _ > > for BackgroundEvent {
68+ type Error = zvariant:: Error ;
69+
70+ fn try_from ( value : & Structure < ' _ > ) -> Result < Self , Self :: Error > {
71+ let ( tag, value) = parse_tag_value_struct ( value) ?;
72+
6673 match tag {
6774 0x01 => {
68- let inner: Structure = value. downcast_ref ( ) . map_err ( |err| {
69- D :: Error :: custom ( format ! (
70- "could not deserialize inner value {value} as struct: {err}"
71- ) )
72- } ) ?;
73- let state = crate :: model:: UsbState :: try_from ( & inner) . map_err ( |err| {
74- D :: Error :: custom ( format ! (
75- "could not deserialize UsbState from structure: {err}"
76- ) )
77- } ) ?;
78- Ok ( BackgroundEvent :: UsbStateChanged ( state) )
75+ let structure: Structure = value. downcast_ref ( ) ?;
76+ Ok ( BackgroundEvent :: UsbStateChanged ( ( & structure) . try_into ( ) ?) )
7977 }
8078 0x02 => {
81- let inner: Structure = value. downcast_ref ( ) . map_err ( |err| {
82- D :: Error :: custom ( format ! (
83- "could not deserialize inner value {value} as struct: {err}"
84- ) )
85- } ) ?;
86- let state = crate :: model:: HybridState :: try_from ( & inner) . map_err ( |err| {
87- D :: Error :: custom ( format ! (
88- "could not deserialize HybridState from structure: {err}"
89- ) )
90- } ) ?;
91- Ok ( BackgroundEvent :: HybridQrStateChanged ( state) )
79+ let structure: Structure = value. downcast_ref ( ) ?;
80+ Ok ( BackgroundEvent :: HybridQrStateChanged (
81+ ( & structure) . try_into ( ) ?,
82+ ) )
9283 }
93- _ => Err ( D :: Error :: custom ( format ! (
84+ _ => Err ( zvariant :: Error :: Message ( format ! (
9485 "Unknown BackgroundEvent tag : {tag}"
9586 ) ) ) ,
9687 }
0 commit comments