2020
2121//! Implements a client to access Agama's storage service.
2222
23- use agama_utils :: api :: storage :: Config ;
23+ use crate :: config :: Config ;
2424use serde_json:: { value:: RawValue , Value } ;
2525use zbus:: { names:: BusName , zvariant:: OwnedObjectPath , Connection , Message } ;
2626
@@ -78,9 +78,7 @@ impl Client {
7878
7979 pub async fn get_config ( & self ) -> Result < Option < Config > , Error > {
8080 let message = self . call ( "GetConfig" , & ( ) ) . await ?;
81- let value: String = message. body ( ) . deserialize ( ) ?;
82- let config = serde_json:: from_str ( value. as_str ( ) ) ?;
83- Ok ( config)
81+ self . json_from ( message)
8482 }
8583
8684 pub async fn get_config_model ( & self ) -> Result < Option < Box < RawValue > > , Error > {
@@ -104,8 +102,10 @@ impl Client {
104102 Ok ( ( ) )
105103 }
106104
107- pub async fn set_config ( & self , config : Box < RawValue > ) -> Result < ( ) , Error > {
108- self . call ( "SetConfig" , & ( config. to_string ( ) ) ) . await ?;
105+ pub async fn set_config ( & self , config : Option < Config > ) -> Result < ( ) , Error > {
106+ let config = config. filter ( |c| c. is_some ( ) ) ;
107+ let json = serde_json:: to_string ( & config) ?;
108+ self . call ( "SetConfig" , & ( json) ) . await ?;
109109 Ok ( ( ) )
110110 }
111111
@@ -140,17 +140,21 @@ impl Client {
140140 . map_err ( |e| e. into ( ) )
141141 }
142142
143- fn json_from ( & self , message : Message ) -> Result < Option < Box < RawValue > > , Error > {
143+ fn json_from < T : serde:: de:: DeserializeOwned > (
144+ & self ,
145+ message : Message ,
146+ ) -> Result < Option < T > , Error > {
144147 let value: String = message. body ( ) . deserialize ( ) ?;
145148 if self . is_null ( value. as_str ( ) ) {
146149 return Ok ( None ) ;
147150 }
148- let json = RawValue :: from_string ( value) ?;
151+ let json = serde_json :: from_str ( value. as_str ( ) ) ?;
149152 Ok ( Some ( json) )
150153 }
151154
152155 fn is_null ( & self , value : & str ) -> bool {
153- match serde_json:: from_str :: < Value > ( value) {
156+ let value = serde_json:: from_str :: < Value > ( value) ;
157+ match value {
154158 Ok ( Value :: Null ) => true ,
155159 Ok ( _) => false ,
156160 Err ( _) => false ,
0 commit comments