nushell/crates/nu-cli/src/commands/tags.rs

58 lines
1.7 KiB
Rust
Raw Normal View History

use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{Signature, TaggedDictBuilder, UntaggedValue};
pub struct Tags;
impl WholeStreamCommand for Tags {
fn name(&self) -> &str {
"tags"
}
fn signature(&self) -> Signature {
Signature::build("tags")
}
fn usage(&self) -> &str {
"Read the tags (metadata) for values."
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
tags(args, registry)
}
}
fn tags(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
Ok(args
.input
.map(move |v| {
let mut tags = TaggedDictBuilder::new(v.tag());
{
2019-09-29 07:13:56 +02:00
let anchor = v.anchor();
let span = v.tag.span;
let mut dict = TaggedDictBuilder::new(v.tag());
dict.insert_untagged("start", UntaggedValue::int(span.start() as i64));
dict.insert_untagged("end", UntaggedValue::int(span.end() as i64));
tags.insert_value("span", dict.into_value());
2019-08-10 22:18:14 +02:00
match anchor {
2019-09-29 07:18:59 +02:00
Some(AnchorLocation::File(source)) => {
tags.insert_untagged("anchor", UntaggedValue::string(source));
}
2019-09-29 07:18:59 +02:00
Some(AnchorLocation::Url(source)) => {
tags.insert_untagged("anchor", UntaggedValue::string(source));
}
_ => {}
}
}
tags.into_value()
})
.to_output_stream())
}