Declare input and output types of commands (#6796)

* Add failing test that list of ints and floats is List<Number>

* Start defining subtype relation

* Make it possible to declare input and output types for commands

- Enforce them in tests

* Declare input and output types of commands

* Add formatted signatures to `help commands` table

* Revert SyntaxShape::Table -> Type::Table change

* Revert unnecessary derive(Hash) on SyntaxShape

Co-authored-by: JT <547158+jntrnr@users.noreply.github.com>
This commit is contained in:
Dan Davison
2022-11-09 16:55:05 -05:00
committed by GitHub
parent f878276de7
commit df94052180
238 changed files with 2315 additions and 756 deletions

View File

@ -3,7 +3,9 @@ use super::delimited::{from_delimited_data, trim_from_str};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Value};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
#[derive(Clone)]
pub struct FromCsv;
@ -15,6 +17,7 @@ impl Command for FromCsv {
fn signature(&self) -> Signature {
Signature::build("from csv")
.input_output_types(vec![(Type::String, Type::Table(vec![]))])
.named(
"separator",
SyntaxShape::String,
@ -54,8 +57,18 @@ impl Command for FromCsv {
vec![
Example {
description: "Convert comma-separated data to a table",
example: "open data.txt | from csv",
result: None,
example: "\"ColA,ColB\n1,2\" | from csv",
result: Some(Value::List {
vals: vec![Value::Record {
cols: vec!["ColA".to_string(), "ColB".to_string()],
vals: vec![
Value::test_int(1),
Value::test_int(2),
],
span: Span::test_data(),
}],
span: Span::test_data(),
})
},
Example {
description: "Convert comma-separated data to a table, ignoring headers",

View File

@ -7,7 +7,7 @@ use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::Category;
use nu_protocol::Config;
use nu_protocol::{
Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
#[derive(Clone)]
@ -22,6 +22,7 @@ impl Command for FromEml {
fn signature(&self) -> Signature {
Signature::build("from eml")
.input_output_types(vec![(Type::String, Type::Record(vec![]))])
.named(
"preview-body",
SyntaxShape::Int,
@ -32,7 +33,7 @@ impl Command for FromEml {
}
fn usage(&self) -> &str {
"Parse text as .eml and create table."
"Parse text as .eml and create record."
}
fn run(
@ -52,7 +53,7 @@ impl Command for FromEml {
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Convert eml structured data into table",
description: "Convert eml structured data into record",
example: "'From: test@email.com
Subject: Welcome
To: someone@somewhere.com
@ -89,7 +90,7 @@ Test' | from eml",
}),
},
Example {
description: "Convert eml structured data into table",
description: "Convert eml structured data into record",
example: "'From: test@email.com
Subject: Welcome
To: someone@somewhere.com

View File

@ -6,7 +6,7 @@ use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span,
Spanned, Value,
Spanned, Type, Value,
};
use std::io::BufReader;
@ -19,7 +19,9 @@ impl Command for FromIcs {
}
fn signature(&self) -> Signature {
Signature::build("from ics").category(Category::Formats)
Signature::build("from ics")
.input_output_types(vec![(Type::String, Type::Table(vec![]))])
.category(Category::Formats)
}
fn usage(&self) -> &str {

View File

@ -2,7 +2,8 @@ use indexmap::map::IndexMap;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Value,
Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type,
Value,
};
#[derive(Clone)]
@ -14,11 +15,13 @@ impl Command for FromIni {
}
fn signature(&self) -> Signature {
Signature::build("from ini").category(Category::Formats)
Signature::build("from ini")
.input_output_types(vec![(Type::String, Type::Record(vec![]))])
.category(Category::Formats)
}
fn usage(&self) -> &str {
"Parse text as .ini and create table"
"Parse text as .ini and create record"
}
fn examples(&self) -> Vec<Example> {
@ -26,7 +29,7 @@ impl Command for FromIni {
example: "'[foo]
a=1
b=2' | from ini",
description: "Converts ini formatted string to table",
description: "Converts ini formatted string to record",
result: Some(Value::Record {
cols: vec!["foo".to_string()],
vals: vec![Value::Record {

View File

@ -2,7 +2,7 @@ use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError,
Signature, Span, Value,
Signature, Span, Type, Value,
};
#[derive(Clone)]
@ -19,6 +19,7 @@ impl Command for FromJson {
fn signature(&self) -> nu_protocol::Signature {
Signature::build("from json")
.input_output_types(vec![(Type::String, Type::Any)])
.switch("objects", "treat each line as a separate value", Some('o'))
.category(Category::Formats)
}

View File

@ -17,7 +17,9 @@ impl Command for FromNuon {
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("from nuon").category(Category::Experimental)
Signature::build("from nuon")
.input_output_types(vec![(Type::String, Type::Any)])
.category(Category::Experimental)
}
fn examples(&self) -> Vec<Example> {

View File

@ -4,7 +4,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use std::io::Cursor;
@ -18,6 +18,8 @@ impl Command for FromOds {
fn signature(&self) -> Signature {
Signature::build("from ods")
.input_output_types(vec![(Type::String, Type::Table(vec![]))])
.allow_variants_without_examples(true)
.named(
"sheets",
SyntaxShape::List(Box::new(SyntaxShape::String)),

View File

@ -4,7 +4,7 @@ use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Spanned,
SyntaxShape, Value,
SyntaxShape, Type, Value,
};
#[derive(Clone)]
@ -19,6 +19,7 @@ impl Command for FromSsv {
fn signature(&self) -> Signature {
Signature::build("from ssv")
.input_output_types(vec![(Type::String, Type::Table(vec![]))])
.switch(
"noheaders",
"don't treat the first row as column names",

View File

@ -1,7 +1,7 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Value,
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type, Value,
};
#[derive(Clone)]
@ -13,18 +13,20 @@ impl Command for FromToml {
}
fn signature(&self) -> Signature {
Signature::build("from toml").category(Category::Formats)
Signature::build("from toml")
.input_output_types(vec![(Type::String, Type::Record(vec![]))])
.category(Category::Formats)
}
fn usage(&self) -> &str {
"Parse text as .toml and create table."
"Parse text as .toml and create record."
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
example: "'a = 1' | from toml",
description: "Converts toml formatted string to table",
description: "Converts toml formatted string to record",
result: Some(Value::Record {
cols: vec!["a".to_string()],
vals: vec![Value::Int {
@ -37,7 +39,7 @@ impl Command for FromToml {
Example {
example: "'a = 1
b = [1, 2]' | from toml",
description: "Converts toml formatted string to table",
description: "Converts toml formatted string to record",
result: Some(Value::Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![

View File

@ -3,7 +3,9 @@ use super::delimited::{from_delimited_data, trim_from_str};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Value};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
#[derive(Clone)]
pub struct FromTsv;
@ -15,6 +17,7 @@ impl Command for FromTsv {
fn signature(&self) -> Signature {
Signature::build("from tsv")
.input_output_types(vec![(Type::String, Type::Table(vec![]))])
.switch(
"noheaders",
"don't treat the first row as column names",
@ -46,6 +49,21 @@ impl Command for FromTsv {
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Convert tab-separated data to a table",
example: "\"ColA\tColB\n1\t2\" | from tsv",
result: Some(Value::List {
vals: vec![Value::Record {
cols: vec!["ColA".to_string(), "ColB".to_string()],
vals: vec![
Value::test_int(1),
Value::test_int(2),
],
span: Span::test_data(),
}],
span: Span::test_data(),
})
},
Example {
description: "Create a tsv file with header columns and open it",
example: r#"$'c1(char tab)c2(char tab)c3(char nl)1(char tab)2(char tab)3' | save tsv-data | open tsv-data | from tsv"#,

View File

@ -1,6 +1,8 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Config, Example, PipelineData, ShellError, Signature, Span, Value};
use nu_protocol::{
Category, Config, Example, PipelineData, ShellError, Signature, Span, Type, Value,
};
#[derive(Clone)]
pub struct FromUrl;
@ -11,11 +13,13 @@ impl Command for FromUrl {
}
fn signature(&self) -> Signature {
Signature::build("from url").category(Category::Formats)
Signature::build("from url")
.input_output_types(vec![(Type::String, Type::Record(vec![]))])
.category(Category::Formats)
}
fn usage(&self) -> &str {
"Parse url-encoded string as a table."
"Parse url-encoded string as a record."
}
fn run(
@ -33,7 +37,7 @@ impl Command for FromUrl {
fn examples(&self) -> Vec<Example> {
vec![Example {
example: "'bread=baguette&cheese=comt%C3%A9&meat=ham&fat=butter' | from url",
description: "Convert url encoded string into a table",
description: "Convert url encoded string into a record",
result: Some(Value::Record {
cols: vec![
"bread".to_string(),

View File

@ -5,7 +5,7 @@ use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span,
Spanned, Value,
Spanned, Type, Value,
};
#[derive(Clone)]
@ -17,7 +17,9 @@ impl Command for FromVcf {
}
fn signature(&self) -> Signature {
Signature::build("from vcf").category(Category::Formats)
Signature::build("from vcf")
.input_output_types(vec![(Type::String, Type::Table(vec![]))])
.category(Category::Formats)
}
fn usage(&self) -> &str {

View File

@ -4,7 +4,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use std::io::Cursor;
@ -18,6 +18,8 @@ impl Command for FromXlsx {
fn signature(&self) -> Signature {
Signature::build("from xlsx")
.input_output_types(vec![(Type::Binary, Type::Table(vec![]))])
.allow_variants_without_examples(true)
.named(
"sheets",
SyntaxShape::List(Box::new(SyntaxShape::String)),

View File

@ -3,7 +3,7 @@ use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span,
Spanned, Value,
Spanned, Type, Value,
};
#[derive(Clone)]
@ -15,11 +15,13 @@ impl Command for FromXml {
}
fn signature(&self) -> Signature {
Signature::build("from xml").category(Category::Formats)
Signature::build("from xml")
.input_output_types(vec![(Type::String, Type::Record(vec![]))])
.category(Category::Formats)
}
fn usage(&self) -> &str {
"Parse text as .xml and create table."
"Parse text as .xml and create record."
}
fn run(
@ -40,7 +42,7 @@ impl Command for FromXml {
<note>
<remember>Event</remember>
</note>' | from xml"#,
description: "Converts xml formatted string to table",
description: "Converts xml formatted string to record",
result: Some(Value::Record {
cols: vec!["note".to_string()],
vals: vec![Value::Record {

View File

@ -3,7 +3,7 @@ use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span,
Spanned, Value,
Spanned, Type, Value,
};
use serde::de::Deserialize;
use std::collections::HashMap;
@ -17,7 +17,9 @@ impl Command for FromYaml {
}
fn signature(&self) -> Signature {
Signature::build("from yaml").category(Category::Formats)
Signature::build("from yaml")
.input_output_types(vec![(Type::String, Type::Any)])
.category(Category::Formats)
}
fn usage(&self) -> &str {

View File

@ -4,7 +4,7 @@ use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Config, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape,
Value,
Type, Value,
};
#[derive(Clone)]
@ -17,6 +17,7 @@ impl Command for ToCsv {
fn signature(&self) -> Signature {
Signature::build("to csv")
.input_output_types(vec![(Type::Any, Type::String)])
.named(
"separator",
SyntaxShape::String,

View File

@ -5,7 +5,7 @@ use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Spanned,
SyntaxShape, Value,
SyntaxShape, Type, Value,
};
use rust_embed::RustEmbed;
use serde::{Deserialize, Serialize};
@ -90,6 +90,7 @@ impl Command for ToHtml {
fn signature(&self) -> Signature {
Signature::build("to html")
.input_output_types(vec![(Type::Any, Type::String)])
.switch("html-color", "change ansi colors to html colors", Some('c'))
.switch("no-color", "remove all ansi colors in output", Some('n'))
.switch(

View File

@ -2,7 +2,8 @@ use nu_engine::CallExt;
use nu_protocol::ast::{Call, PathMember};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value,
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type,
Value,
};
#[derive(Clone)]
@ -15,6 +16,7 @@ impl Command for ToJson {
fn signature(&self) -> Signature {
Signature::build("to json")
.input_output_types(vec![(Type::Any, Type::String)])
.switch("raw", "remove all of the whitespace", Some('r'))
.named(
"indent",

View File

@ -3,7 +3,8 @@ use indexmap::map::IndexMap;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Value,
Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type,
Value,
};
#[derive(Clone)]
@ -16,6 +17,7 @@ impl Command for ToMd {
fn signature(&self) -> Signature {
Signature::build("to md")
.input_output_types(vec![(Type::Any, Type::String)])
.switch(
"pretty",
"Formats the Markdown table to vertically align items",

View File

@ -4,7 +4,7 @@ use nu_parser::escape_quote_string;
use nu_protocol::ast::{Call, RangeInclusion};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Value,
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type, Value,
};
#[derive(Clone)]
@ -16,7 +16,9 @@ impl Command for ToNuon {
}
fn signature(&self) -> Signature {
Signature::build("to nuon").category(Category::Experimental)
Signature::build("to nuon")
.input_output_types(vec![(Type::Any, Type::String)])
.category(Category::Experimental)
}
fn usage(&self) -> &str {

View File

@ -3,7 +3,7 @@ use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
format_duration, format_filesize_from_conf, Category, Config, Example, IntoPipelineData,
PipelineData, ShellError, Signature, Value,
PipelineData, ShellError, Signature, Type, Value,
};
#[derive(Clone)]
@ -15,7 +15,9 @@ impl Command for ToText {
}
fn signature(&self) -> Signature {
Signature::build("to text").category(Category::Formats)
Signature::build("to text")
.input_output_types(vec![(Type::Any, Type::String)])
.category(Category::Formats)
}
fn usage(&self) -> &str {

View File

@ -13,7 +13,9 @@ impl Command for ToToml {
}
fn signature(&self) -> Signature {
Signature::build("to toml").category(Category::Formats)
Signature::build("to toml")
.input_output_types(vec![(Type::Any, Type::String)])
.category(Category::Formats)
}
fn usage(&self) -> &str {

View File

@ -1,7 +1,9 @@
use crate::formats::to::delimited::to_delimited_data;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Config, Example, PipelineData, ShellError, Signature, Span, Value};
use nu_protocol::{
Category, Config, Example, PipelineData, ShellError, Signature, Span, Type, Value,
};
#[derive(Clone)]
pub struct ToTsv;
@ -13,6 +15,7 @@ impl Command for ToTsv {
fn signature(&self) -> Signature {
Signature::build("to tsv")
.input_output_types(vec![(Type::Any, Type::String)])
.switch(
"noheaders",
"do not output the column names as the first row",

View File

@ -1,7 +1,7 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Value,
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type, Value,
};
#[derive(Clone)]
@ -13,7 +13,9 @@ impl Command for ToUrl {
}
fn signature(&self) -> Signature {
Signature::build("to url").category(Category::Formats)
Signature::build("to url")
.input_output_types(vec![(Type::Table(vec![]), Type::String)])
.category(Category::Formats)
}
fn usage(&self) -> &str {

View File

@ -4,7 +4,7 @@ use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span,
Spanned, SyntaxShape, Value,
Spanned, SyntaxShape, Type, Value,
};
use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
use std::collections::HashSet;
@ -21,6 +21,7 @@ impl Command for ToXml {
fn signature(&self) -> Signature {
Signature::build("to xml")
.input_output_types(vec![(Type::Record(vec![]), Type::String)])
.named(
"pretty",
SyntaxShape::Int,

View File

@ -1,7 +1,7 @@
use nu_protocol::ast::{Call, PathMember};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Value,
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type, Value,
};
#[derive(Clone)]
@ -13,7 +13,9 @@ impl Command for ToYaml {
}
fn signature(&self) -> Signature {
Signature::build("to yaml").category(Category::Formats)
Signature::build("to yaml")
.input_output_types(vec![(Type::Any, Type::String)])
.category(Category::Formats)
}
fn usage(&self) -> &str {