Yet more ununwraps (#1150)

This commit is contained in:
Jonathan Turner
2020-01-02 20:07:17 +13:00
committed by GitHub
parent 5e31851070
commit 3e3cb15f3d
7 changed files with 135 additions and 76 deletions

View File

@ -103,11 +103,11 @@ impl TestRegistry {
}
impl SignatureRegistry for TestRegistry {
fn has(&self, name: &str) -> bool {
self.signatures.contains_key(name)
fn has(&self, name: &str) -> Result<bool, ShellError> {
Ok(self.signatures.contains_key(name))
}
fn get(&self, name: &str) -> Option<Signature> {
self.signatures.get(name).cloned()
fn get(&self, name: &str) -> Result<Option<Signature>, ShellError> {
Ok(self.signatures.get(name).cloned())
}
}

View File

@ -127,8 +127,8 @@ impl ExpandExpression for SyntaxShape {
}
pub trait SignatureRegistry {
fn has(&self, name: &str) -> bool;
fn get(&self, name: &str) -> Option<Signature>;
fn has(&self, name: &str) -> Result<bool, ShellError>;
fn get(&self, name: &str) -> Result<Option<Signature>, ShellError>;
}
#[derive(Getters, new)]
@ -673,16 +673,26 @@ impl FallibleColorSyntax for CommandHeadShape {
UnspannedAtomicToken::Word { text } => {
let name = text.slice(context.source);
if context.registry.has(name) {
if context.registry.has(name)? {
// If the registry has the command, color it as an internal command
token_nodes.color_shape(FlatShape::InternalCommand.spanned(text));
let signature = context.registry.get(name).ok_or_else(|| {
ShellError::labeled_error(
"Internal error: could not load signature from registry",
"could not load from registry",
text,
)
})?;
let signature = context
.registry
.get(name)
.map_err(|_| {
ShellError::labeled_error(
"Internal error: could not load signature from registry",
"could not load from registry",
text,
)
})?
.ok_or_else(|| {
ShellError::labeled_error(
"Internal error: could not load signature from registry",
"could not load from registry",
text,
)
})?;
Ok(CommandHeadKind::Internal(signature))
} else {
// Otherwise, color it as an external command
@ -721,10 +731,14 @@ impl ExpandSyntax for CommandHeadShape {
},
UnspannedToken::Bare => {
let name = token_span.slice(context.source);
if context.registry.has(name) {
let signature = context.registry.get(name).ok_or_else(|| {
ParseError::internal_error(name.spanned(token_span))
})?;
if context.registry.has(name)? {
let signature = context
.registry
.get(name)
.map_err(|_| ParseError::internal_error(name.spanned(token_span)))?
.ok_or_else(|| {
ParseError::internal_error(name.spanned(token_span))
})?;
CommandSignature::Internal(signature.spanned(token_span))
} else {
CommandSignature::External(token_span)