Skip to content

Commit 3f6feab

Browse files
committed
pass span through command to returned Values rather than Span::unknown
1 parent d827005 commit 3f6feab

3 files changed

Lines changed: 30 additions & 29 deletions

File tree

src/client.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use dbus::{channel::{Channel, BusType}, Message, arg::messageitem::MessageItem};
22
use nu_plugin::LabeledError;
3-
use nu_protocol::{Spanned, Value, Span};
3+
use nu_protocol::{Spanned, Value};
44

55
use crate::{config::{DbusClientConfig, DbusBusChoice}, dbus_type::DbusType, convert::to_message_item, introspection::Node};
66

@@ -206,7 +206,8 @@ impl DbusClient {
206206
let resp = self.conn.send_with_reply_and_block(message, self.config.timeout.item)
207207
.map_err(|err| self.error(err, context))?;
208208

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))
210211
}
211212

212213
/// Get a D-Bus property from the given object
@@ -223,11 +224,11 @@ impl DbusClient {
223224
self.call(
224225
dest,
225226
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 }),
229230
&[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())
231232
}
232233

233234
/// Get all D-Bus properties from the given object
@@ -242,11 +243,11 @@ impl DbusClient {
242243
self.call(
243244
dest,
244245
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 }),
248249
&[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())
250251
}
251252

252253
/// Set a D-Bus property on the given object

src/convert.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ use std::str::FromStr;
66
use crate::dbus_type::DbusType;
77

88
/// 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> {
1010
let mut out = vec![];
1111
for refarg in message.iter_init() {
12-
out.push(from_refarg(&refarg)?);
12+
out.push(from_refarg(&refarg, span)?);
1313
}
1414
Ok(out)
1515
}
1616

17-
pub fn from_refarg(refarg: &dyn RefArg) -> Result<Value, String> {
17+
pub fn from_refarg(refarg: &dyn RefArg, span: Span) -> Result<Value, String> {
1818
Ok(match refarg.arg_type() {
1919
ArgType::Array => {
2020
if refarg.signature().starts_with("a{") {
@@ -24,48 +24,48 @@ pub fn from_refarg(refarg: &dyn RefArg) -> Result<Value, String> {
2424
while let Some(key) = iter.next() {
2525
if let Some(val) = iter.next() {
2626
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)?);
2828
}
2929
}
3030
}
31-
Value::record(record, Span::unknown())
31+
Value::record(record, span)
3232
} else if &*refarg.signature() == "ay" {
3333
// Byte array - better to return as binary
3434
let bytes = dbus::arg::cast::<Vec<u8>>(&refarg.box_clone()).unwrap().to_owned();
35-
Value::binary(bytes, Span::unknown())
35+
Value::binary(bytes, span)
3636
} else {
3737
// It's an array
3838
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)
4141
}
4242
},
4343
ArgType::Variant => {
4444
let inner = refarg.as_iter().unwrap().nth(0).unwrap();
45-
return from_refarg(inner);
45+
return from_refarg(inner, span);
4646
},
4747
ArgType::Boolean =>
48-
Value::bool(refarg.as_i64().unwrap() != 0, Span::unknown()),
48+
Value::bool(refarg.as_i64().unwrap() != 0, span),
4949

5050
// Strings
5151
ArgType::String | ArgType::ObjectPath | ArgType::Signature =>
52-
Value::string(refarg.as_str().unwrap(), Span::unknown()),
52+
Value::string(refarg.as_str().unwrap(), span),
5353
// Ints
5454
ArgType::Byte | ArgType::Int16 | ArgType::UInt16 | ArgType::Int32 |
5555
ArgType::UInt32 | ArgType::Int64 | ArgType::UnixFd =>
56-
Value::int(refarg.as_i64().unwrap(), Span::unknown()),
56+
Value::int(refarg.as_i64().unwrap(), span),
5757

5858
// 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),
6060

6161
// Floats
6262
ArgType::Double =>
63-
Value::float(refarg.as_f64().unwrap(), Span::unknown()),
63+
Value::float(refarg.as_f64().unwrap(), span),
6464

6565
ArgType::Struct =>
6666
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),
6969

7070
ArgType::DictEntry =>
7171
return Err("Encountered dictionary entry outside of dictionary".into()),

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ impl NuPluginDbus {
225225
// Make the output easier to deal with by returning a list only if there are multiple return
226226
// values (not so common)
227227
match values.len() {
228-
0 if flatten => Ok(Value::nothing(Span::unknown())),
228+
0 if flatten => Ok(Value::nothing(call.head)),
229229
1 if flatten => Ok(values.into_iter().nth(0).unwrap()),
230-
_ => Ok(Value::list(values, Span::unknown()))
230+
_ => Ok(Value::list(values, call.head))
231231
}
232232
}
233233

@@ -263,6 +263,6 @@ impl NuPluginDbus {
263263
call.get_flag("signature")?.as_ref(),
264264
&call.req(3)?,
265265
)?;
266-
Ok(Value::nothing(Span::unknown()))
266+
Ok(Value::nothing(call.head))
267267
}
268268
}

0 commit comments

Comments
 (0)