flex_error/lib.rs
1#![no_std]
2
3/*!
4`flex-error` is a lightweight Rust library that uses macros and traits
5to switch between different error tracing implementations and no_std. The library currently supports 3 modes via Cargo feature flags: `eyre_tracer` (default), `anyhow_tracer`, and `string_tracer` (no_std).
6
7The library separates out several concepts as traits:
8`ErrorDetail`, [`ErrorTracer`], and [`ErrorSource`].
9
10 - `ErrorDetail` is responsible to structured metadata information about a specific error.
11
12 - `ErrorTracer` is responsible for tracing error chains and backtraces.
13
14 - `ErrorSource` allows generic conversion of external error types into an ErrorDetail with optional ErrorTrace.
15
16With the separation of concerns, `flex-error` allows applications to easily
17switch between different error reporting implementations,
18such as [`eyre`] and [`anyhow`], by implementing
19[`ErrorTracer`] for the respective reporters.
20
21`flex-error` defines a [`define_error!`] macro that define custom `Detail`
22types and error types implementing `ErrorSource<DefaultTracer>`.
23The [`DefaultTracer`] type is set globally by the feature flag, so that
24application error types do not have to be over-generalized.
25The trade off is that it is not possible to use multiple
26[`ErrorTracer`] implementations at the same time across different crates that
27use `flex-error`.
28
29!*/
30
31#[cfg(feature = "std")]
32extern crate std;
33
34pub extern crate alloc;
35
36#[cfg(feature = "std")]
37pub use std::error::Error as StdError;
38
39pub mod macros;
40mod source;
41mod tracer;
42pub mod tracer_impl;
43
44pub use source::*;
45pub use tracer::*;
46
47/// The `DefaultTracer` type alias is used when defining error types
48/// using [`define_error!`]. With the default Cargo features, or when
49/// the `eyre_tracer` feature is set, this is configured to use the
50/// [EyreTracer](tracer_impl::eyre::EyreTracer). Otherwise, it will
51/// be set to [AnyhowTracer](tracer_impl::anyhow::AnyhowTracer) if
52/// the `anyhow_tracer` feature is set. If neither `eyre_tracer`
53/// nor `anyhow_tracer` is set, then `DefaultTracer` is set to
54/// [StringTracer](tracer_impl::string::StringTracer).
55///
56/// We hard code globally the default error tracer to be used in
57/// [`define_error!`], to avoid making the error types overly generic.
58
59// If `eyre_tracer` feature is active, it is the default error tracer
60#[cfg(feature = "eyre_tracer")]
61pub type DefaultTracer = tracer_impl::eyre::EyreTracer;
62
63// Otherwise, if `eyre_tracer` feature is active, it is the default error tracer
64#[cfg(all(feature = "anyhow_tracer", not(feature = "eyre_tracer")))]
65pub type DefaultTracer = tracer_impl::anyhow::AnyhowTracer;
66
67// Otherwise, if `string_tracer` feature is active, it is the default error tracer
68#[cfg(all(not(feature = "eyre_tracer"), not(feature = "anyhow_tracer")))]
69pub type DefaultTracer = tracer_impl::string::StringTracer;