From 110fbee6726a8bf81d2225b791d18899532dcae8 Mon Sep 17 00:00:00 2001 From: Chris Krycho Date: Fri, 8 Nov 2024 12:07:36 -0700 Subject: [PATCH] error reporting --- packages/mdbook_trpl/src/config/mod.rs | 6 ++--- packages/mdbook_trpl/src/error.rs | 32 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 packages/mdbook_trpl/src/error.rs diff --git a/packages/mdbook_trpl/src/config/mod.rs b/packages/mdbook_trpl/src/config/mod.rs index bd28abab69..2e5052d5c9 100644 --- a/packages/mdbook_trpl/src/config/mod.rs +++ b/packages/mdbook_trpl/src/config/mod.rs @@ -1,4 +1,4 @@ -//! Get any `preprocessor.trpl` config. +//! Get any `preprocessor.trpl-*` config. use mdbook::preprocess::PreprocessorContext; @@ -45,7 +45,7 @@ pub struct ParseErr; impl TryFrom<&str> for Mode { type Error = ParseErr; - fn try_from(value: &str) -> std::prelude::v1::Result { + fn try_from(value: &str) -> Result { match value { "default" => Ok(Mode::Default), "simple" => Ok(Mode::Simple), @@ -57,7 +57,7 @@ impl TryFrom<&str> for Mode { #[derive(Debug, thiserror::Error)] pub enum Error { #[error(transparent)] - Mdbook(mdbook::errors::Error), + Mdbook(#[from] mdbook::errors::Error), #[error("No config for '{0}'")] NoConfig(String), diff --git a/packages/mdbook_trpl/src/error.rs b/packages/mdbook_trpl/src/error.rs new file mode 100644 index 0000000000..ee163e2ff3 --- /dev/null +++ b/packages/mdbook_trpl/src/error.rs @@ -0,0 +1,32 @@ +pub struct Report(Box); + +#[derive(Debug, thiserror::Error)] +#[error("{0}")] +struct Simple(String); + +impl Report { + pub fn simple(s: impl Into) -> Report { + Report(Box::new(Simple(s.into()))) + } +} + +impl From for Report +where + E: std::error::Error + 'static, +{ + fn from(value: E) -> Self { + Report(Box::new(value)) + } +} + +impl std::fmt::Debug for Report { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + writeln!(f, "{:?}", self.0)?; + let mut current_err = self.0.as_ref(); + while let Some(source_err) = current_err.source() { + writeln!(f, "Caused by: {:?}", source_err)?; + current_err = source_err; + } + Ok(()) + } +}