Continue prepping for object streams

This commit is contained in:
Yehuda Katz
2019-05-11 15:59:57 -07:00
parent aa3fe0b0db
commit e6da37f5be
8 changed files with 70 additions and 21 deletions

View File

@@ -1,6 +1,31 @@
use crate::Value;
use crate::object::Value;
use derive_new::new;
use std::collections::VecDeque;
#[derive(Debug, Clone)]
pub struct Args {
args: Vec<Value>,
#[derive(Debug, Default)]
pub struct ObjectStream {
queue: VecDeque<Value>,
}
#[derive(Debug, Default)]
pub struct Streams {
success: ObjectStream,
error: ObjectStream,
warning: ObjectStream,
debug: ObjectStream,
trace: ObjectStream,
verbose: ObjectStream,
}
#[derive(Debug, new)]
pub struct Args {
argv: Vec<Value>,
#[new(default)]
streams: Streams,
}
impl Args {
crate fn first(&self) -> Option<&Value> {
self.argv.first()
}
}

View File

@@ -1,6 +1,7 @@
use crate::errors::ShellError;
use crate::object::process::Process;
use crate::object::{DirEntry, ShellObject, Value};
use crate::Args;
use derive_new::new;
use std::path::{Path, PathBuf};
use sysinfo::SystemExt;
@@ -11,14 +12,20 @@ pub struct CdBlueprint;
impl crate::CommandBlueprint for CdBlueprint {
fn create(
&self,
args: Vec<String>,
args: Args,
host: &dyn crate::Host,
env: &mut crate::Environment,
) -> Box<dyn crate::Command> {
Box::new(Cd {
) -> Result<Box<dyn crate::Command>, ShellError> {
let target = match args.first() {
// TODO: This needs better infra
None => return Err(ShellError::new(format!("cd must take one arg"))),
Some(v) => v.as_string()?.clone(),
};
Ok(Box::new(Cd {
cwd: env.cwd().to_path_buf(),
target: args[0].clone(),
})
target,
}))
}
}

View File

@@ -5,10 +5,10 @@ use std::path::PathBuf;
pub trait CommandBlueprint {
fn create(
&self,
args: Vec<String>,
args: crate::Args,
host: &dyn crate::Host,
env: &mut crate::Environment,
) -> Box<dyn Command>;
) -> Result<Box<dyn Command>, ShellError>;
}
crate enum CommandAction {

View File

@@ -1,6 +1,7 @@
use crate::errors::ShellError;
use crate::object::process::Process;
use crate::object::{DirEntry, ShellObject, Value};
use crate::Args;
use crate::{Command, CommandSuccess};
use derive_new::new;
use std::path::PathBuf;
@@ -12,13 +13,13 @@ pub struct LsBlueprint;
impl crate::CommandBlueprint for LsBlueprint {
fn create(
&self,
args: Vec<String>,
args: Args,
host: &dyn crate::Host,
env: &mut crate::Environment,
) -> Box<dyn Command> {
Box::new(Ls {
) -> Result<Box<dyn Command>, ShellError> {
Ok(Box::new(Ls {
cwd: env.cwd().to_path_buf(),
})
}))
}
}

View File

@@ -15,11 +15,11 @@ pub struct PsBlueprint {
impl crate::CommandBlueprint for PsBlueprint {
fn create(
&self,
args: Vec<String>,
args: crate::Args,
host: &dyn crate::Host,
env: &mut crate::Environment,
) -> Box<dyn Command> {
Box::new(Ps::new(self.system.clone()))
) -> Result<Box<dyn Command>, ShellError> {
Ok(Box::new(Ps::new(self.system.clone())))
}
}