nu_cmd_lang/core_commands/
export_use.rs1use nu_engine::{
2 command_prelude::*, find_in_dirs_env, get_dirs_var_from_call, get_eval_block, redirect_env,
3};
4use nu_protocol::{
5 ast::{Expr, Expression},
6 engine::CommandType,
7 shell_error::generic::GenericError,
8};
9
10#[derive(Clone)]
11pub struct ExportUse;
12
13impl Command for ExportUse {
14 fn name(&self) -> &str {
15 "export use"
16 }
17
18 fn description(&self) -> &str {
19 "Use definitions from a module and export them from this module."
20 }
21
22 fn signature(&self) -> nu_protocol::Signature {
23 Signature::build("export use")
24 .input_output_types(vec![(Type::Nothing, Type::Nothing)])
25 .param(Parameter::Required(
26 PositionalArg::new("module", SyntaxShape::String)
27 .desc("Module or module file.")
28 .completion(Completion::Builtin(BuiltinCompletion::NuFile {
29 std_virtual_path: true,
30 })),
31 ))
32 .param(Parameter::Rest(
33 PositionalArg::new("members", SyntaxShape::Any)
34 .desc("Which members of the module to import.")
35 .completion(Completion::Builtin(BuiltinCompletion::ModuleExports)),
36 ))
37 .category(Category::Core)
38 }
39
40 fn extra_description(&self) -> &str {
41 "This command is a parser keyword. For details, check:
42 https://www.nushell.sh/book/thinking_in_nu.html"
43 }
44
45 fn command_type(&self) -> CommandType {
46 CommandType::Keyword
47 }
48
49 fn run(
50 &self,
51 engine_state: &EngineState,
52 caller_stack: &mut Stack,
53 call: &Call,
54 input: PipelineData,
55 ) -> Result<PipelineData, ShellError> {
56 if call.get_parser_info(caller_stack, "noop").is_some() {
57 return Ok(PipelineData::empty());
58 }
59 let Some(Expression {
60 expr: Expr::ImportPattern(import_pattern),
61 ..
62 }) = call.get_parser_info(caller_stack, "import_pattern")
63 else {
64 return Err(ShellError::Generic(GenericError::new(
65 "Unexpected import",
66 "import pattern not supported",
67 call.head,
68 )));
69 };
70
71 let import_pattern = import_pattern.clone();
73
74 if let Some(module_id) = import_pattern.head.id {
75 for var_id in &import_pattern.constants {
77 let var = engine_state.get_var(*var_id);
78
79 if let Some(constval) = &var.const_val {
80 caller_stack.add_var(*var_id, constval.clone());
81 } else {
82 return Err(ShellError::NushellFailedSpanned {
83 msg: "Missing Constant".to_string(),
84 label: "constant not added by the parser".to_string(),
85 span: var.declaration_span,
86 });
87 }
88 }
89
90 let module = engine_state.get_module(module_id);
92
93 if let Some(block_id) = module.env_block {
94 let block = engine_state.get_block(block_id);
95
96 let module_arg_str = String::from_utf8_lossy(
98 engine_state.get_span_contents(import_pattern.head.span),
99 );
100
101 let maybe_file_path_or_dir = find_in_dirs_env(
102 &module_arg_str,
103 engine_state,
104 caller_stack,
105 get_dirs_var_from_call(caller_stack, call),
106 )?;
107 let maybe_parent = maybe_file_path_or_dir.as_ref().and_then(|path| {
110 if path.is_dir() {
111 Some(path.to_path_buf())
112 } else {
113 path.parent().map(|p| p.to_path_buf())
114 }
115 });
116
117 let mut callee_stack = caller_stack
118 .gather_captures(engine_state, &block.captures)
119 .reset_pipes();
120
121 if let Some(parent) = maybe_parent {
123 let file_pwd = Value::string(parent.to_string_lossy(), call.head);
124 callee_stack.add_env_var("FILE_PWD".to_string(), file_pwd);
125 }
126
127 if let Some(path) = maybe_file_path_or_dir {
128 let module_file_path = if path.is_dir() {
129 Value::string(path.join("mod.nu").to_string_lossy(), call.head)
132 } else {
133 Value::string(path.to_string_lossy(), call.head)
134 };
135 callee_stack.add_env_var("CURRENT_FILE".to_string(), module_file_path);
136 }
137
138 let eval_block = get_eval_block(engine_state);
139
140 let _ = eval_block(engine_state, &mut callee_stack, block, input)?;
142
143 redirect_env(engine_state, caller_stack, &callee_stack);
145 }
146 } else {
147 return Err(ShellError::Generic(GenericError::new(
148 format!(
149 "Could not import from '{}'",
150 String::from_utf8_lossy(&import_pattern.head.name)
151 ),
152 "module does not exist",
153 import_pattern.head.span,
154 )));
155 }
156
157 Ok(PipelineData::empty())
158 }
159
160 fn examples(&self) -> Vec<Example<'_>> {
161 vec![Example {
162 description: "Re-export a command from another module.",
163 example: r#"module spam { export def foo [] { "foo" } }
164 module eggs { export use spam foo }
165 use eggs foo
166 foo
167 "#,
168 result: Some(Value::test_string("foo")),
169 }]
170 }
171
172 fn search_terms(&self) -> Vec<&str> {
173 vec!["reexport", "import", "module"]
174 }
175}