Skip to content

Commit

Permalink
feat(deps): move fancy reporter (and its deps) to a feature
Browse files Browse the repository at this point in the history
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: #25
  • Loading branch information
zkat committed Sep 16, 2021
1 parent 3d74a50 commit bc495e6
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 13 deletions.
33 changes: 24 additions & 9 deletions Cargo.toml
Expand Up @@ -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"
Expand All @@ -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"]
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/eyreish/mod.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/handlers/mod.rs
Expand Up @@ -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;
2 changes: 2 additions & 0 deletions src/lib.rs
Expand Up @@ -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::*;
Expand All @@ -14,6 +15,7 @@ pub use protocol::*;
mod chain;
mod error;
mod eyreish;
#[cfg(feature = "fancy")]
mod handler;
mod handlers;
mod named_source;
Expand Down
9 changes: 7 additions & 2 deletions tests/narrated.rs
@@ -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();
Expand Down
2 changes: 2 additions & 0 deletions tests/printer.rs
@@ -1,3 +1,5 @@
#![cfg(feature = "fancy")]

use miette::{
Diagnostic, GraphicalReportHandler, GraphicalTheme, MietteError, NamedSource,
NarratableReportHandler, Report, SourceSpan,
Expand Down

0 comments on commit bc495e6

Please sign in to comment.