Move to* and from* to engine-p (#3320)

* WIP

* Finish last batch
This commit is contained in:
JT
2021-04-15 19:43:33 +12:00
committed by GitHub
parent fd7875e572
commit f73732bf1e
49 changed files with 299 additions and 445 deletions

View File

@ -229,11 +229,12 @@ impl EvaluatedCommandArgs {
.ok_or_else(|| ShellError::unimplemented("Better error: expect_nth"))
}
pub fn get_flag<T: FromValue>(&self, name: &str) -> Option<Result<T, ShellError>> {
self.call_info
.args
.get(name)
.map(|x| FromValue::from_value(x))
pub fn get_flag<T: FromValue>(&self, name: &str) -> Result<Option<T>, ShellError> {
if let Some(arg) = self.call_info.args.get(name) {
FromValue::from_value(arg).map(Some)
} else {
Ok(None)
}
}
pub fn req_named<T: FromValue>(&self, name: &str) -> Result<T, ShellError> {
@ -259,11 +260,11 @@ impl EvaluatedCommandArgs {
}
}
pub fn opt<T: FromValue>(&self, pos: usize) -> Option<Result<T, ShellError>> {
pub fn opt<T: FromValue>(&self, pos: usize) -> Result<Option<T>, ShellError> {
if let Some(v) = self.nth(pos) {
Some(FromValue::from_value(v))
FromValue::from_value(v).map(Some)
} else {
None
Ok(None)
}
}

View File

@ -6,9 +6,8 @@ use nu_parser::ParserScope;
use nu_protocol::hir::{
Block, Call, ClassifiedCommand, Expression, Pipeline, SpannedExpression, Synthetic,
};
use nu_protocol::{ReturnSuccess, UntaggedValue, Value};
use nu_protocol::{UntaggedValue, Value};
use nu_source::{Span, Tag};
use nu_stream::ToActionStream;
use nu_stream::{InputStream, OutputStream};
use std::sync::atomic::Ordering;
@ -79,17 +78,15 @@ pub fn run_block(
for pipeline in &group.pipelines {
match output {
Ok(inp) if inp.is_empty() => {}
Ok(inp) => {
let mut output_stream = inp.to_action_stream();
Ok(mut output_stream) => {
match output_stream.next() {
Some(Ok(ReturnSuccess::Value(Value {
Some(Value {
value: UntaggedValue::Error(e),
..
}))) => {
}) => {
return Err(e);
}
Some(Ok(_item)) => {
Some(_item) => {
if let Some(err) = ctx.get_errors().get(0) {
ctx.clear_errors();
return Err(err.clone());
@ -109,9 +106,6 @@ pub fn run_block(
return Err(err.clone());
}
}
Some(Err(e)) => {
return Err(e);
}
}
}
Err(e) => {

View File

@ -9,7 +9,7 @@ use encoding_rs::Encoding;
use nu_data::config::LocalConfigDiff;
use nu_protocol::{CommandAction, ConfigPath, TaggedDictBuilder, Value};
use nu_source::{Span, Tag};
use nu_stream::{ActionStream, Interruptible, ToActionStream};
use nu_stream::{ActionStream, Interruptible, OutputStream, ToActionStream};
use std::collections::VecDeque;
use std::io::{Error, ErrorKind};
use std::path::{Path, PathBuf};
@ -860,9 +860,9 @@ impl Shell for FilesystemShell {
full_path: &Path,
save_data: &[u8],
name: Span,
) -> Result<ActionStream, ShellError> {
) -> Result<OutputStream, ShellError> {
match std::fs::write(full_path, save_data) {
Ok(_) => Ok(ActionStream::empty()),
Ok(_) => Ok(OutputStream::empty()),
Err(e) => Err(ShellError::labeled_error(
e.to_string(),
"IO error while saving",

View File

@ -166,6 +166,26 @@ impl FromValue for PathBuf {
}
}
impl FromValue for Tagged<PathBuf> {
fn from_value(v: &Value) -> Result<Self, ShellError> {
match v {
Value {
value: UntaggedValue::Primitive(Primitive::String(s)),
tag,
} => Ok(PathBuf::from(s).tagged(tag)),
Value {
value: UntaggedValue::Primitive(Primitive::FilePath(p)),
tag,
} => Ok(p.clone().tagged(tag)),
Value { tag, .. } => Err(ShellError::labeled_error(
"Can't convert to filepath",
"can't convert to filepath",
tag.span,
)),
}
}
}
impl FromValue for ColumnPath {
fn from_value(v: &Value) -> Result<Self, ShellError> {
match v {

View File

@ -1,4 +1,4 @@
use nu_stream::ActionStream;
use nu_stream::{ActionStream, OutputStream};
use crate::command_args::EvaluatedWholeStreamCommandArgs;
use crate::maybe_text_codec::StringOrBinary;
@ -49,5 +49,5 @@ pub trait Shell: std::fmt::Debug {
path: &Path,
contents: &[u8],
name: Span,
) -> Result<ActionStream, ShellError>;
) -> Result<OutputStream, ShellError>;
}

View File

@ -1,7 +1,7 @@
use crate::shell::Shell;
use crate::{command_args::EvaluatedWholeStreamCommandArgs, FilesystemShell};
use crate::{filesystem::filesystem_shell::FilesystemShellMode, maybe_text_codec::StringOrBinary};
use nu_stream::ActionStream;
use nu_stream::{ActionStream, OutputStream};
use crate::shell::shell_args::{CdArgs, CopyArgs, LsArgs, MkdirArgs, MvArgs, RemoveArgs};
use encoding_rs::Encoding;
@ -96,7 +96,7 @@ impl ShellManager {
full_path: &Path,
save_data: &[u8],
name: Span,
) -> Result<ActionStream, ShellError> {
) -> Result<OutputStream, ShellError> {
self.shells.lock()[self.current_shell()].save(full_path, save_data, name)
}

View File

@ -8,7 +8,7 @@ use nu_protocol::ValueStructure;
use nu_protocol::{ReturnSuccess, ShellTypeName, UntaggedValue, Value};
use nu_source::SpannedItem;
use nu_source::{Span, Tag, Tagged};
use nu_stream::ActionStream;
use nu_stream::{ActionStream, OutputStream};
use nu_value_ext::ValueExt;
use std::collections::VecDeque;
use std::ffi::OsStr;
@ -247,7 +247,7 @@ impl Shell for ValueShell {
_path: &Path,
_contents: &[u8],
_name: Span,
) -> Result<ActionStream, ShellError> {
) -> Result<OutputStream, ShellError> {
Err(ShellError::unimplemented(
"save on help shell is not supported",
))