2019-05-10 18:59:12 +02:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::object::Value;
|
2019-06-22 05:43:37 +02:00
|
|
|
use crate::parser::{
|
|
|
|
registry::{self, Args},
|
|
|
|
Span, Spanned,
|
|
|
|
};
|
2019-05-13 19:30:51 +02:00
|
|
|
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-05-11 10:08:21 +02:00
|
|
|
use std::path::PathBuf;
|
2019-05-10 18:59:12 +02:00
|
|
|
|
2019-06-22 05:43:37 +02:00
|
|
|
#[derive(Getters)]
|
|
|
|
#[get = "crate"]
|
2019-05-23 09:23:06 +02:00
|
|
|
pub struct CommandArgs {
|
2019-05-24 06:34:43 +02:00
|
|
|
pub host: Arc<Mutex<dyn Host + Send>>,
|
2019-06-15 19:52:55 +02:00
|
|
|
pub env: Arc<Mutex<VecDeque<Environment>>>,
|
2019-06-08 00:35:07 +02:00
|
|
|
pub name_span: Option<Span>,
|
2019-06-22 05:43:37 +02:00
|
|
|
pub args: Args,
|
2019-05-23 09:23:06 +02:00
|
|
|
pub input: InputStream,
|
2019-05-16 02:21:46 +02:00
|
|
|
}
|
|
|
|
|
2019-06-22 05:43:37 +02:00
|
|
|
impl CommandArgs {
|
|
|
|
pub fn nth(&self, pos: usize) -> Option<&Spanned<Value>> {
|
|
|
|
self.args.nth(pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn positional_iter(&self) -> impl Iterator<Item = &Spanned<Value>> {
|
|
|
|
self.args.positional_iter()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expect_nth(&self, pos: usize) -> Result<&Spanned<Value>, ShellError> {
|
|
|
|
self.args.expect_nth(pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.args.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, name: &str) -> Option<&Spanned<Value>> {
|
|
|
|
self.args.get(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has(&self, name: &str) -> bool {
|
|
|
|
self.args.has(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 08:34:42 +02:00
|
|
|
pub struct SinkCommandArgs {
|
|
|
|
pub ctx: Context,
|
2019-06-08 00:35:07 +02:00
|
|
|
pub name_span: Option<Span>,
|
2019-06-22 05:43:37 +02:00
|
|
|
pub args: Args,
|
2019-06-07 08:34:42 +02:00
|
|
|
pub input: Vec<Value>,
|
|
|
|
}
|
|
|
|
|
2019-06-27 06:56:48 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2019-05-13 19:30:51 +02:00
|
|
|
pub enum CommandAction {
|
2019-06-13 23:47:25 +02:00
|
|
|
ChangePath(PathBuf),
|
|
|
|
Enter(Value),
|
|
|
|
Exit,
|
2019-05-11 10:08:21 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 06:56:48 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2019-05-13 19:30:51 +02:00
|
|
|
pub enum ReturnValue {
|
|
|
|
Value(Value),
|
|
|
|
Action(CommandAction),
|
2019-05-11 10:08:21 +02:00
|
|
|
}
|
|
|
|
|
2019-05-13 19:30:51 +02:00
|
|
|
impl ReturnValue {
|
|
|
|
crate fn change_cwd(path: PathBuf) -> ReturnValue {
|
2019-06-13 23:47:25 +02:00
|
|
|
ReturnValue::Action(CommandAction::ChangePath(path))
|
2019-05-11 10:08:21 +02:00
|
|
|
}
|
2019-05-10 18:59:12 +02:00
|
|
|
}
|
2019-05-13 19:30:51 +02:00
|
|
|
|
|
|
|
pub trait Command {
|
2019-05-23 09:23:06 +02:00
|
|
|
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(),
|
|
|
|
mandatory_positional: vec![],
|
|
|
|
optional_positional: vec![],
|
|
|
|
rest_positional: true,
|
|
|
|
named: indexmap::IndexMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
mandatory_positional: vec![],
|
|
|
|
optional_positional: vec![],
|
|
|
|
rest_positional: true,
|
|
|
|
named: indexmap::IndexMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 08:45:18 +02:00
|
|
|
pub struct FnCommand {
|
|
|
|
name: String,
|
|
|
|
func: fn(CommandArgs) -> Result<OutputStream, ShellError>,
|
2019-05-13 19:30:51 +02:00
|
|
|
}
|
2019-05-22 09:12:03 +02:00
|
|
|
|
2019-05-28 08:45:18 +02:00
|
|
|
impl Command for FnCommand {
|
2019-05-23 09:23:06 +02:00
|
|
|
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,
|
|
|
|
func: fn(CommandArgs) -> Result<OutputStream, ShellError>,
|
|
|
|
) -> 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,
|
|
|
|
func: fn(SinkCommandArgs) -> Result<(), ShellError>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sink for FnSink {
|
|
|
|
fn run(&self, args: SinkCommandArgs) -> Result<(), ShellError> {
|
|
|
|
(self.func)(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sink(name: &str, func: fn(SinkCommandArgs) -> Result<(), ShellError>) -> Arc<dyn Sink> {
|
|
|
|
Arc::new(FnSink {
|
|
|
|
name: name.to_string(),
|
|
|
|
func,
|
|
|
|
})
|
|
|
|
}
|