nu_cmd_lang/core_commands/
const_.rs1use nu_engine::command_prelude::*;
2use nu_protocol::engine::CommandType;
3
4#[derive(Clone)]
5pub struct Const;
6
7impl Command for Const {
8 fn name(&self) -> &str {
9 "const"
10 }
11
12 fn description(&self) -> &str {
13 "Create a parse-time constant."
14 }
15
16 fn signature(&self) -> nu_protocol::Signature {
17 Signature::build("const")
18 .input_output_types(vec![(Type::Nothing, Type::Nothing)])
19 .allow_variants_without_examples(true)
20 .required(
21 "const_name",
22 SyntaxShape::VarWithOptType,
23 "The constant name to create.",
24 )
25 .required(
26 "initial_value",
27 SyntaxShape::Keyword(b"=".to_vec(), Box::new(SyntaxShape::MathExpression)),
28 "Equals sign followed by constant value.",
29 )
30 .category(Category::Core)
31 }
32
33 fn extra_description(&self) -> &str {
34 r#"This command is a parser keyword. For details, check:
35 https://www.nushell.sh/book/thinking_in_nu.html"#
36 }
37
38 fn command_type(&self) -> CommandType {
39 CommandType::Keyword
40 }
41
42 fn search_terms(&self) -> Vec<&str> {
43 vec!["set", "let"]
44 }
45
46 fn run(
47 &self,
48 _engine_state: &EngineState,
49 _stack: &mut Stack,
50 _call: &Call,
51 _input: PipelineData,
52 ) -> Result<PipelineData, ShellError> {
53 eprintln!(
56 "Tried to execute 'run' for the 'const' command: this code path should never be reached in IR mode"
57 );
58 unreachable!()
59 }
60
61 fn run_const(
62 &self,
63 _working_set: &StateWorkingSet,
64 _call: &Call,
65 _input: PipelineData,
66 ) -> Result<PipelineData, ShellError> {
67 Ok(PipelineData::empty())
68 }
69
70 fn is_const(&self) -> bool {
71 true
72 }
73
74 fn examples(&self) -> Vec<Example<'_>> {
75 vec![
76 Example {
77 description: "Create a new parse-time constant.",
78 example: "const x = 10",
79 result: None,
80 },
81 Example {
82 description: "Create a composite constant value.",
83 example: "const x = { a: 10, b: 20 }",
84 result: None,
85 },
86 ]
87 }
88}
89
90#[cfg(test)]
91mod test {
92 use nu_protocol::engine::CommandType;
93
94 use super::*;
95
96 #[test]
97 fn test_examples() {
98 use crate::test_examples;
99
100 test_examples(Const {})
101 }
102
103 #[test]
104 fn test_command_type() {
105 assert!(matches!(Const.command_type(), CommandType::Keyword));
106 }
107}