nu_protocol/engine/
command.rs1use serde::{Deserialize, Serialize};
2
3use super::{EngineState, Stack, StateWorkingSet};
4use crate::{
5 Alias, BlockId, DeprecationEntry, DynamicCompletionCallRef, DynamicSuggestion, Example,
6 OutDest, PipelineData, ShellError, Signature, Span, Value, engine::Call,
7};
8use std::{
9 any::Any,
10 borrow::Cow,
11 fmt::{Debug, Display},
12};
13
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum ArgType<'a> {
16 Flag(Cow<'a, str>),
17 Positional(usize),
18}
19
20impl<'a> Display for ArgType<'a> {
21 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22 match self {
23 ArgType::Flag(flag_name) => match flag_name {
24 Cow::Borrowed(v) => write!(f, "{v}"),
25 Cow::Owned(v) => write!(f, "{v}"),
26 },
27 ArgType::Positional(idx) => write!(f, "{idx}"),
28 }
29 }
30}
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
33pub enum CommandType {
34 Builtin,
35 Custom,
36 Keyword,
37 External,
38 Alias,
39 Plugin,
40}
41
42impl Display for CommandType {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 let str = match self {
45 CommandType::Builtin => "built-in",
46 CommandType::Custom => "custom",
47 CommandType::Keyword => "keyword",
48 CommandType::External => "external",
49 CommandType::Alias => "alias",
50 CommandType::Plugin => "plugin",
51 };
52 write!(f, "{str}")
53 }
54}
55
56pub trait Command: Send + Sync + CommandClone + Any {
57 fn name(&self) -> &str;
58
59 fn signature(&self) -> Signature;
60
61 fn description(&self) -> &str;
65
66 fn extra_description(&self) -> &str {
70 ""
71 }
72
73 fn run(
74 &self,
75 engine_state: &EngineState,
76 stack: &mut Stack,
77 call: &Call,
78 input: PipelineData,
79 ) -> Result<PipelineData, ShellError>;
80
81 #[allow(unused_variables)]
85 fn run_const(
86 &self,
87 working_set: &StateWorkingSet,
88 stack: &mut Stack,
89 call: &Call,
90 input: PipelineData,
91 ) -> Result<PipelineData, ShellError> {
92 Err(ShellError::MissingConstEvalImpl { span: call.head })
93 }
94
95 fn examples(&self) -> Vec<Example<'_>> {
96 Vec::new()
97 }
98
99 fn search_terms(&self) -> Vec<&str> {
101 vec![]
102 }
103
104 fn attributes(&self) -> Vec<(String, Value)> {
105 vec![]
106 }
107
108 fn is_const(&self) -> bool {
110 false
111 }
112
113 fn is_sub(&self) -> bool {
115 self.name().contains(' ')
116 }
117
118 fn block_id(&self) -> Option<BlockId> {
120 None
121 }
122
123 fn as_alias(&self) -> Option<&Alias> {
125 None
126 }
127
128 #[cfg(feature = "plugin")]
130 fn plugin_identity(&self) -> Option<&crate::PluginIdentity> {
131 None
132 }
133
134 fn command_type(&self) -> CommandType {
135 CommandType::Builtin
136 }
137
138 fn is_builtin(&self) -> bool {
139 self.command_type() == CommandType::Builtin
140 }
141
142 fn is_custom(&self) -> bool {
143 self.command_type() == CommandType::Custom
144 }
145
146 fn is_keyword(&self) -> bool {
147 self.command_type() == CommandType::Keyword
148 }
149
150 fn is_known_external(&self) -> bool {
151 self.command_type() == CommandType::External
152 }
153
154 fn decl_span(&self) -> Option<Span> {
158 None
159 }
160
161 fn is_alias(&self) -> bool {
162 self.command_type() == CommandType::Alias
163 }
164
165 fn is_plugin(&self) -> bool {
166 self.command_type() == CommandType::Plugin
167 }
168
169 fn deprecation_info(&self) -> Vec<DeprecationEntry> {
170 vec![]
171 }
172
173 fn pipe_redirection(&self) -> (Option<OutDest>, Option<OutDest>) {
174 (None, None)
175 }
176
177 #[allow(unused_variables)]
188 #[expect(deprecated)]
189 fn get_dynamic_completion(
190 &self,
191 engine_state: &EngineState,
192 stack: &mut Stack,
193 call: DynamicCompletionCallRef,
194 arg_type: &ArgType,
195 _experimental: ExperimentalMarker,
196 ) -> Result<Option<Vec<DynamicSuggestion>>, ShellError> {
197 Ok(None)
198 }
199
200 fn requires_ast_for_arguments(&self) -> bool {
203 false
204 }
205}
206
207pub trait CommandClone {
208 fn clone_box(&self) -> Box<dyn Command>;
209}
210
211impl<T> CommandClone for T
212where
213 T: 'static + Command + Clone,
214{
215 fn clone_box(&self) -> Box<dyn Command> {
216 Box::new(self.clone())
217 }
218}
219
220impl Clone for Box<dyn Command> {
221 fn clone(&self) -> Box<dyn Command> {
222 self.clone_box()
223 }
224}
225
226#[derive(Debug, Clone, Copy, PartialEq, Eq)]
231#[deprecated(note = "this method is very experimental, likely to change")]
232pub struct ExperimentalMarker;