-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error handling and hints and stuff
- Loading branch information
Showing
10 changed files
with
199 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use std::borrow::Cow; | ||
|
||
use logos::Span; | ||
|
||
use crate::{ | ||
builtin_parser::Spanned, | ||
command::{CommandHint, CommandHintColor}, | ||
}; | ||
|
||
use super::Value; | ||
|
||
/// An error occuring during the while executing the [`AST`](Ast) of the command. | ||
#[derive(Debug)] | ||
pub enum RunError { | ||
/// A custom text message. Contains very little contextual information, try to find an existing error instead. | ||
Custom { | ||
/// The text of the message | ||
text: String, | ||
span: Span, | ||
}, | ||
VariableNotFound(Span), | ||
ExpectedNumberAfterUnaryOperator(Spanned<Value>), | ||
InvalidVariantForResource(String, String), | ||
CannotIndexValue(Span), | ||
FieldNotFoundInStruct(Span), | ||
CouldntDereferenceValue(Span), | ||
ReferenceToMovedData(Span), | ||
VariableMoved(Span), | ||
CannotBorrowValue(Span), | ||
IncompatibleReflectTypes { | ||
expected: String, | ||
actual: String, | ||
span: Span, | ||
}, | ||
EnumVariantNotFound { | ||
name: String, | ||
span: Span, | ||
}, | ||
CannotMoveOutOfResource(Spanned<String>), | ||
} | ||
|
||
impl RunError { | ||
pub fn spans(&self) -> Vec<Span> { | ||
use RunError::*; | ||
|
||
match self { | ||
Custom { span, .. } => vec![span.clone()], | ||
VariableNotFound(span) => vec![span.clone()], | ||
ExpectedNumberAfterUnaryOperator(Spanned { span, .. }) => vec![span.clone()], | ||
InvalidVariantForResource(_, _) => todo!(), | ||
CannotIndexValue(span) => vec![span.clone()], | ||
FieldNotFoundInStruct(span) => vec![span.clone()], | ||
CouldntDereferenceValue(span) => vec![span.clone()], | ||
ReferenceToMovedData(span) => vec![span.clone()], | ||
VariableMoved(span) => vec![span.clone()], | ||
CannotBorrowValue(span) => vec![span.clone()], | ||
IncompatibleReflectTypes { span, .. } => vec![span.clone()], | ||
EnumVariantNotFound { span, .. } => vec![span.clone()], | ||
CannotMoveOutOfResource(Spanned { span, .. }) => vec![span.clone()], | ||
} | ||
} | ||
pub fn hints(&self) -> Vec<CommandHint> { | ||
self.spans() | ||
.into_iter() | ||
.map(|span| CommandHint::new(span, CommandHintColor::Error, "todo")) | ||
.collect() | ||
} | ||
pub fn message(&self) -> Cow<'static, str> { | ||
use RunError::*; | ||
|
||
match self { | ||
Custom { text, .. } => text.clone().into(), | ||
VariableNotFound(_) => "Variable not found.".into(), | ||
ExpectedNumberAfterUnaryOperator(Spanned { value, .. }) => format!( | ||
"Expected a number after unary operator (-) but got {} instead.", | ||
value.kind() | ||
) | ||
.into(), | ||
InvalidVariantForResource(_, _) => todo!(), | ||
CannotIndexValue(_) => todo!(), | ||
FieldNotFoundInStruct(_) => todo!(), | ||
CouldntDereferenceValue(_) => todo!(), | ||
ReferenceToMovedData(_) => todo!(), | ||
VariableMoved(_) => todo!(), | ||
CannotBorrowValue(_) => todo!(), | ||
IncompatibleReflectTypes { | ||
expected, | ||
actual, | ||
span, | ||
} => todo!(), | ||
EnumVariantNotFound { name, span } => todo!(), | ||
CannotMoveOutOfResource(Spanned { value, .. }) => format!("Cannot move out of resource `{value}`, try borrowing it instead.").into(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.