nu_engine/
eval_helpers.rs

1use crate::{
2    eval_block, eval_block_with_early_return, eval_expression, eval_expression_with_input,
3    eval_ir_block, eval_subexpression,
4};
5use nu_protocol::{
6    ast::{Block, Expression},
7    debugger::{WithDebug, WithoutDebug},
8    engine::{EngineState, Stack},
9    PipelineData, ShellError, Value,
10};
11
12/// Type of eval_block() function
13pub type EvalBlockFn =
14    fn(&EngineState, &mut Stack, &Block, PipelineData) -> Result<PipelineData, ShellError>;
15
16/// Type of eval_ir_block() function
17pub type EvalIrBlockFn =
18    fn(&EngineState, &mut Stack, &Block, PipelineData) -> Result<PipelineData, ShellError>;
19
20/// Type of eval_block_with_early_return() function
21pub type EvalBlockWithEarlyReturnFn =
22    fn(&EngineState, &mut Stack, &Block, PipelineData) -> Result<PipelineData, ShellError>;
23
24/// Type of eval_expression() function
25pub type EvalExpressionFn = fn(&EngineState, &mut Stack, &Expression) -> Result<Value, ShellError>;
26
27/// Type of eval_expression_with_input() function
28pub type EvalExpressionWithInputFn =
29    fn(&EngineState, &mut Stack, &Expression, PipelineData) -> Result<PipelineData, ShellError>;
30
31/// Type of eval_subexpression() function
32pub type EvalSubexpressionFn =
33    fn(&EngineState, &mut Stack, &Block, PipelineData) -> Result<PipelineData, ShellError>;
34
35/// Helper function to fetch `eval_block()` with the correct type parameter based on whether
36/// engine_state is configured with or without a debugger.
37pub fn get_eval_block(engine_state: &EngineState) -> EvalBlockFn {
38    if engine_state.is_debugging() {
39        eval_block::<WithDebug>
40    } else {
41        eval_block::<WithoutDebug>
42    }
43}
44
45/// Helper function to fetch `eval_ir_block()` with the correct type parameter based on whether
46/// engine_state is configured with or without a debugger.
47pub fn get_eval_ir_block(engine_state: &EngineState) -> EvalIrBlockFn {
48    if engine_state.is_debugging() {
49        eval_ir_block::<WithDebug>
50    } else {
51        eval_ir_block::<WithoutDebug>
52    }
53}
54
55/// Helper function to fetch `eval_block_with_early_return()` with the correct type parameter based
56/// on whether engine_state is configured with or without a debugger.
57pub fn get_eval_block_with_early_return(engine_state: &EngineState) -> EvalBlockWithEarlyReturnFn {
58    if engine_state.is_debugging() {
59        eval_block_with_early_return::<WithDebug>
60    } else {
61        eval_block_with_early_return::<WithoutDebug>
62    }
63}
64
65/// Helper function to fetch `eval_expression()` with the correct type parameter based on whether
66/// engine_state is configured with or without a debugger.
67pub fn get_eval_expression(engine_state: &EngineState) -> EvalExpressionFn {
68    if engine_state.is_debugging() {
69        eval_expression::<WithDebug>
70    } else {
71        eval_expression::<WithoutDebug>
72    }
73}
74
75/// Helper function to fetch `eval_expression_with_input()` with the correct type parameter based
76/// on whether engine_state is configured with or without a debugger.
77pub fn get_eval_expression_with_input(engine_state: &EngineState) -> EvalExpressionWithInputFn {
78    if engine_state.is_debugging() {
79        eval_expression_with_input::<WithDebug>
80    } else {
81        eval_expression_with_input::<WithoutDebug>
82    }
83}
84
85/// Helper function to fetch `eval_subexpression()` with the correct type parameter based on whether
86/// engine_state is configured with or without a debugger.
87pub fn get_eval_subexpression(engine_state: &EngineState) -> EvalSubexpressionFn {
88    if engine_state.is_debugging() {
89        eval_subexpression::<WithDebug>
90    } else {
91        eval_subexpression::<WithoutDebug>
92    }
93}