1use crate::globals::print_to_console;
2use nu_engine::CallExt;
3use nu_protocol::engine::Call;
4use nu_protocol::{
5 Category, PipelineData, ShellError, Signature,
6 engine::{Command, EngineState, Stack},
7};
8use nu_protocol::{SyntaxShape, Type};
9
10#[derive(Clone)]
11pub struct Eval;
12
13impl Command for Eval {
14 fn name(&self) -> &str {
15 "eval"
16 }
17
18 fn signature(&self) -> Signature {
19 Signature::build(self.name())
20 .optional("code", SyntaxShape::String, "code to evaluate")
21 .input_output_type(Type::one_of([Type::Nothing, Type::String]), Type::Nothing)
22 .category(Category::FileSystem)
23 }
24
25 fn description(&self) -> &str {
26 "evaluates a string as nushell code."
27 }
28
29 fn run(
30 &self,
31 engine_state: &EngineState,
32 stack: &mut Stack,
33 call: &Call,
34 input: PipelineData,
35 ) -> Result<PipelineData, ShellError> {
36 let code: Option<String> = call.opt(engine_state, stack, 0)?;
37
38 let (span, code) = match code {
39 Some(c) => (Some(call.arguments_span()), c),
40 None => (
41 input.span(),
42 input.collect_string("\n", &engine_state.config)?,
43 ),
44 };
45
46 match super::source_file::eval(engine_state, stack, &code, None) {
47 Ok(d) => Ok(d),
48 Err(err) => {
49 let msg: String = err.into();
50 print_to_console(&msg, true);
51 Err(ShellError::GenericError {
52 error: "source error".into(),
53 msg: "can't source string".into(),
54 span,
55 help: None,
56 inner: vec![],
57 })
58 }
59 }
60 }
61}