nushell/crates/nu-parser/src/known_external.rs
Stefan Holderbach 62575c9a4f
Document and critically review ShellError variants - Ep. 3 (#8340)
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`
2023-03-06 18:33:09 +01:00

136 lines
4.0 KiB
Rust

use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::{
ast::{Argument, Call, Expr, Expression},
engine::Command,
ShellError, Signature,
};
use nu_protocol::{PipelineData, Spanned, Type};
#[derive(Clone)]
pub struct KnownExternal {
pub name: String,
pub signature: Box<Signature>,
pub usage: String,
}
impl Command for KnownExternal {
fn name(&self) -> &str {
&self.name
}
fn signature(&self) -> Signature {
*self.signature.clone()
}
fn usage(&self) -> &str {
&self.usage
}
fn is_known_external(&self) -> bool {
true
}
fn is_builtin(&self) -> bool {
false
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let call_span = call.span();
let head_span = call.head;
let decl_id = engine_state
.find_decl("run-external".as_bytes(), &[])
.ok_or(ShellError::ExternalNotSupported { span: head_span })?;
let command = engine_state.get_decl(decl_id);
let mut extern_call = Call::new(head_span);
let extern_name = if let Some(name_bytes) = engine_state.find_decl_name(call.decl_id, &[]) {
String::from_utf8_lossy(name_bytes)
} else {
return Err(ShellError::NushellFailedSpanned {
msg: "known external name not found".to_string(),
label: "could not find name for this command".to_string(),
span: call.head,
});
};
let extern_name: Vec<_> = extern_name.split(' ').collect();
let arg_extern_name = Expression {
expr: Expr::String(extern_name[0].to_string()),
span: call.head,
ty: Type::String,
custom_completion: None,
};
extern_call.add_positional(arg_extern_name);
for subcommand in extern_name.into_iter().skip(1) {
extern_call.add_positional(Expression {
expr: Expr::String(subcommand.to_string()),
span: call.head,
ty: Type::String,
custom_completion: None,
});
}
for arg in &call.arguments {
match arg {
Argument::Positional(positional) => extern_call.add_positional(positional.clone()),
Argument::Named(named) => {
if let Some(short) = &named.1 {
extern_call.add_positional(Expression {
expr: Expr::String(format!("-{}", short.item)),
span: named.0.span,
ty: Type::String,
custom_completion: None,
});
} else {
extern_call.add_positional(Expression {
expr: Expr::String(format!("--{}", named.0.item)),
span: named.0.span,
ty: Type::String,
custom_completion: None,
});
}
if let Some(arg) = &named.2 {
extern_call.add_positional(arg.clone());
}
}
Argument::Unknown(unknown) => extern_call.add_unknown(unknown.clone()),
}
}
if call.redirect_stdout {
extern_call.add_named((
Spanned {
item: "redirect-stdout".into(),
span: call_span,
},
None,
None,
))
}
if call.redirect_stderr {
extern_call.add_named((
Spanned {
item: "redirect-stderr".into(),
span: call_span,
},
None,
None,
))
}
command.run(engine_state, stack, &extern_call, input)
}
}