nushell/crates/nu-protocol/src/engine/command.rs

79 lines
1.6 KiB
Rust
Raw Normal View History

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;
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
}
fn examples(&self) -> Vec<Example> {
Vec::new()
}
// This is a built-in command
fn is_builtin(&self) -> bool {
true
}
// This is a signature for a known external command
fn is_known_external(&self) -> bool {
false
}
2021-09-02 10:25:22 +02:00
// Is a sub command
fn is_sub(&self) -> bool {
self.name().contains(' ')
}
// 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>)> {
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()
}
}