2020-01-17 23:46:18 +01:00
|
|
|
use crate::commands::help::get_help;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use crate::context::CommandRegistry;
|
|
|
|
use crate::deserializer::ConfigDeserializer;
|
|
|
|
use crate::evaluate::evaluate_args::evaluate_args;
|
2019-05-13 19:30:51 +02:00
|
|
|
use crate::prelude::*;
|
2019-08-02 21:15:07 +02:00
|
|
|
use derive_new::new;
|
2019-06-22 05:43:37 +02:00
|
|
|
use getset::Getters;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
2020-04-13 09:59:57 +02:00
|
|
|
use nu_protocol::hir;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_protocol::{CallInfo, EvaluatedArgs, ReturnValue, Scope, Signature, Value};
|
2019-06-27 06:56:48 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-07-24 00:22:11 +02:00
|
|
|
use std::ops::Deref;
|
2019-10-13 06:12:43 +02:00
|
|
|
use std::sync::atomic::AtomicBool;
|
2019-05-10 18:59:12 +02:00
|
|
|
|
2019-07-24 00:22:11 +02:00
|
|
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
|
|
|
pub struct UnevaluatedCallInfo {
|
|
|
|
pub args: hir::Call,
|
2019-09-14 18:30:24 +02:00
|
|
|
pub name_tag: Tag,
|
2020-04-15 07:43:23 +02:00
|
|
|
pub scope: Scope,
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UnevaluatedCallInfo {
|
2020-04-15 07:43:23 +02:00
|
|
|
pub fn evaluate(self, registry: &CommandRegistry) -> Result<CallInfo, ShellError> {
|
|
|
|
let args = evaluate_args(&self.args, registry, &self.scope)?;
|
|
|
|
|
|
|
|
Ok(CallInfo {
|
|
|
|
args,
|
|
|
|
name_tag: self.name_tag,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn evaluate_with_new_it(
|
2019-07-24 00:22:11 +02:00
|
|
|
self,
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
registry: &CommandRegistry,
|
2020-04-15 07:43:23 +02:00
|
|
|
it: &Value,
|
2019-07-24 00:22:11 +02:00
|
|
|
) -> Result<CallInfo, ShellError> {
|
2020-04-15 07:43:23 +02:00
|
|
|
let mut scope = self.scope.clone();
|
|
|
|
scope = scope.set_it(it.clone());
|
|
|
|
let args = evaluate_args(&self.args, registry, &scope)?;
|
2019-07-24 00:22:11 +02:00
|
|
|
|
|
|
|
Ok(CallInfo {
|
|
|
|
args,
|
2019-09-14 18:30:24 +02:00
|
|
|
name_tag: self.name_tag,
|
2019-07-24 00:22:11 +02:00
|
|
|
})
|
|
|
|
}
|
2020-01-17 23:46:18 +01:00
|
|
|
|
|
|
|
pub fn switch_present(&self, switch: &str) -> bool {
|
|
|
|
self.args.switch_preset(switch)
|
|
|
|
}
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
2019-06-22 05:43:37 +02:00
|
|
|
#[derive(Getters)]
|
2019-09-01 21:56:17 +02:00
|
|
|
#[get = "pub(crate)"]
|
2019-05-23 09:23:06 +02:00
|
|
|
pub struct CommandArgs {
|
2020-01-04 07:44:17 +01:00
|
|
|
pub host: Arc<parking_lot::Mutex<Box<dyn Host>>>,
|
2019-10-13 06:12:43 +02:00
|
|
|
pub ctrl_c: Arc<AtomicBool>,
|
2019-08-07 19:49:11 +02:00
|
|
|
pub shell_manager: ShellManager,
|
2019-07-24 00:22:11 +02:00
|
|
|
pub call_info: UnevaluatedCallInfo,
|
2019-05-23 09:23:06 +02:00
|
|
|
pub input: InputStream,
|
2019-05-16 02:21:46 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 19:02:39 +02:00
|
|
|
#[derive(Getters, Clone)]
|
2019-09-01 21:56:17 +02:00
|
|
|
#[get = "pub(crate)"]
|
2019-08-03 04:17:28 +02:00
|
|
|
pub struct RawCommandArgs {
|
2020-01-04 07:44:17 +01:00
|
|
|
pub host: Arc<parking_lot::Mutex<Box<dyn Host>>>,
|
2019-10-13 06:12:43 +02:00
|
|
|
pub ctrl_c: Arc<AtomicBool>,
|
2019-08-09 06:51:21 +02:00
|
|
|
pub shell_manager: ShellManager,
|
2019-08-03 04:17:28 +02:00
|
|
|
pub call_info: UnevaluatedCallInfo,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RawCommandArgs {
|
2020-01-25 04:13:12 +01:00
|
|
|
pub fn with_input(self, input: impl Into<InputStream>) -> CommandArgs {
|
2019-08-03 04:17:28 +02:00
|
|
|
CommandArgs {
|
|
|
|
host: self.host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c: self.ctrl_c,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager: self.shell_manager,
|
2019-08-03 04:17:28 +02:00
|
|
|
call_info: self.call_info,
|
|
|
|
input: input.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:46:50 +01:00
|
|
|
impl std::fmt::Debug for CommandArgs {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
self.call_info.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 05:43:37 +02:00
|
|
|
impl CommandArgs {
|
2019-07-24 00:22:11 +02:00
|
|
|
pub fn evaluate_once(
|
|
|
|
self,
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
registry: &CommandRegistry,
|
2019-08-15 07:02:02 +02:00
|
|
|
) -> Result<EvaluatedWholeStreamCommandArgs, ShellError> {
|
2019-07-24 00:22:11 +02:00
|
|
|
let host = self.host.clone();
|
2019-10-13 06:12:43 +02:00
|
|
|
let ctrl_c = self.ctrl_c.clone();
|
2019-08-09 06:51:21 +02:00
|
|
|
let shell_manager = self.shell_manager.clone();
|
2019-07-24 00:22:11 +02:00
|
|
|
let input = self.input;
|
2020-04-15 07:43:23 +02:00
|
|
|
let call_info = self.call_info.evaluate(registry)?;
|
2019-07-24 00:22:11 +02:00
|
|
|
|
2019-08-15 07:02:02 +02:00
|
|
|
Ok(EvaluatedWholeStreamCommandArgs::new(
|
2019-08-09 06:51:21 +02:00
|
|
|
host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
|
|
|
call_info,
|
|
|
|
input,
|
|
|
|
))
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
2019-12-07 05:23:59 +01:00
|
|
|
pub fn evaluate_once_with_scope(
|
|
|
|
self,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
scope: &Scope,
|
|
|
|
) -> Result<EvaluatedWholeStreamCommandArgs, ShellError> {
|
|
|
|
let host = self.host.clone();
|
|
|
|
let ctrl_c = self.ctrl_c.clone();
|
|
|
|
let shell_manager = self.shell_manager.clone();
|
|
|
|
let input = self.input;
|
2020-04-15 07:43:23 +02:00
|
|
|
let call_info = UnevaluatedCallInfo {
|
|
|
|
name_tag: self.call_info.name_tag,
|
|
|
|
args: self.call_info.args,
|
|
|
|
scope: scope.clone(),
|
|
|
|
};
|
|
|
|
let call_info = call_info.evaluate(registry)?;
|
2019-12-07 05:23:59 +01:00
|
|
|
|
|
|
|
Ok(EvaluatedWholeStreamCommandArgs::new(
|
|
|
|
host,
|
|
|
|
ctrl_c,
|
|
|
|
shell_manager,
|
|
|
|
call_info,
|
|
|
|
input,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2019-11-04 16:47:03 +01:00
|
|
|
pub fn process<'de, T: Deserialize<'de>, O: ToOutputStream>(
|
2019-08-02 21:15:07 +02:00
|
|
|
self,
|
|
|
|
registry: &CommandRegistry,
|
2019-11-04 16:47:03 +01:00
|
|
|
callback: fn(T, RunnableContext) -> Result<O, ShellError>,
|
|
|
|
) -> Result<RunnableArgs<T, O>, ShellError> {
|
2019-08-09 06:51:21 +02:00
|
|
|
let shell_manager = self.shell_manager.clone();
|
2019-08-03 04:17:28 +02:00
|
|
|
let host = self.host.clone();
|
2019-10-13 06:12:43 +02:00
|
|
|
let ctrl_c = self.ctrl_c.clone();
|
2019-08-02 21:15:07 +02:00
|
|
|
let args = self.evaluate_once(registry)?;
|
2019-10-13 06:12:43 +02:00
|
|
|
let call_info = args.call_info.clone();
|
2019-08-02 21:15:07 +02:00
|
|
|
let (input, args) = args.split();
|
2019-09-14 18:30:24 +02:00
|
|
|
let name_tag = args.call_info.name_tag;
|
2019-10-13 06:12:43 +02:00
|
|
|
let mut deserializer = ConfigDeserializer::from_call_info(call_info);
|
2019-08-02 21:15:07 +02:00
|
|
|
|
|
|
|
Ok(RunnableArgs {
|
|
|
|
args: T::deserialize(&mut deserializer)?,
|
|
|
|
context: RunnableContext {
|
2019-09-01 23:39:59 +02:00
|
|
|
input,
|
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
|
|
|
registry: registry.clone(),
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
2019-09-14 18:30:24 +02:00
|
|
|
name: name_tag,
|
2019-08-03 04:17:28 +02:00
|
|
|
host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c,
|
2019-08-03 04:17:28 +02:00
|
|
|
},
|
|
|
|
callback,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_raw<'de, T: Deserialize<'de>>(
|
|
|
|
self,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
callback: fn(T, RunnableContext, RawCommandArgs) -> Result<OutputStream, ShellError>,
|
|
|
|
) -> Result<RunnableRawArgs<T>, ShellError> {
|
|
|
|
let raw_args = RawCommandArgs {
|
|
|
|
host: self.host.clone(),
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c: self.ctrl_c.clone(),
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager: self.shell_manager.clone(),
|
2019-08-03 04:17:28 +02:00
|
|
|
call_info: self.call_info.clone(),
|
|
|
|
};
|
|
|
|
|
2019-08-09 06:51:21 +02:00
|
|
|
let shell_manager = self.shell_manager.clone();
|
2019-08-03 04:17:28 +02:00
|
|
|
let host = self.host.clone();
|
2019-10-13 06:12:43 +02:00
|
|
|
let ctrl_c = self.ctrl_c.clone();
|
2019-08-03 04:17:28 +02:00
|
|
|
let args = self.evaluate_once(registry)?;
|
2019-10-13 06:12:43 +02:00
|
|
|
let call_info = args.call_info.clone();
|
|
|
|
|
2019-08-03 04:17:28 +02:00
|
|
|
let (input, args) = args.split();
|
2019-09-14 18:30:24 +02:00
|
|
|
let name_tag = args.call_info.name_tag;
|
2019-12-31 08:36:08 +01:00
|
|
|
let mut deserializer = ConfigDeserializer::from_call_info(call_info);
|
2019-08-03 04:17:28 +02:00
|
|
|
|
|
|
|
Ok(RunnableRawArgs {
|
|
|
|
args: T::deserialize(&mut deserializer)?,
|
|
|
|
context: RunnableContext {
|
2019-09-01 23:39:59 +02:00
|
|
|
input,
|
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
|
|
|
registry: registry.clone(),
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
2019-09-14 18:30:24 +02:00
|
|
|
name: name_tag,
|
2019-08-03 04:17:28 +02:00
|
|
|
host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c,
|
2019-08-02 21:15:07 +02:00
|
|
|
},
|
2019-08-03 04:17:28 +02:00
|
|
|
raw_args,
|
2019-08-02 21:15:07 +02:00
|
|
|
callback,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RunnableContext {
|
|
|
|
pub input: InputStream,
|
2019-08-09 06:51:21 +02:00
|
|
|
pub shell_manager: ShellManager,
|
2020-01-04 07:44:17 +01:00
|
|
|
pub host: Arc<parking_lot::Mutex<Box<dyn Host>>>,
|
2019-10-13 06:12:43 +02:00
|
|
|
pub ctrl_c: Arc<AtomicBool>,
|
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
|
|
|
pub registry: CommandRegistry,
|
2019-09-14 18:30:24 +02:00
|
|
|
pub name: Tag,
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RunnableContext {
|
2020-04-30 04:23:40 +02:00
|
|
|
pub fn get_command(&self, name: &str) -> Option<Command> {
|
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
|
|
|
self.registry.get_command(name)
|
2019-09-07 09:32:07 +02:00
|
|
|
}
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
|
|
|
|
2019-11-04 16:47:03 +01:00
|
|
|
pub struct RunnableArgs<T, O: ToOutputStream> {
|
2019-08-02 21:15:07 +02:00
|
|
|
args: T,
|
|
|
|
context: RunnableContext,
|
2019-11-04 16:47:03 +01:00
|
|
|
callback: fn(T, RunnableContext) -> Result<O, ShellError>,
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
|
|
|
|
2019-11-04 16:47:03 +01:00
|
|
|
impl<T, O: ToOutputStream> RunnableArgs<T, O> {
|
2019-08-02 21:15:07 +02:00
|
|
|
pub fn run(self) -> Result<OutputStream, ShellError> {
|
2019-11-04 16:47:03 +01:00
|
|
|
(self.callback)(self.args, self.context).map(|v| v.to_output_stream())
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-03 04:17:28 +02:00
|
|
|
pub struct RunnableRawArgs<T> {
|
|
|
|
args: T,
|
|
|
|
raw_args: RawCommandArgs,
|
|
|
|
context: RunnableContext,
|
|
|
|
callback: fn(T, RunnableContext, RawCommandArgs) -> Result<OutputStream, ShellError>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> RunnableRawArgs<T> {
|
2019-08-17 05:53:39 +02:00
|
|
|
pub fn run(self) -> OutputStream {
|
|
|
|
match (self.callback)(self.args, self.context, self.raw_args) {
|
|
|
|
Ok(stream) => stream,
|
|
|
|
Err(err) => OutputStream::one(Err(err)),
|
|
|
|
}
|
2019-08-03 04:17:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 07:02:02 +02:00
|
|
|
pub struct EvaluatedWholeStreamCommandArgs {
|
2019-07-24 00:22:11 +02:00
|
|
|
pub args: EvaluatedCommandArgs,
|
|
|
|
pub input: InputStream,
|
|
|
|
}
|
|
|
|
|
2019-08-15 07:02:02 +02:00
|
|
|
impl Deref for EvaluatedWholeStreamCommandArgs {
|
2019-07-24 00:22:11 +02:00
|
|
|
type Target = EvaluatedCommandArgs;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.args
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 07:02:02 +02:00
|
|
|
impl EvaluatedWholeStreamCommandArgs {
|
2019-07-24 00:22:11 +02:00
|
|
|
pub fn new(
|
2020-01-04 07:44:17 +01:00
|
|
|
host: Arc<parking_lot::Mutex<dyn Host>>,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c: Arc<AtomicBool>,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager: ShellManager,
|
2019-07-24 00:22:11 +02:00
|
|
|
call_info: CallInfo,
|
|
|
|
input: impl Into<InputStream>,
|
2019-08-15 07:02:02 +02:00
|
|
|
) -> EvaluatedWholeStreamCommandArgs {
|
|
|
|
EvaluatedWholeStreamCommandArgs {
|
2019-07-24 00:22:11 +02:00
|
|
|
args: EvaluatedCommandArgs {
|
|
|
|
host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
2019-07-24 00:22:11 +02:00
|
|
|
call_info,
|
|
|
|
},
|
|
|
|
input: input.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 18:30:24 +02:00
|
|
|
pub fn name_tag(&self) -> Tag {
|
2019-10-13 06:12:43 +02:00
|
|
|
self.args.call_info.name_tag.clone()
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
pub fn parts(self) -> (InputStream, EvaluatedArgs) {
|
2019-08-15 07:02:02 +02:00
|
|
|
let EvaluatedWholeStreamCommandArgs { args, input } = self;
|
2019-07-24 00:22:11 +02:00
|
|
|
|
|
|
|
(input, args.call_info.args)
|
|
|
|
}
|
2019-08-02 21:15:07 +02:00
|
|
|
|
|
|
|
pub fn split(self) -> (InputStream, EvaluatedCommandArgs) {
|
2019-08-15 07:02:02 +02:00
|
|
|
let EvaluatedWholeStreamCommandArgs { args, input } = self;
|
2019-08-02 21:15:07 +02:00
|
|
|
|
|
|
|
(input, args)
|
|
|
|
}
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Getters)]
|
|
|
|
#[get = "pub"]
|
|
|
|
pub struct EvaluatedFilterCommandArgs {
|
|
|
|
args: EvaluatedCommandArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for EvaluatedFilterCommandArgs {
|
|
|
|
type Target = EvaluatedCommandArgs;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.args
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EvaluatedFilterCommandArgs {
|
|
|
|
pub fn new(
|
2020-01-04 07:44:17 +01:00
|
|
|
host: Arc<parking_lot::Mutex<dyn Host>>,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c: Arc<AtomicBool>,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager: ShellManager,
|
2019-07-24 00:22:11 +02:00
|
|
|
call_info: CallInfo,
|
|
|
|
) -> EvaluatedFilterCommandArgs {
|
|
|
|
EvaluatedFilterCommandArgs {
|
|
|
|
args: EvaluatedCommandArgs {
|
|
|
|
host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
2019-07-24 00:22:11 +02:00
|
|
|
call_info,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
#[derive(Getters, new)]
|
2019-09-01 21:56:17 +02:00
|
|
|
#[get = "pub(crate)"]
|
2019-07-24 00:22:11 +02:00
|
|
|
pub struct EvaluatedCommandArgs {
|
2020-01-04 07:44:17 +01:00
|
|
|
pub host: Arc<parking_lot::Mutex<dyn Host>>,
|
2019-10-13 06:12:43 +02:00
|
|
|
pub ctrl_c: Arc<AtomicBool>,
|
2019-08-09 06:51:21 +02:00
|
|
|
pub shell_manager: ShellManager,
|
2019-07-24 00:22:11 +02:00
|
|
|
pub call_info: CallInfo,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EvaluatedCommandArgs {
|
2019-11-21 15:33:14 +01:00
|
|
|
pub fn nth(&self, pos: usize) -> Option<&Value> {
|
2019-07-20 04:27:10 +02:00
|
|
|
self.call_info.args.nth(pos)
|
2019-06-22 05:43:37 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 09:59:57 +02:00
|
|
|
/// Get the nth positional argument, error if not possible
|
|
|
|
pub fn expect_nth(&self, pos: usize) -> Result<&Value, ShellError> {
|
|
|
|
match self.call_info.args.nth(pos) {
|
|
|
|
None => Err(ShellError::unimplemented("Better error: expect_nth")),
|
|
|
|
Some(item) => Ok(item),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
pub fn get(&self, name: &str) -> Option<&Value> {
|
2019-07-20 04:27:10 +02:00
|
|
|
self.call_info.args.get(name)
|
2019-06-22 05:43:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has(&self, name: &str) -> bool {
|
2019-07-20 04:27:10 +02:00
|
|
|
self.call_info.args.has(name)
|
2019-06-22 05:43:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 07:02:02 +02:00
|
|
|
pub trait WholeStreamCommand: Send + Sync {
|
2019-08-02 21:15:07 +02:00
|
|
|
fn name(&self) -> &str;
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2019-12-04 22:14:52 +01:00
|
|
|
Signature::new(self.name()).desc(self.usage()).filter()
|
2019-05-28 08:45:18 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 00:52:32 +02:00
|
|
|
fn usage(&self) -> &str;
|
2019-08-14 19:02:39 +02:00
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
2019-08-30 00:52:32 +02:00
|
|
|
args: CommandArgs,
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
registry: &CommandRegistry,
|
2019-08-24 21:36:19 +02:00
|
|
|
) -> Result<OutputStream, ShellError>;
|
2019-09-04 03:50:23 +02:00
|
|
|
|
|
|
|
fn is_binary(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
2019-08-30 00:52:32 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 04:23:40 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Command(Arc<dyn WholeStreamCommand>);
|
2019-06-07 08:34:42 +02:00
|
|
|
|
2019-11-21 15:33:14 +01:00
|
|
|
impl PrettyDebugWithSource for Command {
|
|
|
|
fn pretty_debug(&self, source: &str) -> DebugDocBuilder {
|
2020-04-30 04:23:40 +02:00
|
|
|
b::typed(
|
|
|
|
"whole stream command",
|
|
|
|
b::description(self.name())
|
|
|
|
+ b::space()
|
|
|
|
+ b::equals()
|
|
|
|
+ b::space()
|
|
|
|
+ self.signature().pretty_debug(source),
|
|
|
|
)
|
2019-11-21 15:33:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Overhaul the expansion system
The main thrust of this (very large) commit is an overhaul of the
expansion system.
The parsing pipeline is:
- Lightly parse the source file for atoms, basic delimiters and pipeline
structure into a token tree
- Expand the token tree into a HIR (high-level intermediate
representation) based upon the baseline syntax rules for expressions
and the syntactic shape of commands.
Somewhat non-traditionally, nu doesn't have an AST at all. It goes
directly from the token tree, which doesn't represent many important
distinctions (like the difference between `hello` and `5KB`) directly
into a high-level representation that doesn't have a direct
correspondence to the source code.
At a high level, nu commands work like macros, in the sense that the
syntactic shape of the invocation of a command depends on the
definition of a command.
However, commands do not have the ability to perform unrestricted
expansions of the token tree. Instead, they describe their arguments in
terms of syntactic shapes, and the expander expands the token tree into
HIR based upon that definition.
For example, the `where` command says that it takes a block as its first
required argument, and the description of the block syntactic shape
expands the syntax `cpu > 10` into HIR that represents
`{ $it.cpu > 10 }`.
This commit overhauls that system so that the syntactic shapes are
described in terms of a few new traits (`ExpandSyntax` and
`ExpandExpression` are the primary ones) that are more composable than
the previous system.
The first big win of this new system is the addition of the `ColumnPath`
shape, which looks like `cpu."max ghz"` or `package.version`.
Previously, while a variable path could look like `$it.cpu."max ghz"`,
the tail of a variable path could not be easily reused in other
contexts. Now, that tail is its own syntactic shape, and it can be used
as part of a command's signature.
This cleans up commands like `inc`, `add` and `edit` as well as
shorthand blocks, which can now look like `| where cpu."max ghz" > 10`
2019-09-18 00:26:27 +02:00
|
|
|
impl std::fmt::Debug for Command {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2020-04-30 04:23:40 +02:00
|
|
|
write!(f, "Command({})", self.name())
|
Overhaul the expansion system
The main thrust of this (very large) commit is an overhaul of the
expansion system.
The parsing pipeline is:
- Lightly parse the source file for atoms, basic delimiters and pipeline
structure into a token tree
- Expand the token tree into a HIR (high-level intermediate
representation) based upon the baseline syntax rules for expressions
and the syntactic shape of commands.
Somewhat non-traditionally, nu doesn't have an AST at all. It goes
directly from the token tree, which doesn't represent many important
distinctions (like the difference between `hello` and `5KB`) directly
into a high-level representation that doesn't have a direct
correspondence to the source code.
At a high level, nu commands work like macros, in the sense that the
syntactic shape of the invocation of a command depends on the
definition of a command.
However, commands do not have the ability to perform unrestricted
expansions of the token tree. Instead, they describe their arguments in
terms of syntactic shapes, and the expander expands the token tree into
HIR based upon that definition.
For example, the `where` command says that it takes a block as its first
required argument, and the description of the block syntactic shape
expands the syntax `cpu > 10` into HIR that represents
`{ $it.cpu > 10 }`.
This commit overhauls that system so that the syntactic shapes are
described in terms of a few new traits (`ExpandSyntax` and
`ExpandExpression` are the primary ones) that are more composable than
the previous system.
The first big win of this new system is the addition of the `ColumnPath`
shape, which looks like `cpu."max ghz"` or `package.version`.
Previously, while a variable path could look like `$it.cpu."max ghz"`,
the tail of a variable path could not be easily reused in other
contexts. Now, that tail is its own syntactic shape, and it can be used
as part of a command's signature.
This cleans up commands like `inc`, `add` and `edit` as well as
shorthand blocks, which can now look like `| where cpu."max ghz" > 10`
2019-09-18 00:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:15:07 +02:00
|
|
|
impl Command {
|
|
|
|
pub fn name(&self) -> &str {
|
2020-04-30 04:23:40 +02:00
|
|
|
self.0.name()
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn signature(&self) -> Signature {
|
2020-04-30 04:23:40 +02:00
|
|
|
self.0.signature()
|
2019-08-02 21:15:07 +02:00
|
|
|
}
|
|
|
|
|
2019-08-30 00:52:32 +02:00
|
|
|
pub fn usage(&self) -> &str {
|
2020-04-30 04:23:40 +02:00
|
|
|
self.0.usage()
|
2019-08-30 00:52:32 +02:00
|
|
|
}
|
|
|
|
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
pub fn run(&self, args: CommandArgs, registry: &CommandRegistry) -> OutputStream {
|
2020-01-17 23:46:18 +01:00
|
|
|
if args.call_info.switch_present("help") {
|
2020-05-04 10:44:33 +02:00
|
|
|
get_help(self.name(), self.usage(), self.signature(), registry).into()
|
2020-01-17 23:46:18 +01:00
|
|
|
} else {
|
2020-04-30 04:23:40 +02:00
|
|
|
match self.0.run(args, registry) {
|
|
|
|
Ok(stream) => stream,
|
|
|
|
Err(err) => OutputStream::one(Err(err)),
|
2020-01-17 23:46:18 +01:00
|
|
|
}
|
2019-06-07 08:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-14 19:02:39 +02:00
|
|
|
|
2019-09-04 03:50:23 +02:00
|
|
|
pub fn is_binary(&self) -> bool {
|
2020-04-30 04:23:40 +02:00
|
|
|
self.0.is_binary()
|
2019-09-04 03:50:23 +02:00
|
|
|
}
|
2019-06-07 08:34:42 +02:00
|
|
|
}
|
|
|
|
|
2019-07-24 00:22:11 +02:00
|
|
|
pub struct FnFilterCommand {
|
2019-05-28 08:45:18 +02:00
|
|
|
name: String,
|
2019-07-24 00:22:11 +02:00
|
|
|
func: fn(EvaluatedFilterCommandArgs) -> Result<OutputStream, ShellError>,
|
2019-05-13 19:30:51 +02:00
|
|
|
}
|
2019-05-22 09:12:03 +02:00
|
|
|
|
2019-08-15 07:02:02 +02:00
|
|
|
impl WholeStreamCommand for FnFilterCommand {
|
2019-07-24 00:22:11 +02:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
2019-08-30 00:52:32 +02:00
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"usage"
|
|
|
|
}
|
|
|
|
|
2019-07-24 00:22:11 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
registry: &CommandRegistry,
|
2019-07-24 00:22:11 +02:00
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
let CommandArgs {
|
|
|
|
host,
|
2019-10-13 06:12:43 +02:00
|
|
|
ctrl_c,
|
2019-08-09 06:51:21 +02:00
|
|
|
shell_manager,
|
2019-07-24 00:22:11 +02:00
|
|
|
call_info,
|
|
|
|
input,
|
|
|
|
} = args;
|
|
|
|
|
2020-01-04 07:44:17 +01:00
|
|
|
let host: Arc<parking_lot::Mutex<dyn Host>> = host.clone();
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
let registry: CommandRegistry = registry.clone();
|
2019-07-24 00:22:11 +02:00
|
|
|
let func = self.func;
|
|
|
|
|
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
|
|
|
let result = input.map(move |it| {
|
2019-07-24 00:22:11 +02:00
|
|
|
let registry = registry.clone();
|
2020-04-15 07:43:23 +02:00
|
|
|
let call_info = match call_info.clone().evaluate_with_new_it(®istry, &it) {
|
2019-07-24 00:22:11 +02:00
|
|
|
Err(err) => return OutputStream::from(vec![Err(err)]).values,
|
|
|
|
Ok(args) => args,
|
|
|
|
};
|
|
|
|
|
2019-10-13 06:12:43 +02:00
|
|
|
let args = EvaluatedFilterCommandArgs::new(
|
|
|
|
host.clone(),
|
|
|
|
ctrl_c.clone(),
|
|
|
|
shell_manager.clone(),
|
|
|
|
call_info,
|
|
|
|
);
|
2019-07-24 00:22:11 +02:00
|
|
|
|
|
|
|
match func(args) {
|
2019-12-06 16:28:26 +01:00
|
|
|
Err(err) => OutputStream::from(vec![Err(err)]).values,
|
2019-07-24 00:22:11 +02:00
|
|
|
Ok(stream) => stream.values,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let result = result.flatten();
|
|
|
|
let result: BoxStream<ReturnValue> = result.boxed();
|
|
|
|
|
|
|
|
Ok(result.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 04:23:40 +02:00
|
|
|
pub fn whole_stream_command(command: impl WholeStreamCommand + 'static) -> Command {
|
|
|
|
Command(Arc::new(command))
|
2019-07-24 00:22:11 +02:00
|
|
|
}
|