mirror of https://github.com/zkat/miette.git
feat(derive): Make `miette-derive` be able to be turned off (#304)
This commit is contained in:
parent
3d6f903df0
commit
c7ba5b7e52
|
|
@ -14,7 +14,7 @@ exclude = ["images/", "tests/", "miette-derive/"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
thiserror = "1.0.40"
|
thiserror = "1.0.40"
|
||||||
miette-derive = { path = "miette-derive", version = "=5.10.0" }
|
miette-derive = { path = "miette-derive", version = "=5.10.0", optional = true }
|
||||||
once_cell = "1.8.0"
|
once_cell = "1.8.0"
|
||||||
unicode-width = "0.1.9"
|
unicode-width = "0.1.9"
|
||||||
|
|
||||||
|
|
@ -44,7 +44,8 @@ lazy_static = "1.4"
|
||||||
serde_json = "1.0.64"
|
serde_json = "1.0.64"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = ["derive"]
|
||||||
|
derive = ["miette-derive"]
|
||||||
no-format-args-capture = []
|
no-format-args-capture = []
|
||||||
fancy-no-backtrace = [
|
fancy-no-backtrace = [
|
||||||
"owo-colors",
|
"owo-colors",
|
||||||
|
|
|
||||||
42
src/error.rs
42
src/error.rs
|
|
@ -1,27 +1,51 @@
|
||||||
use std::io;
|
use std::{fmt, io};
|
||||||
|
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use crate::{self as miette, Diagnostic};
|
use crate::Diagnostic;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Error enum for miette. Used by certain operations in the protocol.
|
Error enum for miette. Used by certain operations in the protocol.
|
||||||
*/
|
*/
|
||||||
#[derive(Debug, Diagnostic, Error)]
|
#[derive(Debug, Error)]
|
||||||
pub enum MietteError {
|
pub enum MietteError {
|
||||||
/// Wrapper around [`std::io::Error`]. This is returned when something went
|
/// Wrapper around [`std::io::Error`]. This is returned when something went
|
||||||
/// wrong while reading a [`SourceCode`](crate::SourceCode).
|
/// wrong while reading a [`SourceCode`](crate::SourceCode).
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
#[diagnostic(code(miette::io_error), url(docsrs))]
|
|
||||||
IoError(#[from] io::Error),
|
IoError(#[from] io::Error),
|
||||||
|
|
||||||
/// Returned when a [`SourceSpan`](crate::SourceSpan) extends beyond the
|
/// Returned when a [`SourceSpan`](crate::SourceSpan) extends beyond the
|
||||||
/// bounds of a given [`SourceCode`](crate::SourceCode).
|
/// bounds of a given [`SourceCode`](crate::SourceCode).
|
||||||
#[error("The given offset is outside the bounds of its Source")]
|
#[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,
|
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,
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -652,6 +652,7 @@
|
||||||
//! and some from [`thiserror`](https://github.com/dtolnay/thiserror), also
|
//! and some from [`thiserror`](https://github.com/dtolnay/thiserror), also
|
||||||
//! under the Apache License. Some code is taken from
|
//! under the Apache License. Some code is taken from
|
||||||
//! [`ariadne`](https://github.com/zesterer/ariadne), which is MIT licensed.
|
//! [`ariadne`](https://github.com/zesterer/ariadne), which is MIT licensed.
|
||||||
|
#[cfg(feature = "derive")]
|
||||||
pub use miette_derive::*;
|
pub use miette_derive::*;
|
||||||
|
|
||||||
pub use error::*;
|
pub use error::*;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue