nushell/crates/nu-command/src/commands/from_tsv.rs

55 lines
1.2 KiB
Rust
Raw Normal View History

use crate::commands::from_delimited_data::from_delimited_data;
2019-08-29 11:02:16 +02:00
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::Signature;
2019-08-29 11:02:16 +02:00
pub struct FromTsv;
2019-08-29 11:02:16 +02:00
#[derive(Deserialize)]
pub struct FromTsvArgs {
noheaders: bool,
2019-08-29 11:02:16 +02:00
}
impl WholeStreamCommand for FromTsv {
2019-08-29 11:02:16 +02:00
fn name(&self) -> &str {
"from tsv"
2019-08-29 11:02:16 +02:00
}
fn signature(&self) -> Signature {
Signature::build("from tsv").switch(
"noheaders",
"don't treat the first row as column names",
Some('n'),
)
2019-08-29 11:02:16 +02:00
}
fn usage(&self) -> &str {
"Parse text as .tsv and create table."
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_tsv(args)
2019-08-29 11:02:16 +02:00
}
}
fn from_tsv(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let (FromTsvArgs { noheaders }, input) = args.process()?;
from_delimited_data(noheaders, '\t', "TSV", input, name)
2019-08-29 11:02:16 +02:00
}
#[cfg(test)]
mod tests {
use super::FromTsv;
use super::ShellError;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
test_examples(FromTsv {})
}
}