Nushell plugin for interacting with D-Bus
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

pass span through command to returned Values rather than Span::unknown

+30 -29
+11 -10
src/client.rs
··· 1 1 use dbus::{channel::{Channel, BusType}, Message, arg::messageitem::MessageItem}; 2 2 use nu_plugin::LabeledError; 3 - use nu_protocol::{Spanned, Value, Span}; 3 + use nu_protocol::{Spanned, Value}; 4 4 5 5 use crate::{config::{DbusClientConfig, DbusBusChoice}, dbus_type::DbusType, convert::to_message_item, introspection::Node}; 6 6 ··· 206 206 let resp = self.conn.send_with_reply_and_block(message, self.config.timeout.item) 207 207 .map_err(|err| self.error(err, context))?; 208 208 209 - crate::convert::from_message(&resp).map_err(|err| self.error(err, context)) 209 + crate::convert::from_message(&resp, self.config.span) 210 + .map_err(|err| self.error(err, context)) 210 211 } 211 212 212 213 /// Get a D-Bus property from the given object ··· 223 224 self.call( 224 225 dest, 225 226 object, 226 - &Spanned { item: "org.freedesktop.DBus.Properties".into(), span: Span::unknown() }, 227 - &Spanned { item: "Get".into(), span: Span::unknown() }, 228 - Some(&Spanned { item: "ss".into(), span: Span::unknown() }), 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 }), 229 230 &[interface_val, property_val] 230 - ).map(|val| val.into_iter().nth(0).unwrap_or(Value::nothing(Span::unknown()))) 231 + ).map(|val| val.into_iter().nth(0).unwrap_or_default()) 231 232 } 232 233 233 234 /// Get all D-Bus properties from the given object ··· 242 243 self.call( 243 244 dest, 244 245 object, 245 - &Spanned { item: "org.freedesktop.DBus.Properties".into(), span: Span::unknown() }, 246 - &Spanned { item: "GetAll".into(), span: Span::unknown() }, 247 - Some(&Spanned { item: "s".into(), span: Span::unknown() }), 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 }), 248 249 &[interface_val] 249 - ).map(|val| val.into_iter().nth(0).unwrap_or(Value::nothing(Span::unknown()))) 250 + ).map(|val| val.into_iter().nth(0).unwrap_or_default()) 250 251 } 251 252 252 253 /// Set a D-Bus property on the given object
+16 -16
src/convert.rs
··· 6 6 use crate::dbus_type::DbusType; 7 7 8 8 /// Get the arguments of a message as nushell Values 9 - pub fn from_message(message: &Message) -> Result<Vec<Value>, String> { 9 + pub fn from_message(message: &Message, span: Span) -> Result<Vec<Value>, String> { 10 10 let mut out = vec![]; 11 11 for refarg in message.iter_init() { 12 - out.push(from_refarg(&refarg)?); 12 + out.push(from_refarg(&refarg, span)?); 13 13 } 14 14 Ok(out) 15 15 } 16 16 17 - pub fn from_refarg(refarg: &dyn RefArg) -> Result<Value, String> { 17 + pub fn from_refarg(refarg: &dyn RefArg, span: Span) -> Result<Value, String> { 18 18 Ok(match refarg.arg_type() { 19 19 ArgType::Array => { 20 20 if refarg.signature().starts_with("a{") { ··· 24 24 while let Some(key) = iter.next() { 25 25 if let Some(val) = iter.next() { 26 26 if let Some(key_str) = key.as_str() { 27 - record.insert(key_str, from_refarg(val)?); 27 + record.insert(key_str, from_refarg(val, span)?); 28 28 } 29 29 } 30 30 } 31 - Value::record(record, Span::unknown()) 31 + Value::record(record, span) 32 32 } else if &*refarg.signature() == "ay" { 33 33 // Byte array - better to return as binary 34 34 let bytes = dbus::arg::cast::<Vec<u8>>(&refarg.box_clone()).unwrap().to_owned(); 35 - Value::binary(bytes, Span::unknown()) 35 + Value::binary(bytes, span) 36 36 } else { 37 37 // It's an array 38 38 Value::list( 39 - refarg.as_iter().unwrap().map(from_refarg).flatten().collect(), 40 - Span::unknown()) 39 + refarg.as_iter().unwrap().map(|v| from_refarg(v, span)).flatten().collect(), 40 + span) 41 41 } 42 42 }, 43 43 ArgType::Variant => { 44 44 let inner = refarg.as_iter().unwrap().nth(0).unwrap(); 45 - return from_refarg(inner); 45 + return from_refarg(inner, span); 46 46 }, 47 47 ArgType::Boolean => 48 - Value::bool(refarg.as_i64().unwrap() != 0, Span::unknown()), 48 + Value::bool(refarg.as_i64().unwrap() != 0, span), 49 49 50 50 // Strings 51 51 ArgType::String | ArgType::ObjectPath | ArgType::Signature => 52 - Value::string(refarg.as_str().unwrap(), Span::unknown()), 52 + Value::string(refarg.as_str().unwrap(), span), 53 53 // Ints 54 54 ArgType::Byte | ArgType::Int16 | ArgType::UInt16 | ArgType::Int32 | 55 55 ArgType::UInt32 | ArgType::Int64 | ArgType::UnixFd => 56 - Value::int(refarg.as_i64().unwrap(), Span::unknown()), 56 + Value::int(refarg.as_i64().unwrap(), span), 57 57 58 58 // Nushell doesn't support u64, so present it as a string 59 - ArgType::UInt64 => Value::string(refarg.as_u64().unwrap().to_string(), Span::unknown()), 59 + ArgType::UInt64 => Value::string(refarg.as_u64().unwrap().to_string(), span), 60 60 61 61 // Floats 62 62 ArgType::Double => 63 - Value::float(refarg.as_f64().unwrap(), Span::unknown()), 63 + Value::float(refarg.as_f64().unwrap(), span), 64 64 65 65 ArgType::Struct => 66 66 Value::list( 67 - refarg.as_iter().unwrap().map(from_refarg).flatten().collect(), 68 - Span::unknown()), 67 + refarg.as_iter().unwrap().map(|v| from_refarg(v, span)).flatten().collect(), 68 + span), 69 69 70 70 ArgType::DictEntry => 71 71 return Err("Encountered dictionary entry outside of dictionary".into()),
+3 -3
src/main.rs
··· 225 225 // Make the output easier to deal with by returning a list only if there are multiple return 226 226 // values (not so common) 227 227 match values.len() { 228 - 0 if flatten => Ok(Value::nothing(Span::unknown())), 228 + 0 if flatten => Ok(Value::nothing(call.head)), 229 229 1 if flatten => Ok(values.into_iter().nth(0).unwrap()), 230 - _ => Ok(Value::list(values, Span::unknown())) 230 + _ => Ok(Value::list(values, call.head)) 231 231 } 232 232 } 233 233 ··· 263 263 call.get_flag("signature")?.as_ref(), 264 264 &call.req(3)?, 265 265 )?; 266 - Ok(Value::nothing(Span::unknown())) 266 + Ok(Value::nothing(call.head)) 267 267 } 268 268 }