Allow knowing the command name tag given no input. (#3988)

```
tags
```
This commit is contained in:
Andrés N. Robalino 2021-09-03 01:46:15 -05:00 committed by GitHub
parent c9c6bd4836
commit 4e2d3ceaaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,43 +18,52 @@ impl WholeStreamCommand for Tags {
"Read the tags (metadata) for values." "Read the tags (metadata) for values."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(tags(args)) Ok(tags(args))
} }
} }
fn tags(args: CommandArgs) -> ActionStream { fn build_tag_table(tag: impl Into<Tag>) -> Value {
args.input let tag = tag.into();
.map(move |v| { let span = tag.span;
TaggedDictBuilder::build(v.tag(), |tags| {
if let Some(anchor) = anchor_as_value(&v) { TaggedDictBuilder::build(tag.clone(), |tags| {
if let Some(anchor) = anchor_as_value(&tag) {
tags.insert_value("anchor", anchor); tags.insert_value("anchor", anchor);
} }
tags.insert_value( tags.insert_value(
"span", "span",
TaggedDictBuilder::build(v.tag(), |span_dict| { TaggedDictBuilder::build(tag.clone(), |span_dict| {
let span = v.tag().span;
span_dict.insert_untagged("start", UntaggedValue::int(span.start() as i64)); span_dict.insert_untagged("start", UntaggedValue::int(span.start() as i64));
span_dict.insert_untagged("end", UntaggedValue::int(span.end() as i64)); span_dict.insert_untagged("end", UntaggedValue::int(span.end() as i64));
}), }),
); );
}) })
})
.into_action_stream()
} }
fn anchor_as_value(value: &Value) -> Option<Value> { fn tags(args: CommandArgs) -> OutputStream {
let tag = value.tag(); if args.input.is_empty() {
let anchor = tag.anchor; OutputStream::one(build_tag_table(&args.name_tag()))
} else {
args.input
.map(move |v| build_tag_table(v.tag()))
.into_output_stream()
}
}
fn anchor_as_value(tag: &Tag) -> Option<Value> {
let anchor = tag.anchor.as_ref();
anchor.as_ref()?; anchor.as_ref()?;
Some(TaggedDictBuilder::build(value.tag(), |table| { Some(TaggedDictBuilder::build(tag, |table| {
let value = match anchor { let value = match anchor {
Some(AnchorLocation::File(path)) => Some(("file", UntaggedValue::from(path))), Some(AnchorLocation::File(path)) => {
Some(("file", UntaggedValue::from(path.to_string())))
}
Some(AnchorLocation::Url(destination)) => { Some(AnchorLocation::Url(destination)) => {
Some(("url", UntaggedValue::from(destination))) Some(("url", UntaggedValue::from(destination.to_string())))
} }
Some(AnchorLocation::Source(text)) => Some(( Some(AnchorLocation::Source(text)) => Some((
"source", "source",