-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(derive): Make
miette-derive
be able to be turned off (#304)
- Loading branch information
1 parent
3d6f903
commit c7ba5b7
Showing
3 changed files
with
37 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,51 @@ | ||
use std::io; | ||
use std::{fmt, io}; | ||
|
||
use thiserror::Error; | ||
|
||
use crate::{self as miette, Diagnostic}; | ||
use crate::Diagnostic; | ||
|
||
/** | ||
Error enum for miette. Used by certain operations in the protocol. | ||
*/ | ||
#[derive(Debug, Diagnostic, Error)] | ||
#[derive(Debug, Error)] | ||
pub enum MietteError { | ||
/// Wrapper around [`std::io::Error`]. This is returned when something went | ||
/// wrong while reading a [`SourceCode`](crate::SourceCode). | ||
#[error(transparent)] | ||
#[diagnostic(code(miette::io_error), url(docsrs))] | ||
IoError(#[from] io::Error), | ||
|
||
/// Returned when a [`SourceSpan`](crate::SourceSpan) extends beyond the | ||
/// bounds of a given [`SourceCode`](crate::SourceCode). | ||
#[error("The given offset is outside the bounds of its Source")] | ||
#[diagnostic( | ||
code(miette::span_out_of_bounds), | ||
help("Double-check your spans. Do you have an off-by-one error?"), | ||
url(docsrs) | ||
)] | ||
OutOfBounds, | ||
} | ||
|
||
impl Diagnostic for MietteError { | ||
fn code<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> { | ||
match self { | ||
MietteError::IoError(_) => Some(Box::new("miette::io_error")), | ||
MietteError::OutOfBounds => Some(Box::new("miette::span_out_of_bounds")), | ||
} | ||
} | ||
|
||
fn help<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> { | ||
match self { | ||
MietteError::IoError(_) => None, | ||
MietteError::OutOfBounds => Some(Box::new( | ||
"Double-check your spans. Do you have an off-by-one error?", | ||
)), | ||
} | ||
} | ||
|
||
fn url<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> { | ||
let crate_version = env!("CARGO_PKG_VERSION"); | ||
let variant = match self { | ||
MietteError::IoError(_) => "#variant.IoError", | ||
MietteError::OutOfBounds => "#variant.OutOfBounds", | ||
}; | ||
Some(Box::new(format!( | ||
"https://docs.rs/miette/{}/miette/enum.MietteError.html{}", | ||
crate_version, variant, | ||
))) | ||
} | ||
} |
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