33
44//! Tauri commands for interacting with the MIDIMon daemon
55
6+ use crate :: config_helpers:: { config_to_json, generate_mapping_toml, suggestion_to_config} ;
7+ use crate :: device_templates:: { DeviceTemplate , DeviceTemplateRegistry } ;
8+ use crate :: midi_learn:: { LearnSessionState , MidiLearnResult , MidiLearnSession , TriggerSuggestion } ;
9+ use crate :: state:: AppState ;
610use serde:: { Deserialize , Serialize } ;
711use serde_json:: json;
812use tauri:: State ;
913use uuid:: Uuid ;
10- use crate :: state:: AppState ;
11- use crate :: midi_learn:: { MidiLearnSession , MidiLearnResult , LearnSessionState , TriggerSuggestion } ;
12- use crate :: config_helpers:: { suggestion_to_config, generate_mapping_toml, config_to_json} ;
13- use crate :: device_templates:: { DeviceTemplate , DeviceTemplateRegistry } ;
1414
1515// Import daemon types (we'll re-export these from daemon crate)
16- use midimon_daemon:: daemon:: {
17- IpcClient , IpcCommand , IpcRequest , ResponseStatus ,
18- } ;
16+ use midimon_daemon:: daemon:: { IpcClient , IpcCommand , IpcRequest , ResponseStatus } ;
1917
2018/// Daemon status information for UI
2119#[ derive( Debug , Serialize , Deserialize ) ]
@@ -71,24 +69,30 @@ pub async fn get_daemon_status(state: State<'_, AppState>) -> Result<DaemonStatu
7169
7270 // Parse daemon info from response
7371 if let Some ( data) = response. data {
74- let lifecycle_state = data. get ( "daemon" )
72+ let lifecycle_state = data
73+ . get ( "daemon" )
7574 . and_then ( |d| d. get ( "lifecycle_state" ) )
7675 . and_then ( |s| s. as_str ( ) )
7776 . map ( String :: from) ;
7877
79- let uptime_secs = data. get ( "daemon" )
78+ let uptime_secs = data
79+ . get ( "daemon" )
8080 . and_then ( |d| d. get ( "uptime_seconds" ) )
8181 . and_then ( |u| u. as_u64 ( ) ) ;
8282
83- let events_processed = data. get ( "statistics" )
83+ let events_processed = data
84+ . get ( "statistics" )
8485 . and_then ( |s| s. get ( "events_processed" ) )
8586 . and_then ( |e| e. as_u64 ( ) ) ;
8687
8788 let device = data. get ( "device" ) . and_then ( |d| {
8889 Some ( DeviceInfo {
8990 connected : d. get ( "connected" ) ?. as_bool ( ) ?,
9091 name : d. get ( "name" ) . and_then ( |n| n. as_str ( ) ) . map ( String :: from) ,
91- port : d. get ( "port" ) . and_then ( |p| p. as_u64 ( ) ) . map ( |p| p as usize ) ,
92+ port : d
93+ . get ( "port" )
94+ . and_then ( |p| p. as_u64 ( ) )
95+ . map ( |p| p as usize ) ,
9296 } )
9397 } ) ;
9498
@@ -113,7 +117,8 @@ pub async fn get_daemon_status(state: State<'_, AppState>) -> Result<DaemonStatu
113117 } )
114118 }
115119 } else {
116- let error_msg = response. error
120+ let error_msg = response
121+ . error
117122 . map ( |e| e. message )
118123 . unwrap_or_else ( || "Unknown error" . to_string ( ) ) ;
119124
@@ -237,22 +242,26 @@ pub async fn validate_config(_state: State<'_, AppState>) -> Result<ConfigValida
237242 ResponseStatus :: Success => {
238243 // Parse validation result from response data
239244 if let Some ( data) = response. data {
240- let valid = data. get ( "valid" )
241- . and_then ( |v| v. as_bool ( ) )
242- . unwrap_or ( true ) ;
245+ let valid = data. get ( "valid" ) . and_then ( |v| v. as_bool ( ) ) . unwrap_or ( true ) ;
243246
244- let errors = data. get ( "errors" )
247+ let errors = data
248+ . get ( "errors" )
245249 . and_then ( |e| e. as_array ( ) )
246- . map ( |arr| arr. iter ( )
247- . filter_map ( |v| v. as_str ( ) . map ( String :: from) )
248- . collect ( ) )
250+ . map ( |arr| {
251+ arr. iter ( )
252+ . filter_map ( |v| v. as_str ( ) . map ( String :: from) )
253+ . collect ( )
254+ } )
249255 . unwrap_or_default ( ) ;
250256
251- let warnings = data. get ( "warnings" )
257+ let warnings = data
258+ . get ( "warnings" )
252259 . and_then ( |w| w. as_array ( ) )
253- . map ( |arr| arr. iter ( )
254- . filter_map ( |v| v. as_str ( ) . map ( String :: from) )
255- . collect ( ) )
260+ . map ( |arr| {
261+ arr. iter ( )
262+ . filter_map ( |v| v. as_str ( ) . map ( String :: from) )
263+ . collect ( )
264+ } )
256265 . unwrap_or_default ( ) ;
257266
258267 Ok ( ConfigValidation {
@@ -373,8 +382,8 @@ pub async fn save_config(
373382#[ tauri:: command]
374383pub async fn get_config_path ( ) -> Result < String , String > {
375384 // Use the same logic as daemon to find config path
376- let config_dir = dirs :: config_dir ( )
377- . ok_or_else ( || "Failed to determine config directory" . to_string ( ) ) ?;
385+ let config_dir =
386+ dirs :: config_dir ( ) . ok_or_else ( || "Failed to determine config directory" . to_string ( ) ) ?;
378387
379388 let config_path = config_dir. join ( "midimon" ) . join ( "config.toml" ) ;
380389
@@ -398,7 +407,9 @@ pub async fn start_midi_learn(
398407
399408/// Get the status of the current MIDI Learn session
400409#[ tauri:: command]
401- pub async fn get_midi_learn_status ( state : State < ' _ , AppState > ) -> Result < LearnSessionState , String > {
410+ pub async fn get_midi_learn_status (
411+ state : State < ' _ , AppState > ,
412+ ) -> Result < LearnSessionState , String > {
402413 let session = state. get_learn_session ( ) . await ;
403414 match session {
404415 Some ( s) => Ok ( s. get_state ( ) . await ) ,
@@ -428,7 +439,9 @@ pub async fn cancel_midi_learn(state: State<'_, AppState>) -> Result<(), String>
428439
429440/// Get the result of the MIDI Learn session
430441#[ tauri:: command]
431- pub async fn get_midi_learn_result ( state : State < ' _ , AppState > ) -> Result < Option < MidiLearnResult > , String > {
442+ pub async fn get_midi_learn_result (
443+ state : State < ' _ , AppState > ,
444+ ) -> Result < Option < MidiLearnResult > , String > {
432445 let session = state. get_learn_session ( ) . await ;
433446 match session {
434447 Some ( s) => {
@@ -437,7 +450,7 @@ pub async fn get_midi_learn_result(state: State<'_, AppState>) -> Result<Option<
437450 s. set_timed_out ( ) . await ;
438451 }
439452 Ok ( s. get_result ( ) . await )
440- } ,
453+ }
441454 None => Ok ( None ) ,
442455 }
443456}
@@ -454,14 +467,18 @@ pub fn generate_trigger_config_toml(
454467
455468/// Convert trigger suggestion to JSON config
456469#[ tauri:: command]
457- pub fn trigger_suggestion_to_json ( suggestion : TriggerSuggestion ) -> Result < serde_json:: Value , String > {
470+ pub fn trigger_suggestion_to_json (
471+ suggestion : TriggerSuggestion ,
472+ ) -> Result < serde_json:: Value , String > {
458473 let config = suggestion_to_config ( & suggestion) ;
459474 Ok ( config_to_json ( & config) )
460475}
461476
462477/// Get the current frontmost application
463478#[ tauri:: command]
464- pub async fn get_frontmost_app ( state : State < ' _ , AppState > ) -> Result < Option < crate :: app_detection:: AppInfo > , String > {
479+ pub async fn get_frontmost_app (
480+ state : State < ' _ , AppState > ,
481+ ) -> Result < Option < crate :: app_detection:: AppInfo > , String > {
465482 Ok ( state. get_current_app ( ) . await )
466483}
467484
@@ -481,7 +498,9 @@ pub async fn stop_app_monitoring(state: State<'_, AppState>) -> Result<(), Strin
481498
482499/// List all registered profiles
483500#[ tauri:: command]
484- pub async fn list_profiles ( state : State < ' _ , AppState > ) -> Result < Vec < crate :: profile_manager:: AppProfile > , String > {
501+ pub async fn list_profiles (
502+ state : State < ' _ , AppState > ,
503+ ) -> Result < Vec < crate :: profile_manager:: AppProfile > , String > {
485504 let manager = state. get_profile_manager ( ) . await ;
486505 Ok ( manager. list_profiles ( ) . await )
487506}
@@ -625,9 +644,7 @@ pub fn list_templates_by_category(category: String) -> Result<Vec<DeviceTemplate
625644
626645/// Create a config from a template
627646#[ tauri:: command]
628- pub fn create_config_from_template (
629- template_id : String ,
630- ) -> Result < String , String > {
647+ pub fn create_config_from_template ( template_id : String ) -> Result < String , String > {
631648 let registry = DeviceTemplateRegistry :: new ( ) ;
632649 registry. create_config_from_template ( & template_id)
633650}
0 commit comments