nushell/src/commands/command.rs

181 lines
4.2 KiB
Rust
Raw Normal View History

use crate::context::SourceMap;
use crate::context::SpanSource;
2019-05-10 18:59:12 +02:00
use crate::errors::ShellError;
use crate::object::Value;
2019-08-01 03:58:42 +02:00
use crate::parser::registry::{self, Args};
use crate::prelude::*;
2019-06-22 05:43:37 +02:00
use getset::Getters;
2019-06-27 06:56:48 +02:00
use serde::{Deserialize, Serialize};
2019-07-08 18:44:53 +02:00
use std::path::PathBuf;
use uuid::Uuid;
2019-05-10 18:59:12 +02:00
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct CallInfo {
pub args: Args,
pub source_map: SourceMap,
pub name_span: Option<Span>,
}
2019-06-22 05:43:37 +02:00
#[derive(Getters)]
#[get = "crate"]
pub struct CommandArgs {
2019-05-24 06:34:43 +02:00
pub host: Arc<Mutex<dyn Host + Send>>,
2019-07-16 21:10:25 +02:00
pub env: Arc<Mutex<Environment>>,
pub call_info: CallInfo,
pub input: InputStream,
2019-05-16 02:21:46 +02:00
}
2019-06-22 05:43:37 +02:00
impl CommandArgs {
2019-08-01 03:58:42 +02:00
pub fn nth(&self, pos: usize) -> Option<&Tagged<Value>> {
self.call_info.args.nth(pos)
2019-06-22 05:43:37 +02:00
}
2019-08-01 03:58:42 +02:00
pub fn positional_iter(&self) -> impl Iterator<Item = &Tagged<Value>> {
self.call_info.args.positional_iter()
2019-06-22 05:43:37 +02:00
}
2019-08-01 03:58:42 +02:00
pub fn expect_nth(&self, pos: usize) -> Result<&Tagged<Value>, ShellError> {
self.call_info.args.expect_nth(pos)
2019-06-22 05:43:37 +02:00
}
pub fn len(&self) -> usize {
self.call_info.args.len()
2019-06-22 05:43:37 +02:00
}
2019-08-01 03:58:42 +02:00
pub fn get(&self, name: &str) -> Option<&Tagged<Value>> {
self.call_info.args.get(name)
2019-06-22 05:43:37 +02:00
}
2019-07-16 21:10:25 +02:00
#[allow(unused)]
2019-06-22 05:43:37 +02:00
pub fn has(&self, name: &str) -> bool {
self.call_info.args.has(name)
2019-06-22 05:43:37 +02:00
}
}
2019-06-07 08:34:42 +02:00
pub struct SinkCommandArgs {
pub ctx: Context,
pub call_info: CallInfo,
2019-08-01 03:58:42 +02:00
pub input: Vec<Tagged<Value>>,
2019-06-07 08:34:42 +02:00
}
2019-06-27 06:56:48 +02:00
#[derive(Debug, Serialize, Deserialize)]
pub enum CommandAction {
2019-06-13 23:47:25 +02:00
ChangePath(PathBuf),
AddSpanSource(Uuid, SpanSource),
2019-06-13 23:47:25 +02:00
Exit,
2019-05-11 10:08:21 +02:00
}
2019-06-27 06:56:48 +02:00
#[derive(Debug, Serialize, Deserialize)]
pub enum ReturnSuccess {
2019-08-01 03:58:42 +02:00
Value(Tagged<Value>),
Action(CommandAction),
2019-05-11 10:08:21 +02:00
}
pub type ReturnValue = Result<ReturnSuccess, ShellError>;
2019-08-01 03:58:42 +02:00
impl From<Tagged<Value>> for ReturnValue {
fn from(input: Tagged<Value>) -> ReturnValue {
Ok(ReturnSuccess::Value(input))
}
}
impl ReturnSuccess {
pub fn change_cwd(path: PathBuf) -> ReturnValue {
Ok(ReturnSuccess::Action(CommandAction::ChangePath(path)))
}
2019-08-01 03:58:42 +02:00
pub fn value(input: impl Into<Tagged<Value>>) -> ReturnValue {
2019-07-08 18:44:53 +02:00
Ok(ReturnSuccess::Value(input.into()))
}
pub fn action(input: CommandAction) -> ReturnValue {
Ok(ReturnSuccess::Action(input))
}
2019-07-08 18:44:53 +02:00
pub fn spanned_value(input: Value, span: Span) -> ReturnValue {
2019-08-01 03:58:42 +02:00
Ok(ReturnSuccess::Value(Tagged::from_item(input, span)))
2019-05-11 10:08:21 +02:00
}
2019-05-10 18:59:12 +02:00
}
pub trait Command {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError>;
2019-05-28 08:45:18 +02:00
fn name(&self) -> &str;
2019-06-22 05:43:37 +02:00
fn config(&self) -> registry::CommandConfig {
registry::CommandConfig {
2019-05-28 08:45:18 +02:00
name: self.name().to_string(),
positional: vec![],
2019-05-28 08:45:18 +02:00
rest_positional: true,
named: indexmap::IndexMap::new(),
2019-07-02 09:56:20 +02:00
is_filter: true,
is_sink: false,
2019-05-28 08:45:18 +02:00
}
}
}
2019-06-07 08:34:42 +02:00
pub trait Sink {
fn run(&self, args: SinkCommandArgs) -> Result<(), ShellError>;
fn name(&self) -> &str;
2019-06-22 05:43:37 +02:00
fn config(&self) -> registry::CommandConfig {
registry::CommandConfig {
2019-06-07 08:34:42 +02:00
name: self.name().to_string(),
positional: vec![],
2019-06-07 08:34:42 +02:00
rest_positional: true,
named: indexmap::IndexMap::new(),
2019-07-02 09:56:20 +02:00
is_filter: false,
is_sink: true,
2019-06-07 08:34:42 +02:00
}
}
}
2019-05-28 08:45:18 +02:00
pub struct FnCommand {
name: String,
2019-07-03 19:37:09 +02:00
func: Box<dyn Fn(CommandArgs) -> Result<OutputStream, ShellError>>,
}
2019-05-22 09:12:03 +02:00
2019-05-28 08:45:18 +02:00
impl Command for FnCommand {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
2019-05-28 08:45:18 +02:00
(self.func)(args)
2019-05-22 09:12:03 +02:00
}
2019-05-28 08:45:18 +02:00
fn name(&self) -> &str {
&self.name
}
}
pub fn command(
name: &str,
2019-07-03 19:37:09 +02:00
func: Box<dyn Fn(CommandArgs) -> Result<OutputStream, ShellError>>,
2019-05-28 08:45:18 +02:00
) -> Arc<dyn Command> {
Arc::new(FnCommand {
name: name.to_string(),
func,
})
2019-05-22 09:12:03 +02:00
}
2019-06-07 08:34:42 +02:00
pub struct FnSink {
name: String,
2019-07-03 19:37:09 +02:00
func: Box<dyn Fn(SinkCommandArgs) -> Result<(), ShellError>>,
2019-06-07 08:34:42 +02:00
}
impl Sink for FnSink {
fn run(&self, args: SinkCommandArgs) -> Result<(), ShellError> {
(self.func)(args)
}
fn name(&self) -> &str {
&self.name
}
}
2019-07-03 19:37:09 +02:00
pub fn sink(
name: &str,
func: Box<dyn Fn(SinkCommandArgs) -> Result<(), ShellError>>,
) -> Arc<dyn Sink> {
2019-06-07 08:34:42 +02:00
Arc::new(FnSink {
name: name.to_string(),
func,
})
}