mirror of
https://github.com/nushell/nushell
synced 2025-01-07 10:49:03 +00:00
62575c9a4f
Continuation of #8229 and #8326 # Description The `ShellError` enum at the moment is kind of messy. Many variants are basic tuple structs where you always have to reference the implementation with its macro invocation to know which field serves which purpose. Furthermore we have both variants that are kind of redundant or either overly broad to be useful for the user to match on or overly specific with few uses. So I set out to start fixing the lacking documentation and naming to make it feasible to critically review the individual usages and fix those. Furthermore we can decide to join or split up variants that don't seem to be fit for purpose. # Call to action **Everyone:** Feel free to add review comments if you spot inconsistent use of `ShellError` variants. # User-Facing Changes (None now, end goal more explicit and consistent error messages) # Tests + Formatting (No additional tests needed so far) # Commits (so far) - Remove `ShellError::FeatureNotEnabled` - Name fields on `SE::ExternalNotSupported` - Name field on `SE::InvalidProbability` - Name fields on `SE::NushellFailed` variants - Remove unused `SE::NushellFailedSpannedHelp` - Name field on `SE::VariableNotFoundAtRuntime` - Name fields on `SE::EnvVarNotFoundAtRuntime` - Name fields on `SE::ModuleNotFoundAtRuntime` - Remove usused `ModuleOrOverlayNotFoundAtRuntime` - Name fields on `SE::OverlayNotFoundAtRuntime` - Name field on `SE::NotFound`
141 lines
3.1 KiB
Rust
141 lines
3.1 KiB
Rust
use crate::engine::{EngineState, Stack};
|
|
use crate::PipelineData;
|
|
use crate::{
|
|
ast::{Call, Expression},
|
|
engine::Command,
|
|
BlockId, Example, ShellError, Signature,
|
|
};
|
|
use std::path::PathBuf;
|
|
|
|
#[derive(Clone)]
|
|
pub struct Alias {
|
|
pub name: String,
|
|
pub command: Option<Box<dyn Command>>, // None if external call
|
|
pub wrapped_call: Expression,
|
|
}
|
|
|
|
impl Command for Alias {
|
|
fn name(&self) -> &str {
|
|
&self.name
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
if let Some(cmd) = &self.command {
|
|
cmd.signature()
|
|
} else {
|
|
Signature::new(&self.name).allows_unknown_args()
|
|
}
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
if let Some(cmd) = &self.command {
|
|
cmd.usage()
|
|
} else {
|
|
"This alias wraps an unknown external command."
|
|
}
|
|
}
|
|
|
|
fn extra_usage(&self) -> &str {
|
|
if let Some(cmd) = &self.command {
|
|
cmd.extra_usage()
|
|
} else {
|
|
""
|
|
}
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
_engine_state: &EngineState,
|
|
_stack: &mut Stack,
|
|
call: &Call,
|
|
_input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
Err(ShellError::NushellFailedSpanned {
|
|
msg: "Can't run alias directly. Unwrap it first".to_string(),
|
|
label: "originates from here".to_string(),
|
|
span: call.head,
|
|
})
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
if let Some(cmd) = &self.command {
|
|
cmd.examples()
|
|
} else {
|
|
vec![]
|
|
}
|
|
}
|
|
|
|
fn is_builtin(&self) -> bool {
|
|
if let Some(cmd) = &self.command {
|
|
cmd.is_builtin()
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
fn is_known_external(&self) -> bool {
|
|
if let Some(cmd) = &self.command {
|
|
cmd.is_known_external()
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
fn is_alias(&self) -> bool {
|
|
true
|
|
}
|
|
|
|
fn as_alias(&self) -> Option<&Alias> {
|
|
Some(self)
|
|
}
|
|
|
|
fn is_custom_command(&self) -> bool {
|
|
if let Some(cmd) = &self.command {
|
|
cmd.is_custom_command()
|
|
} else if self.get_block_id().is_some() {
|
|
true
|
|
} else {
|
|
self.is_known_external()
|
|
}
|
|
}
|
|
|
|
fn is_sub(&self) -> bool {
|
|
if let Some(cmd) = &self.command {
|
|
cmd.is_sub()
|
|
} else {
|
|
self.name().contains(' ')
|
|
}
|
|
}
|
|
|
|
fn is_parser_keyword(&self) -> bool {
|
|
if let Some(cmd) = &self.command {
|
|
cmd.is_parser_keyword()
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
fn is_plugin(&self) -> Option<(&PathBuf, &Option<PathBuf>)> {
|
|
if let Some(cmd) = &self.command {
|
|
cmd.is_plugin()
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
fn get_block_id(&self) -> Option<BlockId> {
|
|
if let Some(cmd) = &self.command {
|
|
cmd.get_block_id()
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
if let Some(cmd) = &self.command {
|
|
cmd.search_terms()
|
|
} else {
|
|
vec![]
|
|
}
|
|
}
|
|
}
|