2021-12-02 06:42:56 +01:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2021-10-25 18:58:58 +02:00
|
|
|
use crate::{ast::Call, BlockId, Example, PipelineData, ShellError, Signature};
|
2021-09-02 20:21:37 +02:00
|
|
|
|
2021-10-25 18:58:58 +02:00
|
|
|
use super::{EngineState, Stack};
|
2021-09-02 10:25:22 +02:00
|
|
|
|
2021-10-25 06:01:02 +02:00
|
|
|
pub trait Command: Send + Sync + CommandClone {
|
2021-09-02 10:25:22 +02:00
|
|
|
fn name(&self) -> &str;
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
fn signature(&self) -> Signature;
|
2021-09-02 10:25:22 +02:00
|
|
|
|
|
|
|
fn usage(&self) -> &str;
|
|
|
|
|
|
|
|
fn extra_usage(&self) -> &str {
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
2021-09-03 00:58:15 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
2021-10-25 08:31:39 +02:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-09-03 00:58:15 +02:00
|
|
|
call: &Call,
|
2021-10-25 06:01:02 +02:00
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError>;
|
2021-09-02 10:25:22 +02:00
|
|
|
|
|
|
|
fn is_binary(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commands that are not meant to be run by users
|
|
|
|
fn is_private(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a built-in command
|
|
|
|
fn is_builtin(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is a sub command
|
|
|
|
fn is_sub(&self) -> bool {
|
|
|
|
self.name().contains(' ')
|
|
|
|
}
|
|
|
|
|
2021-12-18 19:13:56 +01:00
|
|
|
// Is a plugin command (returns plugin's path, encoding and type of shell
|
|
|
|
// if the declaration is a plugin)
|
|
|
|
fn is_plugin(&self) -> Option<(&PathBuf, &str, &Option<PathBuf>)> {
|
2021-11-19 03:51:42 +01:00
|
|
|
None
|
2021-09-02 10:25:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-06 01:16:27 +02:00
|
|
|
// If command is a block i.e. def blah [] { }, get the block id
|
|
|
|
fn get_block_id(&self) -> Option<BlockId> {
|
2021-09-02 20:21:37 +02:00
|
|
|
None
|
2021-09-02 10:25:22 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-25 06:01:02 +02:00
|
|
|
|
|
|
|
pub trait CommandClone {
|
|
|
|
fn clone_box(&self) -> Box<dyn Command>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> CommandClone for T
|
|
|
|
where
|
|
|
|
T: 'static + Command + Clone,
|
|
|
|
{
|
|
|
|
fn clone_box(&self) -> Box<dyn Command> {
|
|
|
|
Box::new(self.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for Box<dyn Command> {
|
|
|
|
fn clone(&self) -> Box<dyn Command> {
|
|
|
|
self.clone_box()
|
|
|
|
}
|
|
|
|
}
|