@@ -11,6 +11,7 @@ use std::path::{Path, PathBuf};
1111use std:: { collections:: HashSet , io} ;
1212use thiserror:: Error ;
1313use toml;
14+ use toml_edit:: { self , DocumentMut } ;
1415
1516use lan_mouse_cli:: CliArgs ;
1617use lan_mouse_ipc:: { Position , DEFAULT_PORT } ;
@@ -44,7 +45,7 @@ fn default_path() -> Result<PathBuf, VarError> {
4445 Ok ( PathBuf :: from ( default_path) )
4546}
4647
47- #[ derive( Serialize , Deserialize , Debug , Default ) ]
48+ #[ derive( Serialize , Deserialize , Clone , Debug , Default ) ]
4849struct ConfigToml {
4950 capture_backend : Option < CaptureBackend > ,
5051 emulation_backend : Option < EmulationBackend > ,
@@ -414,15 +415,21 @@ impl Config {
414415
415416 /// set configured clients
416417 pub fn set_clients ( & mut self , clients : Vec < ConfigClient > ) {
418+ if clients. is_empty ( ) {
419+ return ;
420+ }
417421 if self . config_toml . is_none ( ) {
418422 self . config_toml = Default :: default ( ) ;
419423 }
420424 self . config_toml . as_mut ( ) . expect ( "config" ) . clients =
421- clients. into_iter ( ) . map ( |c| c. into ( ) ) . collect :: < Vec < _ > > ( ) ;
425+ Some ( clients. into_iter ( ) . map ( |c| c. into ( ) ) . collect :: < Vec < _ > > ( ) ) ;
422426 }
423427
424428 /// set authorized keys
425429 pub fn set_authorized_keys ( & mut self , fingerprints : HashMap < String , String > ) {
430+ if fingerprints. is_empty ( ) {
431+ return ;
432+ }
426433 if self . config_toml . is_none ( ) {
427434 self . config_toml = Default :: default ( ) ;
428435 }
@@ -432,7 +439,24 @@ impl Config {
432439 . authorized_fingerprints = Some ( fingerprints) ;
433440 }
434441
435- pub fn write_back ( & self ) {
436- todo ! ( )
442+ pub fn write_back ( & self ) -> Result < ( ) , io:: Error > {
443+ log:: info!( "writing config to {:?}" , & self . config_path) ;
444+ /* load the current configuration file */
445+ let current_config = fs:: read_to_string ( & self . config_path ) ?;
446+ let current_config = current_config. parse :: < DocumentMut > ( ) . expect ( "fix me" ) ;
447+ let _current_config =
448+ toml_edit:: de:: from_document :: < ConfigToml > ( current_config) . expect ( "fixme" ) ;
449+
450+ /* the new config */
451+ let new_config = self . config_toml . clone ( ) . unwrap_or_default ( ) ;
452+ // let new_config = toml_edit::ser::to_document::<ConfigToml>(&new_config).expect("fixme");
453+ let new_config = toml_edit:: ser:: to_string_pretty ( & new_config) . expect ( "todo" ) ;
454+
455+ /* TODO merge documents */
456+
457+ /* write new config to file */
458+ fs:: write ( & self . config_path , new_config) ?;
459+
460+ Ok ( ( ) )
437461 }
438462}
0 commit comments