Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make miette-derive be able to be turned off #304

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exclude = ["images/", "tests/", "miette-derive/"]

[dependencies]
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"
unicode-width = "0.1.9"

Expand Down Expand Up @@ -44,7 +44,8 @@ lazy_static = "1.4"
serde_json = "1.0.64"

[features]
default = []
default = ["derive"]
derive = ["miette-derive"]
no-format-args-capture = []
fancy-no-backtrace = [
"owo-colors",
Expand Down
37 changes: 28 additions & 9 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
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");
Some(Box::new(format!(
"https://docs.rs/miette/{crate_version}/miette/enum.MietteError.html"
ManicMarrc marked this conversation as resolved.
Show resolved Hide resolved
)))
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@
//! and some from [`thiserror`](https://github.com/dtolnay/thiserror), also
//! under the Apache License. Some code is taken from
//! [`ariadne`](https://github.com/zesterer/ariadne), which is MIT licensed.
#[cfg(feature = "derive")]
pub use miette_derive::*;

pub use error::*;
Expand Down