Skip to content

Instantly share code, notes, and snippets.

@azriel91
Created September 19, 2020 08:40
Show Gist options
  • Save azriel91/951c02fe6d5c21a97f6bc7bc8b838058 to your computer and use it in GitHub Desktop.
Save azriel91/951c02fe6d5c21a97f6bc7bc8b838058 to your computer and use it in GitHub Desktop.
Source Error
/// Information pertaining to an error from a source file.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SourceError<'path, 'source> {
/// Path to the file that contains the source.
pub path: &'path Path,
/// The expression that this error is for.
pub expr: Expr<'source>,
/// The suggestion or hint to provide to the user.
pub suggestion: Option<Suggestion<'source>>,
/// Whether this is a denied error or warning.
pub severity: Severity,
}
/// Line number and full line containing an expression.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ExprContext {
/// Start (inclusive) and end (exclusive) positions of the line in the source file.
pub span_source: Span,
/// Line number of the line within the source.
pub line_number: usize,
/// Full line containing the expression.
pub line: &'file str,
}
/// A token or value in the source file.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Expr<'source> {
/// Line number and full line containing the expression.
pub context: ExprContext<'source>,
/// Start (inclusive) and end (exclusive) positions of the value in the source file.
pub span_source: Span,
/// Value of the expression.
pub value: &'source str,
}
/// Start (inclusive) and end (exclusive) positions of a `&str`.
///
/// This type is used instead of [`std::ops::Range`] because `Range` is `!Copy`.
///
/// See:
///
/// * <https://github.com/rust-lang/rust/issues/48649>
/// * <https://github.com/rust-lang/rust/pull/27186#issuecomment-123390413>
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Span {
/// Exclusive start position of the `&str`.
pub start: usize,
/// Exclusive end position of the `&str`.
pub end: usize,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Suggestion<'source> {
/// Suggestions to provide as valid exprs.
ValidExprs(Vec<&'source str>),
/// Simple message to give to the user.
Hint(&'source str),
}
/// Whether this is a denied error or warning.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Severity {
/// Error should prevent the operation from progressing.
///
/// Recommended when the user needs to address an issue before retrying the operation.
Deny,
/// Error should not prevent the operation from continuing.
///
/// Recommended for informatives such as deprecation notices and convention changes.
Warn,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment