2019-12-31 04:05:02 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use crate::context::CommandRegistry;
|
|
|
|
use crate::prelude::*;
|
|
|
|
use indexmap::set::IndexSet;
|
|
|
|
use nu_errors::ShellError;
|
|
|
|
use nu_protocol::{ReturnSuccess, Signature};
|
|
|
|
|
|
|
|
pub struct Uniq;
|
|
|
|
|
2020-05-29 08:22:52 +00:00
|
|
|
#[async_trait]
|
2019-12-31 04:05:02 +00:00
|
|
|
impl WholeStreamCommand for Uniq {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"uniq"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("uniq")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Return the unique rows"
|
|
|
|
}
|
|
|
|
|
2020-05-29 08:22:52 +00:00
|
|
|
async fn run(
|
2019-12-31 04:05:02 +00:00
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-06-04 08:42:23 +00:00
|
|
|
uniq(args, registry).await
|
2019-12-31 04:05:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 08:42:23 +00:00
|
|
|
async fn uniq(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let input = args.input;
|
|
|
|
let uniq_values: IndexSet<_> = input.collect().await;
|
2019-12-31 04:05:02 +00:00
|
|
|
|
2020-06-04 08:42:23 +00:00
|
|
|
let mut values_vec_deque = VecDeque::new();
|
2019-12-31 04:05:02 +00:00
|
|
|
|
2020-06-04 08:42:23 +00:00
|
|
|
for item in uniq_values
|
|
|
|
.iter()
|
|
|
|
.map(|row| ReturnSuccess::value(row.clone()))
|
|
|
|
{
|
|
|
|
values_vec_deque.push_back(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(futures::stream::iter(values_vec_deque).to_output_stream())
|
2019-12-31 04:05:02 +00:00
|
|
|
}
|
2020-05-18 12:56:01 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Uniq;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn examples_work_as_expected() {
|
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
|
|
|
test_examples(Uniq {})
|
|
|
|
}
|
|
|
|
}
|