2019-07-03 22:31:15 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! stream {
|
|
|
|
($($expr:expr),*) => {{
|
|
|
|
let mut v = VecDeque::new();
|
|
|
|
|
|
|
|
$(
|
|
|
|
v.push_back($expr);
|
|
|
|
)*
|
|
|
|
|
|
|
|
v
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! trace_stream {
|
2019-07-12 21:22:08 +02:00
|
|
|
(target: $target:tt, $desc:tt = $expr:expr) => {{
|
|
|
|
if log::log_enabled!(target: $target, log::Level::Trace) {
|
2019-07-03 22:31:15 +02:00
|
|
|
use futures::stream::StreamExt;
|
|
|
|
|
|
|
|
let objects = $expr.values.inspect(|o| {
|
2019-07-12 21:22:08 +02:00
|
|
|
trace!(target: $target, "{} = {:#?}", $desc, o.debug());
|
2019-07-03 22:31:15 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$crate::stream::InputStream::from_stream(objects.boxed())
|
|
|
|
} else {
|
|
|
|
$expr
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
Add support for ~ expansion
This ended up being a bit of a yak shave. The basic idea in this commit is to
expand `~` in paths, but only in paths.
The way this is accomplished is by doing the expansion inside of the code that
parses literal syntax for `SyntaxType::Path`.
As a quick refresher: every command is entitled to expand its arguments in a
custom way. While this could in theory be used for general-purpose macros,
today the expansion facility is limited to syntactic hints.
For example, the syntax `where cpu > 0` expands under the hood to
`where { $it.cpu > 0 }`. This happens because the first argument to `where`
is defined as a `SyntaxType::Block`, and the parser coerces binary expressions
whose left-hand-side looks like a member into a block when the command is
expecting one.
This is mildly more magical than what most programming languages would do,
but we believe that it makes sense to allow commands to fine-tune the syntax
because of the domain nushell is in (command-line shells).
The syntactic expansions supported by this facility are relatively limited.
For example, we don't allow `$it` to become a bare word, simply because the
command asks for a string in the relevant position. That would quickly
become more confusing than it's worth.
This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command
declares a parameter as a `SyntaxType::Path`, string literals and bare
words passed as an argument to that parameter are processed using the
path expansion rules. Right now, that only means that `~` is expanded into
the home directory, but additional rules are possible in the future.
By restricting this expansion to a syntactic expansion when passed as an
argument to a command expecting a path, we avoid making `~` a generally
reserved character. This will also allow us to give good tab completion
for paths with `~` characters in them when a command is expecting a path.
In order to accomplish the above, this commit changes the parsing functions
to take a `Context` instead of just a `CommandRegistry`. From the perspective
of macro expansion, you can think of the `CommandRegistry` as a dictionary
of in-scope macros, and the `Context` as the compile-time state used in
expansion. This could gain additional functionality over time as we find
more uses for the expansion system.
2019-08-26 21:21:03 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! trace_out_stream {
|
|
|
|
(target: $target:tt, source: $source:expr, $desc:tt = $expr:expr) => {{
|
|
|
|
if log::log_enabled!(target: $target, log::Level::Trace) {
|
|
|
|
use futures::stream::StreamExt;
|
|
|
|
|
|
|
|
let source = $source.clone();
|
|
|
|
|
|
|
|
let objects = $expr.values.inspect(move |o| {
|
|
|
|
trace!(target: $target, "{} = {}", $desc, o.debug(&source));
|
|
|
|
});
|
|
|
|
|
|
|
|
$crate::stream::OutputStream::new(objects)
|
|
|
|
} else {
|
|
|
|
$expr
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use crate::cli::MaybeOwned;
|
|
|
|
pub(crate) use crate::commands::command::{
|
2019-08-15 07:02:02 +02:00
|
|
|
CallInfo, CommandAction, CommandArgs, ReturnSuccess, ReturnValue, RunnableContext,
|
2019-07-03 22:31:15 +02:00
|
|
|
};
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use crate::commands::PerItemCommand;
|
2019-08-31 00:13:09 +02:00
|
|
|
pub(crate) use crate::commands::RawCommandArgs;
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use crate::context::CommandRegistry;
|
2019-09-29 07:18:59 +02:00
|
|
|
pub(crate) use crate::context::{AnchorLocation, Context};
|
2019-09-05 18:23:42 +02:00
|
|
|
pub(crate) use crate::data::base as value;
|
|
|
|
pub(crate) use crate::data::meta::{Tag, Tagged, TaggedItem};
|
|
|
|
pub(crate) use crate::data::types::ExtractType;
|
|
|
|
pub(crate) use crate::data::{Primitive, Value};
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use crate::env::host::handle_unexpected;
|
|
|
|
pub(crate) use crate::env::Host;
|
2019-09-01 18:20:31 +02:00
|
|
|
pub(crate) use crate::errors::{CoerceInto, ShellError};
|
2019-09-14 18:30:24 +02:00
|
|
|
pub(crate) use crate::parser::hir::SyntaxShape;
|
2019-08-30 19:29:04 +02:00
|
|
|
pub(crate) use crate::parser::parse::parser::Number;
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use crate::parser::registry::Signature;
|
|
|
|
pub(crate) use crate::shell::filesystem_shell::FilesystemShell;
|
2019-09-05 18:23:42 +02:00
|
|
|
pub(crate) use crate::shell::help_shell::HelpShell;
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use crate::shell::shell_manager::ShellManager;
|
|
|
|
pub(crate) use crate::shell::value_shell::ValueShell;
|
|
|
|
pub(crate) use crate::stream::{InputStream, OutputStream};
|
2019-09-14 18:30:24 +02:00
|
|
|
pub(crate) use crate::traits::{HasTag, ToDebug};
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use crate::Text;
|
2019-09-26 02:22:17 +02:00
|
|
|
pub(crate) use async_stream::stream as async_stream;
|
2019-09-01 18:20:31 +02:00
|
|
|
pub(crate) use bigdecimal::BigDecimal;
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use futures::stream::BoxStream;
|
|
|
|
pub(crate) use futures::{FutureExt, Stream, StreamExt};
|
2019-09-01 18:20:31 +02:00
|
|
|
pub(crate) use num_bigint::BigInt;
|
2019-08-30 19:29:04 +02:00
|
|
|
pub(crate) use num_traits::cast::{FromPrimitive, ToPrimitive};
|
2019-09-01 18:20:31 +02:00
|
|
|
pub(crate) use num_traits::identities::Zero;
|
|
|
|
pub(crate) use serde::Deserialize;
|
2019-08-29 13:08:28 +02:00
|
|
|
pub(crate) use std::collections::VecDeque;
|
|
|
|
pub(crate) use std::future::Future;
|
|
|
|
pub(crate) use std::sync::{Arc, Mutex};
|
2019-07-03 22:31:15 +02:00
|
|
|
|
|
|
|
pub trait FromInputStream {
|
|
|
|
fn from_input_stream(self) -> OutputStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> FromInputStream for T
|
|
|
|
where
|
2019-08-01 03:58:42 +02:00
|
|
|
T: Stream<Item = Tagged<Value>> + Send + 'static,
|
2019-07-03 22:31:15 +02:00
|
|
|
{
|
|
|
|
fn from_input_stream(self) -> OutputStream {
|
|
|
|
OutputStream {
|
|
|
|
values: self.map(ReturnSuccess::value).boxed(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ToOutputStream {
|
|
|
|
fn to_output_stream(self) -> OutputStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, U> ToOutputStream for T
|
|
|
|
where
|
|
|
|
T: Stream<Item = U> + Send + 'static,
|
|
|
|
U: Into<ReturnValue>,
|
|
|
|
{
|
|
|
|
fn to_output_stream(self) -> OutputStream {
|
|
|
|
OutputStream {
|
|
|
|
values: self.map(|item| item.into()).boxed(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|