how.rs/src/context.rs

131 lines
3.2 KiB
Rust
Raw Normal View History

2022-07-21 20:39:47 -04:00
use std::borrow::Cow;
2023-01-17 07:29:58 -05:00
use std::fmt;
2022-07-21 20:40:44 -04:00
/// Provides context furthering the explanation of *how* you got to an error.
2022-07-21 20:39:47 -04:00
#[derive(Debug, Clone)]
pub struct Context(ContextInner);
2023-01-17 07:29:58 -05:00
impl fmt::Display for Context {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
#[derive(Debug, Clone)]
enum ContextInner {
Elem(ContextElem),
Compound(Vec<ContextElem>),
}
#[derive(Debug, Clone)]
enum ContextElem {
Str(&'static str),
String(String),
}
impl fmt::Display for ContextInner {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Elem(elem) => fmt::Display::fmt(elem, f),
Self::Compound(elems) => {
let mut elems = elems.iter();
if let Some(elem) = elems.next() {
fmt::Display::fmt(elem, f)?;
for elem in elems {
write!(f, "\n{elem}")?;
2022-07-21 20:40:44 -04:00
}
2022-07-21 20:39:47 -04:00
}
2022-07-21 20:40:44 -04:00
Ok(())
2022-07-21 20:39:47 -04:00
}
}
}
}
2023-01-17 07:29:58 -05:00
impl fmt::Display for ContextElem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Str(s) => f.write_str(s),
Self::String(s) => f.write_str(s),
}
}
2022-07-21 20:39:47 -04:00
}
pub trait IntoContext {
fn into_context(self) -> Context;
2022-07-21 20:40:44 -04:00
#[inline(always)]
2022-07-21 20:39:47 -04:00
fn chain(self, other: impl IntoContext) -> Context
where
Self: Sized,
{
self.into_context().chain(other)
}
}
impl IntoContext for Context {
#[inline(always)]
fn into_context(self) -> Context {
self
}
2022-07-21 20:40:44 -04:00
/// Chains another piece of context that is equal from a hierarchical perspective.
2022-09-06 07:47:11 -04:00
#[inline]
2022-07-21 20:40:44 -04:00
fn chain(self, other: impl IntoContext) -> Self {
2023-01-17 07:29:58 -05:00
Context(ContextInner::Compound(match self.0 {
ContextInner::Compound(mut elems) => {
match other.into_context().0 {
ContextInner::Elem(elem) => elems.push(elem),
ContextInner::Compound(mut elems1) => elems.append(&mut elems1),
};
elems
2022-07-21 20:40:44 -04:00
}
2023-01-17 07:29:58 -05:00
ContextInner::Elem(elem) => {
match other.into_context().0 {
ContextInner::Elem(elem1) => vec![elem, elem1],
ContextInner::Compound(mut elems) => {
elems.insert(0, elem);
elems
}
}
},
2022-09-06 07:47:11 -04:00
}))
2022-07-21 20:40:44 -04:00
}
2022-07-21 20:39:47 -04:00
}
impl IntoContext for String {
2023-01-17 07:29:58 -05:00
#[inline(always)]
2022-07-21 20:39:47 -04:00
fn into_context(self) -> Context {
2023-01-17 07:29:58 -05:00
Context(ContextInner::Elem(ContextElem::String(self)))
2022-07-21 20:39:47 -04:00
}
}
impl IntoContext for &'static str {
2023-01-17 07:29:58 -05:00
#[inline(always)]
2022-07-21 20:39:47 -04:00
fn into_context(self) -> Context {
2023-01-17 07:29:58 -05:00
Context(ContextInner::Elem(ContextElem::Str(self)))
2022-07-21 20:39:47 -04:00
}
}
impl IntoContext for Cow<'static, str> {
2023-01-17 07:29:58 -05:00
#[inline]
2022-07-21 20:39:47 -04:00
fn into_context(self) -> Context {
2023-01-17 07:29:58 -05:00
match self {
Cow::Borrowed(s) => s.into_context(),
Cow::Owned(s) => s.into_context(),
}
2022-07-21 20:39:47 -04:00
}
}
impl<C, F> IntoContext for F
where
C: IntoContext,
F: FnOnce() -> C,
{
#[inline(always)]
fn into_context(self) -> Context {
self().into_context()
}
}