This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds a simple error type to allow producing nicer errors
- Loading branch information
1 parent
1e1bc02
commit 1a1ee75
Showing
3 changed files
with
77 additions
and
23 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
docker-images/syntax-highlighter/crates/scip-syntax/src/scip_strict.rs
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use std::borrow::Cow; | ||
|
||
mod context_error; | ||
mod format; | ||
mod parse; | ||
|
||
|
49 changes: 49 additions & 0 deletions
49
docker-images/syntax-highlighter/crates/scip-syntax/src/scip_strict/context_error.rs
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,49 @@ | ||
use std::fmt; | ||
|
||
use nom::error::{ContextError, ErrorKind, FromExternalError, ParseError}; | ||
|
||
/// default error type, only contains the error's location and code | ||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub struct CtxError<I> { | ||
/// position of the error in the input data | ||
pub input: I, | ||
/// contextual error message | ||
pub context: &'static str, | ||
} | ||
|
||
impl<I> ParseError<I> for CtxError<I> { | ||
fn from_error_kind(input: I, _kind: ErrorKind) -> Self { | ||
CtxError { | ||
input, | ||
context: "no context set yet", | ||
} | ||
} | ||
|
||
fn append(_: I, _: ErrorKind, other: Self) -> Self { | ||
other | ||
} | ||
} | ||
|
||
impl<I> ContextError<I> for CtxError<I> { | ||
fn add_context(_input: I, context: &'static str, mut other: Self) -> Self { | ||
other.context = context; | ||
other | ||
} | ||
} | ||
|
||
impl<I, E> FromExternalError<I, E> for CtxError<I> { | ||
/// Create a new error from an input position and an external error | ||
fn from_external_error(input: I, _kind: ErrorKind, _e: E) -> Self { | ||
CtxError { | ||
input, | ||
context: "no context set yet", | ||
} | ||
} | ||
} | ||
|
||
/// The Display implementation allows the std::error::Error implementation | ||
impl<I: fmt::Display> fmt::Display for CtxError<I> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "error '{}' at: {}", self.context, self.input) | ||
} | ||
} |
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