Move input/output type from Command to Signature (#5880)

This commit is contained in:
JT
2022-06-26 09:23:56 +12:00
committed by GitHub
parent 575ddbd4ef
commit f2989bf704
117 changed files with 344 additions and 981 deletions

View File

@ -1,6 +1,6 @@
use std::path::PathBuf;
use crate::{ast::Call, BlockId, Example, PipelineData, ShellError, Signature, Type};
use crate::{ast::Call, BlockId, Example, PipelineData, ShellError, Signature};
use super::{EngineState, Stack};
@ -62,20 +62,6 @@ pub trait Command: Send + Sync + CommandClone {
fn search_terms(&self) -> Vec<&str> {
vec![]
}
// Command input type. The Type is used during parsing to find the
// correct internal command with similar names. The input type is
// obtained from the previous expression found in the pipeline
fn input_type(&self) -> Type {
Type::Any
}
// Command output type. The output type is the value from the command
// It is used during parsing to find the next command in case there
// are commands with similar names
fn output_type(&self) -> Type {
Type::Any
}
}
pub trait CommandClone {

View File

@ -977,7 +977,7 @@ impl<'a> StateWorkingSet<'a> {
pub fn add_decl(&mut self, decl: Box<dyn Command>) -> DeclId {
let name = decl.name().as_bytes().to_vec();
let input_type = decl.input_type();
let input_type = decl.signature().input_type;
self.delta.decls.push(decl);
let decl_id = self.num_decls() - 1;

View File

@ -10,6 +10,7 @@ use crate::BlockId;
use crate::PipelineData;
use crate::ShellError;
use crate::SyntaxShape;
use crate::Type;
use crate::VarId;
use std::fmt::Write;
@ -106,6 +107,8 @@ pub struct Signature {
pub optional_positional: Vec<PositionalArg>,
pub rest_positional: Option<PositionalArg>,
pub named: Vec<Flag>,
pub input_type: Type,
pub output_type: Type,
pub is_filter: bool,
pub creates_scope: bool,
// Signature category used to classify commands stored in the list of declarations
@ -135,6 +138,8 @@ impl Signature {
required_positional: vec![],
optional_positional: vec![],
rest_positional: None,
input_type: Type::Any,
output_type: Type::Any,
named: vec![],
is_filter: false,
creates_scope: false,
@ -314,6 +319,18 @@ impl Signature {
self
}
/// Changes the input type of the command signature
pub fn input_type(mut self, input_type: Type) -> Signature {
self.input_type = input_type;
self
}
/// Changes the output type of the command signature
pub fn output_type(mut self, output_type: Type) -> Signature {
self.output_type = output_type;
self
}
/// Changes the signature category
pub fn category(mut self, category: Category) -> Signature {
self.category = category;