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,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::Span;
use nu_protocol::{Example, PipelineData, ShellError, Signature, SyntaxShape, Value};
use nu_protocol::{Example, PipelineData, ShellError, Signature, SyntaxShape, Type, Value};
use std::marker::PhantomData;
pub trait HashDigest: digest::Digest + Clone {
@ -50,6 +50,10 @@ where
fn signature(&self) -> Signature {
Signature::build(self.name())
.input_output_types(vec![
(Type::String, Type::String),
(Type::String, Type::Binary),
])
.switch(
"binary",
"Output binary instead of hexadecimal representation",

View File

@ -12,7 +12,7 @@ impl HashDigest for Md5 {
fn examples() -> Vec<Example> {
vec![
Example {
description: "get a hexadecimaly encoded string of the md5 digest of a string",
description: "Return the md5 hash of a string, hex-encoded",
example: "echo 'abcdefghijklmnopqrstuvwxyz' | hash md5",
result: Some(Value::String {
val: "c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
@ -20,7 +20,7 @@ impl HashDigest for Md5 {
}),
},
Example {
description: "get the md5 digest of a string in binary",
description: "Return the md5 hash of a string, as binary",
example: "echo 'abcdefghijklmnopqrstuvwxyz' | hash md5 --binary",
result: Some(Value::Binary {
val: vec![
@ -31,7 +31,7 @@ impl HashDigest for Md5 {
}),
},
Example {
description: "md5 encode a file",
description: "Return the md5 hash of a file's contents",
example: "open ./nu_0_24_1_windows.zip | hash md5",
result: None,
},

View File

@ -12,7 +12,7 @@ impl HashDigest for Sha256 {
fn examples() -> Vec<Example> {
vec![
Example {
description: "get a hexadecimaly encoded string of the sha256 digest of a string",
description: "Return the sha256 hash of a string, hex-encoded",
example: "echo 'abcdefghijklmnopqrstuvwxyz' | hash sha256",
result: Some(Value::String {
val: "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73"
@ -21,7 +21,7 @@ impl HashDigest for Sha256 {
}),
},
Example {
description: "get the sha256 digest of a string in binary",
description: "Return the sha256 hash of a string, as binary",
example: "echo 'abcdefghijklmnopqrstuvwxyz' | hash sha256 --binary",
result: Some(Value::Binary {
val: vec![
@ -33,7 +33,7 @@ impl HashDigest for Sha256 {
}),
},
Example {
description: "sha256 encode a file",
description: "Return the sha256 hash of a file's contents",
example: "open ./nu_0_24_1_windows.zip | hash sha256",
result: None,
},