2019-08-29 22:52:32 +00:00
|
|
|
use crate::commands::command::Command;
|
2019-12-04 19:52:31 +00:00
|
|
|
use crate::data::TaggedListBuilder;
|
2019-08-29 22:52:32 +00:00
|
|
|
use crate::prelude::*;
|
2019-12-04 19:52:31 +00:00
|
|
|
use nu_protocol::{NamedType, PositionalType, Signature, TaggedDictBuilder, UntaggedValue, Value};
|
2019-08-29 22:52:32 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
pub(crate) fn command_dict(command: Arc<Command>, tag: impl Into<Tag>) -> Value {
|
2019-08-29 22:52:32 +00:00
|
|
|
let tag = tag.into();
|
|
|
|
|
2019-10-13 04:12:43 +00:00
|
|
|
let mut cmd_dict = TaggedDictBuilder::new(&tag);
|
2019-08-29 22:52:32 +00:00
|
|
|
|
2019-12-04 19:52:31 +00:00
|
|
|
cmd_dict.insert_untagged("name", UntaggedValue::string(command.name()));
|
2019-08-29 22:52:32 +00:00
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
cmd_dict.insert_untagged(
|
2019-08-29 22:52:32 +00:00
|
|
|
"type",
|
2019-12-04 19:52:31 +00:00
|
|
|
UntaggedValue::string(match command.deref() {
|
2019-08-29 22:52:32 +00:00
|
|
|
Command::WholeStream(_) => "Command",
|
|
|
|
Command::PerItem(_) => "Filter",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
cmd_dict.insert_value("signature", signature_dict(command.signature(), tag));
|
2019-12-04 19:52:31 +00:00
|
|
|
cmd_dict.insert_untagged("usage", UntaggedValue::string(command.usage()));
|
2019-08-29 22:52:32 +00:00
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
cmd_dict.into_value()
|
2019-08-29 22:52:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
fn for_spec(name: &str, ty: &str, required: bool, tag: impl Into<Tag>) -> Value {
|
2019-08-29 22:52:32 +00:00
|
|
|
let tag = tag.into();
|
|
|
|
|
|
|
|
let mut spec = TaggedDictBuilder::new(tag);
|
|
|
|
|
2019-12-04 19:52:31 +00:00
|
|
|
spec.insert_untagged("name", UntaggedValue::string(name));
|
|
|
|
spec.insert_untagged("type", UntaggedValue::string(ty));
|
2019-11-21 14:33:14 +00:00
|
|
|
spec.insert_untagged(
|
2019-09-05 16:23:42 +00:00
|
|
|
"required",
|
2019-12-04 19:52:31 +00:00
|
|
|
UntaggedValue::string(if required { "yes" } else { "no" }),
|
2019-09-05 16:23:42 +00:00
|
|
|
);
|
2019-08-29 22:52:32 +00:00
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
spec.into_value()
|
2019-08-29 22:52:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
fn signature_dict(signature: Signature, tag: impl Into<Tag>) -> Value {
|
2019-08-29 22:52:32 +00:00
|
|
|
let tag = tag.into();
|
2019-10-13 04:12:43 +00:00
|
|
|
let mut sig = TaggedListBuilder::new(&tag);
|
2019-08-29 22:52:32 +00:00
|
|
|
|
|
|
|
for arg in signature.positional.iter() {
|
2019-10-28 05:15:35 +00:00
|
|
|
let is_required = match arg.0 {
|
2019-09-05 16:23:42 +00:00
|
|
|
PositionalType::Mandatory(_, _) => true,
|
|
|
|
PositionalType::Optional(_, _) => false,
|
2019-08-29 22:52:32 +00:00
|
|
|
};
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
sig.push_value(for_spec(arg.0.name(), "argument", is_required, &tag));
|
2019-08-29 22:52:32 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 15:28:26 +00:00
|
|
|
if signature.rest_positional.is_some() {
|
2019-08-29 22:52:32 +00:00
|
|
|
let is_required = false;
|
2019-11-21 14:33:14 +00:00
|
|
|
sig.push_value(for_spec("rest", "argument", is_required, &tag));
|
2019-08-29 22:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (name, ty) in signature.named.iter() {
|
2019-10-28 05:15:35 +00:00
|
|
|
match ty.0 {
|
2020-02-12 02:24:31 +00:00
|
|
|
NamedType::Mandatory(_, _) => sig.push_value(for_spec(name, "flag", true, &tag)),
|
|
|
|
NamedType::Optional(_, _) => sig.push_value(for_spec(name, "flag", false, &tag)),
|
|
|
|
NamedType::Switch(_) => sig.push_value(for_spec(name, "switch", false, &tag)),
|
2019-08-29 22:52:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
sig.into_value()
|
2019-08-29 22:52:32 +00:00
|
|
|
}
|