feat(deps): move fancy reporter (and its deps) to a feature

BREAKING CHANGE: The default fancy reporter is no longer available unless you enable the "fancy" feature. This also means you will not be pulling in a bunch of deps if you are using miette for a library

Fixes: https://github.com/zkat/miette/issues/25
This commit is contained in:
Kat Marchán 2021-09-16 13:18:57 -07:00
parent 4c2463f9ae
commit 247e8f8b39
7 changed files with 63 additions and 13 deletions

View File

@ -15,15 +15,16 @@ exclude = ["images/", "tests/", "miette-derive/"]
thiserror = "1.0.26"
miette-derive = { path = "miette-derive", version = "=2.2.1-alpha.0"}
once_cell = "1.8.0"
owo-colors = "2.0.0"
atty = "0.2.14"
ci_info = "0.14.2"
textwrap = "0.14.2"
term_size = "0.3.2"
unicode-width = "0.1.8"
supports-hyperlinks = "1.1.0"
supports-color = "1.0.2"
supports-unicode = "1.0.0"
owo-colors = { version = "2.0.0", optional = true }
atty = { version = "0.2.14", optional = true }
ci_info = { version = "0.14.2", optional = true }
textwrap = { version = "0.14.2", optional = true }
term_size = { version = "0.3.2", optional = true }
unicode-width = { version = "0.1.8", optional = true }
supports-hyperlinks = { version = "1.1.0", optional = true }
supports-color = { version = "1.0.2", optional = true }
supports-unicode = { version = "1.0.0", optional = true }
[dev-dependencies]
semver = "1.0.4"
@ -35,5 +36,19 @@ rustversion = "1.0"
trybuild = { version = "1.0.19", features = ["diff"] }
syn = { version = "1.0", features = ["full"] }
[features]
default = []
fancy = [
"owo-colors",
"atty",
"ci_info",
"textwrap",
"term_size",
"unicode-width",
"supports-hyperlinks",
"supports-color",
"supports-unicode"
]
[workspace]
members = ["miette-derive"]

View File

@ -66,6 +66,12 @@ Using [`cargo-edit`](https://crates.io/crates/cargo-edit):
$ cargo add miette
```
If you want to use the fancy printer in all these screenshots:
```sh
$ cargo add miette --features fancy
```
## Example
```rust
@ -240,6 +246,14 @@ fn pretend_this_is_main() -> Result<()> {
}
```
Please note: in order to get fancy diagnostic rendering with all the pretty
colors and arrows, you should install `miette` with the `fancy` feature
enabled:
```toml
miette = { version = "X.Y.Z", features = ["fancy"] }
```
### ... diagnostic code URLs
`miette` supports providing a URL for individual diagnostics. This URL will be

View File

@ -26,7 +26,12 @@ pub use ReportHandler as EyreContext;
#[allow(unreachable_pub)]
pub use WrapErr as Context;
use crate::{Diagnostic, MietteHandler};
use crate::Diagnostic;
#[cfg(feature = "fancy")]
use crate::MietteHandler;
#[cfg(not(feature = "fancy"))]
use crate::NarratableReportHandler;
use error::ErrorImpl;
mod context;
@ -87,7 +92,10 @@ fn capture_handler(error: &(dyn Diagnostic + 'static)) -> Box<dyn ReportHandler>
}
fn get_default_printer(_err: &(dyn Diagnostic + 'static)) -> Box<dyn ReportHandler + 'static> {
Box::new(MietteHandler::new())
#[cfg(feature = "fancy")]
return Box::new(MietteHandler::new());
#[cfg(not(feature = "fancy"))]
return Box::new(NarratableReportHandler::new());
}
impl dyn ReportHandler {

View File

@ -3,12 +3,16 @@ Reporters included with `miette`.
*/
#[allow(unreachable_pub)]
#[cfg(feature = "fancy")]
pub use graphical::*;
#[allow(unreachable_pub)]
pub use narratable::*;
#[allow(unreachable_pub)]
#[cfg(feature = "fancy")]
pub use theme::*;
#[cfg(feature = "fancy")]
mod graphical;
mod narratable;
#[cfg(feature = "fancy")]
mod theme;

View File

@ -6,6 +6,7 @@ pub use miette_derive::*;
pub use error::*;
pub use eyreish::*;
#[cfg(feature = "fancy")]
pub use handler::*;
pub use handlers::*;
pub use named_source::*;
@ -14,6 +15,7 @@ pub use protocol::*;
mod chain;
mod error;
mod eyreish;
#[cfg(feature = "fancy")]
mod handler;
mod handlers;
mod named_source;

View File

@ -1,13 +1,18 @@
use miette::{
Diagnostic, GraphicalReportHandler, GraphicalTheme, MietteError, NamedSource,
Diagnostic, MietteError, NamedSource,
NarratableReportHandler, Report, SourceSpan,
};
#[cfg(feature = "fancy")]
use miette::{GraphicalReportHandler, GraphicalTheme};
use thiserror::Error;
fn fmt_report(diag: Report) -> String {
let mut out = String::new();
// Mostly for dev purposes.
if std::env::var("STYLE").is_ok() {
if cfg!(feature = "fancy") && std::env::var("STYLE").is_ok() {
#[cfg(feature = "fancy")]
GraphicalReportHandler::new_themed(GraphicalTheme::unicode())
.render_report(&mut out, diag.as_ref())
.unwrap();

View File

@ -1,3 +1,5 @@
#![cfg(feature = "fancy")]
use miette::{
Diagnostic, GraphicalReportHandler, GraphicalTheme, MietteError, NamedSource,
NarratableReportHandler, Report, SourceSpan,