2019-08-29 22:52:32 +00:00
|
|
|
use crate::commands::command::Command;
|
2019-09-05 16:23:42 +00:00
|
|
|
use crate::data::{TaggedDictBuilder, TaggedListBuilder, Value};
|
2019-08-29 22:52:32 +00:00
|
|
|
use crate::parser::registry::{NamedType, PositionalType, Signature};
|
|
|
|
use crate::prelude::*;
|
|
|
|
use std::ops::Deref;
|
|
|
|
|
|
|
|
pub(crate) fn command_dict(command: Arc<Command>, tag: impl Into<Tag>) -> Tagged<Value> {
|
|
|
|
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
|
|
|
|
|
|
|
cmd_dict.insert("name", Value::string(command.name()));
|
|
|
|
|
|
|
|
cmd_dict.insert(
|
|
|
|
"type",
|
|
|
|
Value::string(match command.deref() {
|
|
|
|
Command::WholeStream(_) => "Command",
|
|
|
|
Command::PerItem(_) => "Filter",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
cmd_dict.insert_tagged("signature", signature_dict(command.signature(), tag));
|
|
|
|
cmd_dict.insert("usage", Value::string(command.usage()));
|
|
|
|
|
|
|
|
cmd_dict.into_tagged_value()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn for_spec(name: &str, ty: &str, required: bool, tag: impl Into<Tag>) -> Tagged<Value> {
|
|
|
|
let tag = tag.into();
|
|
|
|
|
|
|
|
let mut spec = TaggedDictBuilder::new(tag);
|
|
|
|
|
|
|
|
spec.insert("name", Value::string(name));
|
|
|
|
spec.insert("type", Value::string(ty));
|
2019-09-05 16:23:42 +00:00
|
|
|
spec.insert(
|
|
|
|
"required",
|
|
|
|
Value::string(if required { "yes" } else { "no" }),
|
|
|
|
);
|
2019-08-29 22:52:32 +00:00
|
|
|
|
|
|
|
spec.into_tagged_value()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature_dict(signature: Signature, tag: impl Into<Tag>) -> Tagged<Value> {
|
|
|
|
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-10-28 05:15:35 +00:00
|
|
|
sig.insert_tagged(for_spec(arg.0.name(), "argument", is_required, &tag));
|
2019-08-29 22:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(_) = signature.rest_positional {
|
|
|
|
let is_required = false;
|
2019-10-13 04:12:43 +00:00
|
|
|
sig.insert_tagged(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 {
|
2019-10-13 04:12:43 +00:00
|
|
|
NamedType::Mandatory(_) => sig.insert_tagged(for_spec(name, "flag", true, &tag)),
|
|
|
|
NamedType::Optional(_) => sig.insert_tagged(for_spec(name, "flag", false, &tag)),
|
|
|
|
NamedType::Switch => sig.insert_tagged(for_spec(name, "switch", false, &tag)),
|
2019-08-29 22:52:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sig.into_tagged_value()
|
|
|
|
}
|