nushell/crates/nu-engine/src/eval_helpers.rs
Ian Manske c747ec75c9
Add command_prelude module (#12291)
# Description
When implementing a `Command`, one must also import all the types
present in the function signatures for `Command`. This makes it so that
we often import the same set of types in each command implementation
file. E.g., something like this:
```rust
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
    record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData,
    ShellError, Signature, Span, Type, Value,
};
```

This PR adds the `nu_engine::command_prelude` module which contains the
necessary and commonly used types to implement a `Command`:
```rust
// command_prelude.rs
pub use crate::CallExt;
pub use nu_protocol::{
    ast::{Call, CellPath},
    engine::{Command, EngineState, Stack},
    record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, IntoSpanned,
    PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};
```

This should reduce the boilerplate needed to implement a command and
also gives us a place to track the breadth of the `Command` API. I tried
to be conservative with what went into the prelude modules, since it
might be hard/annoying to remove items from the prelude in the future.
Let me know if something should be included or excluded.
2024-03-26 21:17:30 +00:00

84 lines
3.0 KiB
Rust

use crate::{
eval_block, eval_block_with_early_return, eval_expression, eval_expression_with_input,
eval_subexpression,
};
use nu_protocol::{
ast::{Block, Expression},
debugger::{WithDebug, WithoutDebug},
engine::{EngineState, Stack},
PipelineData, ShellError, Value,
};
/// Type of eval_block() function
pub type EvalBlockFn =
fn(&EngineState, &mut Stack, &Block, PipelineData) -> Result<PipelineData, ShellError>;
/// Type of eval_block_with_early_return() function
pub type EvalBlockWithEarlyReturnFn =
fn(&EngineState, &mut Stack, &Block, PipelineData) -> Result<PipelineData, ShellError>;
/// Type of eval_expression() function
pub type EvalExpressionFn = fn(&EngineState, &mut Stack, &Expression) -> Result<Value, ShellError>;
/// Type of eval_expression_with_input() function
pub type EvalExpressionWithInputFn = fn(
&EngineState,
&mut Stack,
&Expression,
PipelineData,
) -> Result<(PipelineData, bool), ShellError>;
/// Type of eval_subexpression() function
pub type EvalSubexpressionFn =
fn(&EngineState, &mut Stack, &Block, PipelineData) -> Result<PipelineData, ShellError>;
/// Helper function to fetch `eval_block()` with the correct type parameter based on whether
/// engine_state is configured with or without a debugger.
pub fn get_eval_block(engine_state: &EngineState) -> EvalBlockFn {
if engine_state.is_debugging() {
eval_block::<WithDebug>
} else {
eval_block::<WithoutDebug>
}
}
/// Helper function to fetch `eval_block_with_early_return()` with the correct type parameter based
/// on whether engine_state is configured with or without a debugger.
pub fn get_eval_block_with_early_return(engine_state: &EngineState) -> EvalBlockWithEarlyReturnFn {
if engine_state.is_debugging() {
eval_block_with_early_return::<WithDebug>
} else {
eval_block_with_early_return::<WithoutDebug>
}
}
/// Helper function to fetch `eval_expression()` with the correct type parameter based on whether
/// engine_state is configured with or without a debugger.
pub fn get_eval_expression(engine_state: &EngineState) -> EvalExpressionFn {
if engine_state.is_debugging() {
eval_expression::<WithDebug>
} else {
eval_expression::<WithoutDebug>
}
}
/// Helper function to fetch `eval_expression_with_input()` with the correct type parameter based
/// on whether engine_state is configured with or without a debugger.
pub fn get_eval_expression_with_input(engine_state: &EngineState) -> EvalExpressionWithInputFn {
if engine_state.is_debugging() {
eval_expression_with_input::<WithDebug>
} else {
eval_expression_with_input::<WithoutDebug>
}
}
/// Helper function to fetch `eval_subexpression()` with the correct type parameter based on whether
/// engine_state is configured with or without a debugger.
pub fn get_eval_subexpression(engine_state: &EngineState) -> EvalSubexpressionFn {
if engine_state.is_debugging() {
eval_subexpression::<WithDebug>
} else {
eval_subexpression::<WithoutDebug>
}
}