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