nu_protocol/
alias.rs

1use crate::{
2    PipelineData, ShellError, Signature,
3    ast::Expression,
4    engine::{Call, Command, CommandType, EngineState, Stack},
5};
6
7/// Command wrapper of an alias.
8///
9/// Our current aliases are implemented as wrapping commands
10/// This has some limitations compared to text-substitution macro aliases but can reliably use more
11/// of our machinery
12#[derive(Clone)]
13pub struct Alias {
14    pub name: String,
15    /// Wrapped inner [`Command`]. `None` if alias of external call
16    pub command: Option<Box<dyn Command>>,
17    pub wrapped_call: Expression,
18    pub description: String,
19    pub extra_description: String,
20}
21
22impl Command for Alias {
23    fn name(&self) -> &str {
24        &self.name
25    }
26
27    fn signature(&self) -> Signature {
28        if let Some(cmd) = &self.command {
29            cmd.signature()
30        } else {
31            Signature::new(&self.name).allows_unknown_args()
32        }
33    }
34
35    fn description(&self) -> &str {
36        &self.description
37    }
38
39    fn extra_description(&self) -> &str {
40        &self.extra_description
41    }
42
43    fn run(
44        &self,
45        _engine_state: &EngineState,
46        _stack: &mut Stack,
47        call: &Call,
48        _input: PipelineData,
49    ) -> Result<PipelineData, ShellError> {
50        Err(ShellError::NushellFailedSpanned {
51            msg: "Can't run alias directly. Unwrap it first".to_string(),
52            label: "originates from here".to_string(),
53            span: call.head,
54        })
55    }
56
57    fn command_type(&self) -> CommandType {
58        CommandType::Alias
59    }
60
61    fn as_alias(&self) -> Option<&Alias> {
62        Some(self)
63    }
64}