1- use dbus:: { blocking :: LocalConnection , channel:: { Channel , BusType } } ;
1+ use dbus:: { channel:: { Channel , BusType } , Message } ;
22use nu_plugin:: LabeledError ;
3- use nu_protocol:: Spanned ;
3+ use nu_protocol:: { Spanned , Value } ;
44
55use crate :: config:: { DbusClientConfig , DbusBusChoice } ;
66
77/// Executes D-Bus actions on a connection, handling nushell types
88pub struct DbusClient {
99 config : DbusClientConfig ,
10- conn : LocalConnection ,
10+ conn : Channel ,
1111}
1212
1313impl DbusClient {
@@ -31,7 +31,7 @@ impl DbusClient {
3131 } ) ?;
3232 Ok ( DbusClient {
3333 config,
34- conn : LocalConnection :: from ( channel)
34+ conn : channel
3535 } )
3636 }
3737
@@ -42,9 +42,16 @@ impl DbusClient {
4242 object : & Spanned < String > ,
4343 interface : & Spanned < String > ,
4444 method : & Spanned < String > ,
45- ) -> Result < ( ) , LabeledError > {
46- // TODO convert & return response
45+ ) -> Result < Value , LabeledError > {
4746 // TODO accept arguments
47+ macro_rules! error {
48+ ( $label: expr) => ( LabeledError {
49+ label: $label,
50+ msg: "while calling a D-Bus method" . into( ) ,
51+ span: Some ( self . config. span)
52+ } )
53+ }
54+
4855 // Validate inputs before sending to the dbus lib so we don't panic
4956 macro_rules! validate_with {
5057 ( $type: ty, $spanned: expr) => ( <$type>:: new( & $spanned. item) . map_err( |msg| {
@@ -60,19 +67,18 @@ impl DbusClient {
6067 let valid_interface = validate_with ! ( dbus:: strings:: Interface , interface) ?;
6168 let valid_method = validate_with ! ( dbus:: strings:: Member , method) ?;
6269
63- // Send method call
64- let proxy = self . conn . with_proxy (
70+ // Construct the method call message
71+ let message = Message :: new_method_call (
6572 valid_dest,
6673 valid_object,
67- self . config . timeout . item
68- ) ;
69- let ( ) = proxy. method_call ( valid_interface, valid_method, ( ) ) . map_err ( |err| {
70- LabeledError {
71- label : err. to_string ( ) ,
72- msg : "while calling a D-Bus method" . into ( ) ,
73- span : Some ( self . config . span ) ,
74- }
75- } ) ?;
76- Ok ( ( ) )
74+ valid_interface,
75+ valid_method,
76+ ) . map_err ( |err| error ! ( err) ) ?;
77+
78+ // Send it on the channel and get the response
79+ let resp = self . conn . send_with_reply_and_block ( message, self . config . timeout . item )
80+ . map_err ( |err| error ! ( err. to_string( ) ) ) ?;
81+
82+ Ok ( crate :: convert:: from_message ( & resp) ?)
7783 }
7884}
0 commit comments