1- use dbus:: { channel:: { Channel , BusType } , Message , arg:: messageitem:: MessageItem } ;
1+ use dbus:: {
2+ arg:: messageitem:: MessageItem ,
3+ channel:: { BusType , Channel } ,
4+ Message ,
5+ } ;
26use nu_plugin:: LabeledError ;
37use nu_protocol:: { Spanned , Value } ;
48
5- use crate :: { config:: { DbusClientConfig , DbusBusChoice } , dbus_type:: DbusType , convert:: to_message_item, introspection:: Node , pattern:: Pattern } ;
9+ use crate :: {
10+ config:: { DbusBusChoice , DbusClientConfig } ,
11+ convert:: to_message_item,
12+ dbus_type:: DbusType ,
13+ introspection:: Node ,
14+ pattern:: Pattern ,
15+ } ;
616
717/// Executes D-Bus actions on a connection, handling nushell types
818pub struct DbusClient {
@@ -12,13 +22,13 @@ pub struct DbusClient {
1222
1323// Convenience macros for error handling
1424macro_rules! validate_with {
15- ( $type: ty, $spanned: expr) => ( <$type> :: new ( & $spanned . item ) . map_err ( |msg| {
16- LabeledError {
25+ ( $type: ty, $spanned: expr) => {
26+ <$type> :: new ( & $spanned . item ) . map_err ( |msg| LabeledError {
1727 label: msg,
1828 msg: "this argument is incorrect" . into( ) ,
1929 span: Some ( $spanned. span) ,
20- }
21- } ) )
30+ } )
31+ } ;
2232}
2333
2434impl DbusClient {
@@ -33,24 +43,23 @@ impl DbusClient {
3343 ch. register ( ) ?;
3444 Ok ( ch)
3545 } ) ,
36- } . map_err ( |err| {
37- LabeledError {
38- label : err. to_string ( ) ,
39- msg : "while connecting to D-Bus as specified here" . into ( ) ,
40- span : Some ( config. bus_choice . span ) ,
41- }
46+ }
47+ . map_err ( |err| LabeledError {
48+ label : err. to_string ( ) ,
49+ msg : "while connecting to D-Bus as specified here" . into ( ) ,
50+ span : Some ( config. bus_choice . span ) ,
4251 } ) ?;
4352 Ok ( DbusClient {
4453 config,
45- conn : channel
54+ conn : channel,
4655 } )
4756 }
4857
4958 fn error ( & self , err : impl std:: fmt:: Display , msg : impl std:: fmt:: Display ) -> LabeledError {
5059 LabeledError {
5160 label : err. to_string ( ) ,
5261 msg : msg. to_string ( ) ,
53- span : Some ( self . config . span )
62+ span : Some ( self . config . span ) ,
5463 }
5564 }
5665
@@ -69,15 +78,19 @@ impl DbusClient {
6978 valid_dest,
7079 valid_object,
7180 "org.freedesktop.DBus.Introspectable" ,
72- "Introspect"
73- ) . map_err ( |err| self . error ( err, context) ) ?;
81+ "Introspect" ,
82+ )
83+ . map_err ( |err| self . error ( err, context) ) ?;
7484
7585 // Send and get the response
76- let resp = self . conn . send_with_reply_and_block ( message, self . config . timeout . item )
86+ let resp = self
87+ . conn
88+ . send_with_reply_and_block ( message, self . config . timeout . item )
7789 . map_err ( |err| self . error ( err, context) ) ?;
7890
7991 // Parse it to a Node
80- let xml: & str = resp. get1 ( )
92+ let xml: & str = resp
93+ . get1 ( )
8194 . ok_or_else ( || self . error ( "Introspect method returned the wrong type" , context) ) ?;
8295
8396 Node :: from_xml ( xml) . map_err ( |err| self . error ( err, context) )
@@ -95,10 +108,10 @@ impl DbusClient {
95108
96109 if let Some ( sig) = node. get_method_args_signature ( & interface. item , & method. item ) {
97110 DbusType :: parse_all ( & sig) . map_err ( |err| LabeledError {
98- label : format ! ( "while getting interface {:?} method {:?} signature: {}" ,
99- interface. item ,
100- method. item,
101- err ) ,
111+ label : format ! (
112+ "while getting interface {:?} method {:?} signature: {}" ,
113+ interface . item , method. item, err
114+ ) ,
102115 msg : "try running with --no-introspect or --signature" . into ( ) ,
103116 span : Some ( self . config . span ) ,
104117 } )
@@ -122,17 +135,20 @@ impl DbusClient {
122135 let node = self . introspect ( dest, object) ?;
123136
124137 if let Some ( sig) = node. get_property_signature ( & interface. item , & property. item ) {
125- DbusType :: parse_all ( & sig) . map_err ( |err| LabeledError {
126- label : format ! ( "while getting interface {:?} property {:?} signature: {}" ,
127- interface. item ,
128- property. item,
129- err ) ,
138+ DbusType :: parse_all ( sig) . map_err ( |err| LabeledError {
139+ label : format ! (
140+ "while getting interface {:?} property {:?} signature: {}" ,
141+ interface . item , property. item, err
142+ ) ,
130143 msg : "try running with --no-introspect or --signature" . into ( ) ,
131144 span : Some ( self . config . span ) ,
132145 } )
133146 } else {
134147 Err ( LabeledError {
135- label : format ! ( "Property {:?} not found on {:?}" , property. item, interface. item) ,
148+ label : format ! (
149+ "Property {:?} not found on {:?}" ,
150+ property. item, interface. item
151+ ) ,
136152 msg : "check that this property/interface is correct" . into ( ) ,
137153 span : Some ( property. span ) ,
138154 } )
@@ -158,52 +174,61 @@ impl DbusClient {
158174 let valid_method = validate_with ! ( dbus:: strings:: Member , method) ?;
159175
160176 // Parse the signature
161- let mut valid_signature = signature. map ( |s| DbusType :: parse_all ( & s. item ) . map_err ( |err| {
162- LabeledError {
163- label : err,
164- msg : "in signature specified here" . into ( ) ,
165- span : Some ( s. span ) ,
166- }
167- } ) ) . transpose ( ) ?;
177+ let mut valid_signature = signature
178+ . map ( |s| {
179+ DbusType :: parse_all ( & s. item ) . map_err ( |err| LabeledError {
180+ label : err,
181+ msg : "in signature specified here" . into ( ) ,
182+ span : Some ( s. span ) ,
183+ } )
184+ } )
185+ . transpose ( ) ?;
168186
169187 // If not provided, try introspection (unless disabled)
170188 if valid_signature. is_none ( ) && self . config . introspect {
171189 match self . get_method_signature_by_introspection ( dest, object, interface, method) {
172190 Ok ( sig) => {
173191 valid_signature = Some ( sig) ;
174- } ,
192+ }
175193 Err ( err) => {
176- eprintln ! ( "Warning: D-Bus introspection failed on {:?}. \
194+ eprintln ! (
195+ "Warning: D-Bus introspection failed on {:?}. \
177196 Use `--no-introspect` or pass `--signature` to silence this warning. \
178197 Cause: {}",
179- object. item,
180- err . label ) ;
198+ object. item, err . label
199+ ) ;
181200 }
182201 }
183202 }
184203
185204 if let Some ( sig) = & valid_signature {
186205 if sig. len ( ) != args. len ( ) {
187- self . error ( format ! ( "expected {} arguments, got {}" , sig. len( ) , args. len( ) ) , context) ;
206+ self . error (
207+ format ! ( "expected {} arguments, got {}" , sig. len( ) , args. len( ) ) ,
208+ context,
209+ ) ;
188210 }
189211 }
190212
191213 // Construct the method call message
192- let mut message = Message :: new_method_call (
193- valid_dest,
194- valid_object,
195- valid_interface,
196- valid_method,
197- ) . map_err ( |err| self . error ( err, context) ) ?;
214+ let mut message =
215+ Message :: new_method_call ( valid_dest, valid_object, valid_interface, valid_method)
216+ . map_err ( |err| self . error ( err, context) ) ?;
198217
199218 // Convert the args to message items
200- let sigs_iter = valid_signature. iter ( ) . flatten ( ) . map ( Some ) . chain ( std:: iter:: repeat ( None ) ) ;
219+ let sigs_iter = valid_signature
220+ . iter ( )
221+ . flatten ( )
222+ . map ( Some )
223+ . chain ( std:: iter:: repeat ( None ) ) ;
201224 for ( val, sig) in args. iter ( ) . zip ( sigs_iter) {
202225 message = message. append1 ( to_message_item ( val, sig) ?) ;
203226 }
204227
205228 // Send it on the channel and get the response
206- let resp = self . conn . send_with_reply_and_block ( message, self . config . timeout . item )
229+ let resp = self
230+ . conn
231+ . send_with_reply_and_block ( message, self . config . timeout . item )
207232 . map_err ( |err| self . error ( err, context) ) ?;
208233
209234 crate :: convert:: from_message ( & resp, self . config . span )
@@ -224,11 +249,21 @@ impl DbusClient {
224249 self . call (
225250 dest,
226251 object,
227- & Spanned { item : "org.freedesktop.DBus.Properties" . into ( ) , span : self . config . span } ,
228- & Spanned { item : "Get" . into ( ) , span : self . config . span } ,
229- Some ( & Spanned { item : "ss" . into ( ) , span : self . config . span } ) ,
230- & [ interface_val, property_val]
231- ) . map ( |val| val. into_iter ( ) . nth ( 0 ) . unwrap_or_default ( ) )
252+ & Spanned {
253+ item : "org.freedesktop.DBus.Properties" . into ( ) ,
254+ span : self . config . span ,
255+ } ,
256+ & Spanned {
257+ item : "Get" . into ( ) ,
258+ span : self . config . span ,
259+ } ,
260+ Some ( & Spanned {
261+ item : "ss" . into ( ) ,
262+ span : self . config . span ,
263+ } ) ,
264+ & [ interface_val, property_val] ,
265+ )
266+ . map ( |val| val. into_iter ( ) . nth ( 0 ) . unwrap_or_default ( ) )
232267 }
233268
234269 /// Get all D-Bus properties from the given object
@@ -243,11 +278,21 @@ impl DbusClient {
243278 self . call (
244279 dest,
245280 object,
246- & Spanned { item : "org.freedesktop.DBus.Properties" . into ( ) , span : self . config . span } ,
247- & Spanned { item : "GetAll" . into ( ) , span : self . config . span } ,
248- Some ( & Spanned { item : "s" . into ( ) , span : self . config . span } ) ,
249- & [ interface_val]
250- ) . map ( |val| val. into_iter ( ) . nth ( 0 ) . unwrap_or_default ( ) )
281+ & Spanned {
282+ item : "org.freedesktop.DBus.Properties" . into ( ) ,
283+ span : self . config . span ,
284+ } ,
285+ & Spanned {
286+ item : "GetAll" . into ( ) ,
287+ span : self . config . span ,
288+ } ,
289+ Some ( & Spanned {
290+ item : "s" . into ( ) ,
291+ span : self . config . span ,
292+ } ) ,
293+ & [ interface_val] ,
294+ )
295+ . map ( |val| val. into_iter ( ) . nth ( 0 ) . unwrap_or_default ( ) )
251296 }
252297
253298 /// Set a D-Bus property on the given object
@@ -267,34 +312,42 @@ impl DbusClient {
267312 let valid_object = validate_with ! ( dbus:: strings:: Path , object) ?;
268313
269314 // Parse the signature
270- let mut valid_signature = signature. map ( |s| DbusType :: parse_all ( & s. item ) . map_err ( |err| {
271- LabeledError {
272- label : err,
273- msg : "in signature specified here" . into ( ) ,
274- span : Some ( s. span ) ,
275- }
276- } ) ) . transpose ( ) ?;
315+ let mut valid_signature = signature
316+ . map ( |s| {
317+ DbusType :: parse_all ( & s. item ) . map_err ( |err| LabeledError {
318+ label : err,
319+ msg : "in signature specified here" . into ( ) ,
320+ span : Some ( s. span ) ,
321+ } )
322+ } )
323+ . transpose ( ) ?;
277324
278325 // If not provided, try introspection (unless disabled)
279326 if valid_signature. is_none ( ) && self . config . introspect {
280327 match self . get_property_signature_by_introspection ( dest, object, interface, property) {
281328 Ok ( sig) => {
282329 valid_signature = Some ( sig) ;
283- } ,
330+ }
284331 Err ( err) => {
285- eprintln ! ( "Warning: D-Bus introspection failed on {:?}. \
332+ eprintln ! (
333+ "Warning: D-Bus introspection failed on {:?}. \
286334 Use `--no-introspect` or pass `--signature` to silence this warning. \
287335 Cause: {}",
288- object. item,
289- err . label ) ;
336+ object. item, err . label
337+ ) ;
290338 }
291339 }
292340 }
293341
294342 if let Some ( sig) = & valid_signature {
295343 if sig. len ( ) != 1 {
296- self . error ( format ! (
297- "expected single object signature, but there are {}" , sig. len( ) ) , context) ;
344+ self . error (
345+ format ! (
346+ "expected single object signature, but there are {}" ,
347+ sig. len( )
348+ ) ,
349+ context,
350+ ) ;
298351 }
299352 }
300353
@@ -304,40 +357,47 @@ impl DbusClient {
304357 valid_object,
305358 "org.freedesktop.DBus.Properties" ,
306359 "Set" ,
307- ) . map_err ( |err| self . error ( err, context) ) ?
308- . append2 ( & interface. item , & property. item )
309- . append1 (
310- // Box it in a variant as required for property setting
311- MessageItem :: Variant ( Box :: new (
312- to_message_item ( value, valid_signature. as_ref ( ) . map ( |s| & s[ 0 ] ) ) ?) )
313- ) ;
360+ )
361+ . map_err ( |err| self . error ( err, context) ) ?
362+ . append2 ( & interface. item , & property. item )
363+ . append1 (
364+ // Box it in a variant as required for property setting
365+ MessageItem :: Variant ( Box :: new ( to_message_item (
366+ value,
367+ valid_signature. as_ref ( ) . map ( |s| & s[ 0 ] ) ,
368+ ) ?) ) ,
369+ ) ;
314370
315371 // Send it on the channel and get the response
316- self . conn . send_with_reply_and_block ( message, self . config . timeout . item )
372+ self . conn
373+ . send_with_reply_and_block ( message, self . config . timeout . item )
317374 . map_err ( |err| self . error ( err, context) ) ?;
318375
319376 Ok ( ( ) )
320377 }
321378
322- pub fn list ( & self , pattern : Option < & Pattern > )
323- -> Result < Vec < String > , LabeledError >
324- {
379+ pub fn list ( & self , pattern : Option < & Pattern > ) -> Result < Vec < String > , LabeledError > {
325380 let context = "while listing D-Bus connection names" ;
326381
327382 let message = Message :: new_method_call (
328383 "org.freedesktop.DBus" ,
329384 "/org/freedesktop/DBus" ,
330385 "org.freedesktop.DBus" ,
331- "ListNames"
332- ) . map_err ( |err| self . error ( err, context) ) ?;
386+ "ListNames" ,
387+ )
388+ . map_err ( |err| self . error ( err, context) ) ?;
333389
334- self . conn . send_with_reply_and_block ( message, self . config . timeout . item )
390+ self . conn
391+ . send_with_reply_and_block ( message, self . config . timeout . item )
335392 . map_err ( |err| self . error ( err, context) )
336393 . and_then ( |reply| reply. read1 ( ) . map_err ( |err| self . error ( err, context) ) )
337394 . map ( |names : Vec < String > | {
338395 // Filter the names by the pattern
339396 if let Some ( pattern) = pattern {
340- names. into_iter ( ) . filter ( |name| pattern. is_match ( name) ) . collect ( )
397+ names
398+ . into_iter ( )
399+ . filter ( |name| pattern. is_match ( name) )
400+ . collect ( )
341401 } else {
342402 names
343403 }
0 commit comments