diff --git a/simple-eyre/.github/workflows/ci.yml b/simple-eyre/.github/workflows/ci.yml new file mode 100644 index 0000000..f6a7b52 --- /dev/null +++ b/simple-eyre/.github/workflows/ci.yml @@ -0,0 +1,121 @@ +on: + push: + branches: + - master + pull_request: {} + +name: Continuous integration + +jobs: + check: + name: Check + runs-on: ubuntu-latest + strategy: + matrix: + rust: + - stable + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + toolchain: ${{ matrix.rust }} + override: true + - uses: actions-rs/cargo@v1 + with: + command: check + + test-features: + name: Test Suite + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + - uses: actions-rs/cargo@v1 + with: + command: test + - uses: actions-rs/cargo@v1 + with: + command: test + args: --all-features + - uses: actions-rs/cargo@v1 + with: + command: test + args: --no-default-features + + test-versions: + name: Test Suite + runs-on: ubuntu-latest + strategy: + matrix: + rust: + - stable + - beta + - nightly + - 1.39.0 + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + toolchain: ${{ matrix.rust }} + override: true + - uses: actions-rs/cargo@v1 + with: + command: test + + test-os: + name: Test Suite + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macOS-latest] + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + profile: minimal + - uses: actions-rs/cargo@v1 + with: + command: test + + fmt: + name: Rustfmt + runs-on: ubuntu-latest + strategy: + matrix: + rust: + - stable + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + toolchain: ${{ matrix.rust }} + override: true + - run: rustup component add rustfmt + - uses: actions-rs/cargo@v1 + with: + command: fmt + args: --all -- --check + + clippy: + name: Clippy + runs-on: ubuntu-latest + strategy: + matrix: + rust: + - stable + steps: + - uses: actions/checkout@v1 + - uses: actions-rs/toolchain@v1 + with: + toolchain: ${{ matrix.rust }} + override: true + - run: rustup component add clippy + - uses: actions-rs/cargo@v1 + with: + command: clippy + args: --all-targets --all-features -- -D warnings diff --git a/simple-eyre/.gitignore b/simple-eyre/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/simple-eyre/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/simple-eyre/CHANGELOG.md b/simple-eyre/CHANGELOG.md new file mode 100644 index 0000000..36d49a8 --- /dev/null +++ b/simple-eyre/CHANGELOG.md @@ -0,0 +1,18 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + + + +## [Unreleased] - ReleaseDate + +## [0.3.1] - 2021-06-24 +# Fixed +- Fixed lifetime inference error caused by recent `std` change. + + + +[Unreleased]: https://github.com/yaahc/simple-eyre/compare/v0.3.1...HEAD +[0.3.1]: https://github.com/yaahc/displaydoc/releases/tag/v0.3.1 diff --git a/simple-eyre/Cargo.toml b/simple-eyre/Cargo.toml new file mode 100644 index 0000000..6ffb84e --- /dev/null +++ b/simple-eyre/Cargo.toml @@ -0,0 +1,61 @@ +[package] +name = "simple-eyre" +version = "0.3.1" +authors = ["Jane Lusby "] +edition = "2018" +license = "MIT OR Apache-2.0" +readme = "README.md" +repository = "https://github.com/yaahc/simple-eyre" +homepage = "https://github.com/yaahc/simple-eyre" +documentation = "https://docs.rs/simple-eyre" +keywords = ["error"] +description = """ +One of the simplest error reporters one can build ontop of eyre, defining only an error report +""" + +[dependencies] +eyre = "0.6.0" +indenter = "0.3.0" + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] + +[package.metadata.release] +no-dev-version = true + +[[package.metadata.release.pre-release-replacements]] +file = "CHANGELOG.md" +search = "Unreleased" +replace="{{version}}" + +[[package.metadata.release.pre-release-replacements]] +file = "src/lib.rs" +search = "#!\\[doc\\(html_root_url.*" +replace = "#![doc(html_root_url = \"https://docs.rs/{{crate_name}}/{{version}}\")]" +exactly = 1 + +[[package.metadata.release.pre-release-replacements]] +file = "CHANGELOG.md" +search = "ReleaseDate" +replace="{{date}}" + +[[package.metadata.release.pre-release-replacements]] +file="CHANGELOG.md" +search="" +replace="\n\n## [Unreleased] - ReleaseDate" +exactly=1 + +# Disable this replacement on the very first release +[[package.metadata.release.pre-release-replacements]] +file = "CHANGELOG.md" +search = "\\.\\.\\.HEAD" +replace="...{{tag_name}}" +exactly = 1 +# END SECTION, do not comment out the replacement below this, and do not reorder them + +[[package.metadata.release.pre-release-replacements]] +file="CHANGELOG.md" +search="" +replace="\n[Unreleased]: https://github.com/yaahc/{{crate_name}}/compare/{{tag_name}}...HEAD" +exactly=1 diff --git a/simple-eyre/README.md b/simple-eyre/README.md new file mode 100644 index 0000000..466f644 --- /dev/null +++ b/simple-eyre/README.md @@ -0,0 +1,51 @@ +## simple-eyre + +[![Latest Version](https://img.shields.io/crates/v/simple-eyre.svg)](https://crates.io/crates/simple-eyre) +[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/simple-eyre) + +This library provides a custom [`eyre::EyreHandler`] type for usage with [`eyre`] that provides +a minimal error report with no additional context. Essentially the minimal implementation of an +error reporter. + +## Setup + +Add the following to your toml file: + +```toml +[dependencies] +simple-eyre = "0.3" +``` + +Then install the hook handler before constructing any `eyre::Report` types. + +# Example + +```rust,should_panic +use simple_eyre::eyre::{eyre, WrapErr, Report}; + +fn main() -> Result<(), Report> { + simple_eyre::install()?; + + let e: Report = eyre!("oh no this program is just bad!"); + + Err(e).wrap_err("usage example successfully experienced a failure") +} +``` + +[`eyre::EyreHandler`]: https://docs.rs/eyre/*/eyre/trait.EyreHandler.html +[`eyre`]: https://docs.rs/eyre + +#### License + + +Licensed under either of Apache License, Version +2.0 or MIT license at your option. + + +
+ + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in this crate by you, as defined in the Apache-2.0 license, shall +be dual licensed as above, without any additional terms or conditions. + diff --git a/simple-eyre/src/lib.rs b/simple-eyre/src/lib.rs new file mode 100644 index 0000000..e9c9e24 --- /dev/null +++ b/simple-eyre/src/lib.rs @@ -0,0 +1,119 @@ +//! This library provides a custom [`eyre::EyreHandler`] type for usage with [`eyre`] that provides +//! a minimal error report with no additional context. Essentially the minimal implementation of an +//! error reporter. +//! +//! ## Setup +//! +//! Add the following to your toml file: +//! +//! ```toml +//! [dependencies] +//! simple-eyre = "0.3" +//! ``` +//! +//! Then install the hook handler before constructing any `eyre::Report` types. +//! +//! # Example +//! +//! ```rust,should_panic +//! use simple_eyre::eyre::{eyre, WrapErr, Report}; +//! +//! fn main() -> Result<(), Report> { +//! simple_eyre::install()?; +//! +//! let e: Report = eyre!("oh no this program is just bad!"); +//! +//! Err(e).wrap_err("usage example successfully experienced a failure") +//! } +//! ``` +//! +//! [`eyre::EyreHandler`]: https://docs.rs/eyre/*/eyre/trait.EyreHandler.html +//! [`eyre`]: https://docs.rs/eyre +#![doc(html_root_url = "https://docs.rs/simple-eyre/0.3.1")] +#![warn( + missing_debug_implementations, + missing_docs, + missing_doc_code_examples, + rust_2018_idioms, + unreachable_pub, + bad_style, + const_err, + dead_code, + improper_ctypes, + non_shorthand_field_patterns, + no_mangle_generic_items, + overflowing_literals, + path_statements, + patterns_in_fns_without_body, + private_in_public, + unconditional_recursion, + unused, + unused_allocation, + unused_comparisons, + unused_parens, + while_true +)] +pub use eyre; +#[doc(hidden)] +pub use eyre::{Report, Result}; + +use eyre::EyreHandler; +use indenter::indented; +use std::error::Error; + +/// A custom context type for minimal error reporting via `eyre` +#[derive(Debug)] +pub struct Handler; + +impl EyreHandler for Handler { + fn debug( + &self, + error: &(dyn Error + 'static), + f: &mut core::fmt::Formatter<'_>, + ) -> core::fmt::Result { + use core::fmt::Write as _; + + if f.alternate() { + return core::fmt::Debug::fmt(error, f); + } + + write!(f, "{}", error)?; + + if let Some(cause) = error.source() { + write!(f, "\n\nCaused by:")?; + + let multiple = cause.source().is_some(); + let errors = std::iter::successors(Some(cause), |e| (*e).source()); + + for (n, error) in errors.enumerate() { + writeln!(f)?; + + if multiple { + write!(indented(f).ind(n), "{}", error)?; + } else { + write!(indented(f), "{}", error)?; + } + } + } + + Ok(()) + } +} + +/// Install the `simple-eyre` hook as the global error report hook. +/// +/// # Details +/// +/// This function must be called to enable the customization of `eyre::Report` +/// provided by `simple-eyre`. This function should be called early, ideally +/// before any errors could be encountered. +/// +/// Only the first install will succeed. Calling this function after another +/// report handler has been installed will cause an error. **Note**: This +/// function _must_ be called before any `eyre::Report`s are constructed to +/// prevent the default handler from being installed. +pub fn install() -> Result<()> { + crate::eyre::set_hook(Box::new(move |_| Box::new(Handler)))?; + + Ok(()) +}