Skip to content

Commit

Permalink
revert name + some simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
asmello committed Nov 18, 2024
1 parent a9cfea1 commit cf3bcfa
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
//!
use crate::{
diagnostic::{impl_diagnostic_url, IntoDiagnostic, Label},
index::{OutOfBoundsError, ParseIndexError},
report::{impl_diagnostic_url, Enrich, Label},
Pointer,
};
use alloc::borrow::Cow;
Expand Down Expand Up @@ -228,7 +228,7 @@ impl fmt::Display for Error {
}
}

impl<'s> Enrich<'s> for Error {
impl<'s> IntoDiagnostic<'s> for Error {
type Subject = Cow<'s, Pointer>;

fn url() -> &'static str {
Expand Down
75 changes: 35 additions & 40 deletions src/report.rs → src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
use core::fmt;

/// Implemented by errors which can be converted into a [`Report`].
pub trait Enrich<'s>: Sized + private::Sealed {
/// Implemented by errors which can be converted into a [`Diagnostic`].
pub trait IntoDiagnostic<'s>: Sized + private::Sealed {
/// The value which caused the error.
type Subject;

/// Enrich the error with its subject.
fn enrich(self, subject: impl Into<Self::Subject>) -> Enriched<'s, Self> {
Enriched::new(self, subject)
fn enrich(self, subject: impl Into<Self::Subject>) -> Diagnostic<'s, Self> {
Diagnostic::new(self, subject)
}

/// The docs.rs URL for this error
Expand Down Expand Up @@ -44,12 +44,12 @@ impl From<Label> for miette::LabeledSpan {
/// An error wrapper which includes the [`String`] which failed to parse or the
/// [`PointerBuf`] being used.
#[derive(Clone, PartialEq, Eq)]
pub struct Enriched<'s, S: Enrich<'s>> {
pub struct Diagnostic<'s, S: IntoDiagnostic<'s>> {
source: S,
subject: S::Subject,
}

impl<'s, S: Enrich<'s>> Enriched<'s, S> {
impl<'s, S: IntoDiagnostic<'s>> Diagnostic<'s, S> {
/// Create a new `Report` with the given subject and error.
fn new(source: S, subject: impl Into<S::Subject>) -> Self {
Self {
Expand All @@ -58,11 +58,6 @@ impl<'s, S: Enrich<'s>> Enriched<'s, S> {
}
}

/// Returns labels associated with spans where the error occurs.
pub fn labels(&self) -> Option<Box<dyn Iterator<Item = Label>>> {
self.source.labels(&self.subject)
}

/// The value which caused the error.
pub fn subject(&self) -> &S::Subject {
&self.subject
Expand All @@ -73,15 +68,15 @@ impl<'s, S: Enrich<'s>> Enriched<'s, S> {
&self.source
}

/// The docs.rs URL for the error of this [`Report`].
pub fn url() -> &'static str {
S::url()
/// The original parts of the [`Diagnostic`].
pub fn decompose(self) -> (S, S::Subject) {
(self.source, self.subject)
}
}

impl<'s, S> core::ops::Deref for Enriched<'s, S>
impl<'s, S> core::ops::Deref for Diagnostic<'s, S>
where
S: Enrich<'s>,
S: IntoDiagnostic<'s>,
{
type Target = S;

Expand All @@ -90,18 +85,18 @@ where
}
}

impl<'s, S> fmt::Display for Enriched<'s, S>
impl<'s, S> fmt::Display for Diagnostic<'s, S>
where
S: Enrich<'s> + fmt::Display,
S: IntoDiagnostic<'s> + fmt::Display,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fmt::Display::fmt(&self.source, f)
}
}

impl<'s, S> fmt::Debug for Enriched<'s, S>
impl<'s, S> fmt::Debug for Diagnostic<'s, S>
where
S: Enrich<'s> + fmt::Debug,
S: IntoDiagnostic<'s> + fmt::Debug,
S::Subject: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -113,9 +108,9 @@ where
}

#[cfg(feature = "std")]
impl<'s, S> std::error::Error for Enriched<'s, S>
impl<'s, S> std::error::Error for Diagnostic<'s, S>
where
S: Enrich<'s> + fmt::Debug + std::error::Error + 'static,
S: IntoDiagnostic<'s> + fmt::Debug + std::error::Error + 'static,
S::Subject: fmt::Debug,
{
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
Expand All @@ -124,36 +119,36 @@ where
}

#[cfg(feature = "miette")]
impl<'s, S> miette::Diagnostic for Enriched<'s, S>
impl<'s, S> miette::Diagnostic for Diagnostic<'s, S>
where
S: Enrich<'s> + fmt::Debug + std::error::Error + 'static,
S: IntoDiagnostic<'s> + fmt::Debug + std::error::Error + 'static,
S::Subject: fmt::Debug + miette::SourceCode,
{
fn url<'a>(&'a self) -> Option<Box<dyn core::fmt::Display + 'a>> {
Some(Box::new(Self::url()))
Some(Box::new(S::url()))
}

fn source_code(&self) -> Option<&dyn miette::SourceCode> {
Some(&self.subject)
}

fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
Some(Box::new(self.labels()?.map(Into::into)))
Some(Box::new(S::labels(self, &self.subject)?.map(Into::into)))
}
}

macro_rules! impl_diagnostic_url {
(enum $type:ident) => {
$crate::report::impl_diagnostic_url!("enum", "", $type)
$crate::diagnostic::impl_diagnostic_url!("enum", "", $type)
};
(struct $type:ident) => {
$crate::report::impl_diagnostic_url!("struct", "", $type)
$crate::diagnostic::impl_diagnostic_url!("struct", "", $type)
};
(enum $mod:ident::$type:ident) => {
$crate::report::impl_diagnostic_url!("enum", concat!("/", stringify!($mod)), $type)
$crate::diagnostic::impl_diagnostic_url!("enum", concat!("/", stringify!($mod)), $type)
};
(struct $mod:ident::$type:ident) => {
$crate::report::impl_diagnostic_url!("struct", concat!("/", stringify!($mod)), $type)
$crate::diagnostic::impl_diagnostic_url!("struct", concat!("/", stringify!($mod)), $type)
};
($kind:literal, $mod:expr, $type:ident) => {
concat!(
Expand All @@ -178,38 +173,38 @@ mod private {
}

pub trait Diagnose<'s, T> {

Check warning on line 175 in src/diagnostic.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] src/diagnostic.rs#L175

warning: missing documentation for a trait --> src/diagnostic.rs:175:1 | 175 | pub trait Diagnose<'s, T> { | ^^^^^^^^^^^^^^^^^^^^^^^^^
Raw output
src/diagnostic.rs:175:1:w:warning: missing documentation for a trait
   --> src/diagnostic.rs:175:1
    |
175 | pub trait Diagnose<'s, T> {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^


__END__
type Error: Enrich<'s>;
type Error: IntoDiagnostic<'s>;

Check warning on line 176 in src/diagnostic.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] src/diagnostic.rs#L176

warning: missing documentation for an associated type --> src/diagnostic.rs:176:5 | 176 | type Error: IntoDiagnostic<'s>; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Raw output
src/diagnostic.rs:176:5:w:warning: missing documentation for an associated type
   --> src/diagnostic.rs:176:5
    |
176 |     type Error: IntoDiagnostic<'s>;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


__END__

#[allow(clippy::missing_errors_doc)]
fn diagnose(

Check warning on line 179 in src/diagnostic.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] src/diagnostic.rs#L179

warning: missing documentation for a method --> src/diagnostic.rs:179:5 | 179 | / fn diagnose( 180 | | self, 181 | | subject: impl Into<<Self::Error as IntoDiagnostic<'s>>::Subject>, 182 | | ) -> Result<T, Diagnostic<'s, Self::Error>>; | |________________________________________________^
Raw output
src/diagnostic.rs:179:5:w:warning: missing documentation for a method
   --> src/diagnostic.rs:179:5
    |
179 | /     fn diagnose(
180 | |         self,
181 | |         subject: impl Into<<Self::Error as IntoDiagnostic<'s>>::Subject>,
182 | |     ) -> Result<T, Diagnostic<'s, Self::Error>>;
    | |________________________________________________^


__END__
self,
subject: impl Into<<Self::Error as Enrich<'s>>::Subject>,
) -> Result<T, Enriched<'s, Self::Error>>;
subject: impl Into<<Self::Error as IntoDiagnostic<'s>>::Subject>,
) -> Result<T, Diagnostic<'s, Self::Error>>;

#[allow(clippy::missing_errors_doc)]
fn diagnose_with<F, S>(self, f: F) -> Result<T, Enriched<'s, Self::Error>>
fn diagnose_with<F, S>(self, f: F) -> Result<T, Diagnostic<'s, Self::Error>>

Check warning on line 185 in src/diagnostic.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] src/diagnostic.rs#L185

warning: missing documentation for a method --> src/diagnostic.rs:185:5 | 185 | / fn diagnose_with<F, S>(self, f: F) -> Result<T, Diagnostic<'s, Self::Error>> 186 | | where 187 | | F: FnOnce() -> S, 188 | | S: Into<<Self::Error as IntoDiagnostic<'s>>::Subject>; | |______________________________________________________________^
Raw output
src/diagnostic.rs:185:5:w:warning: missing documentation for a method
   --> src/diagnostic.rs:185:5
    |
185 | /     fn diagnose_with<F, S>(self, f: F) -> Result<T, Diagnostic<'s, Self::Error>>
186 | |     where
187 | |         F: FnOnce() -> S,
188 | |         S: Into<<Self::Error as IntoDiagnostic<'s>>::Subject>;
    | |______________________________________________________________^


__END__
where
F: FnOnce() -> S,
S: Into<<Self::Error as Enrich<'s>>::Subject>;
S: Into<<Self::Error as IntoDiagnostic<'s>>::Subject>;
}

impl<'s, T, E> Diagnose<'s, T> for Result<T, E>
where
E: Enrich<'s>,
E: IntoDiagnostic<'s>,
{
type Error = E;

fn diagnose(
self,
subject: impl Into<<Self::Error as Enrich<'s>>::Subject>,
) -> Result<T, Enriched<'s, Self::Error>> {
subject: impl Into<<Self::Error as IntoDiagnostic<'s>>::Subject>,
) -> Result<T, Diagnostic<'s, Self::Error>> {
self.map_err(|error| error.enrich(subject.into()))
}

fn diagnose_with<F, S>(self, f: F) -> Result<T, Enriched<'s, Self::Error>>
fn diagnose_with<F, S>(self, f: F) -> Result<T, Diagnostic<'s, Self::Error>>
where
F: FnOnce() -> S,
S: Into<<Self::Error as Enrich<'s>>::Subject>,
S: Into<<Self::Error as IntoDiagnostic<'s>>::Subject>,
{
self.diagnose(f())
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub mod resolve;
#[cfg(feature = "resolve")]
pub use resolve::{Resolve, ResolveMut};

pub mod report;
pub mod diagnostic;

mod pointer;
pub use pointer::{ParseError, Pointer, PointerBuf};
Expand Down
6 changes: 3 additions & 3 deletions src/pointer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
report::{impl_diagnostic_url, Enrich, Enriched, Label},
diagnostic::{impl_diagnostic_url, Diagnostic, IntoDiagnostic, Label},
token::InvalidEncodingError,
Components, Token, Tokens,
};
Expand Down Expand Up @@ -942,7 +942,7 @@ impl PointerBuf {
///
/// ## Errors
/// Returns a [`RichParseError`] if the string is not a valid JSON Pointer.
pub fn parse<'s>(s: impl Into<Cow<'s, str>>) -> Result<Self, Enriched<'s, ParseError>> {
pub fn parse<'s>(s: impl Into<Cow<'s, str>>) -> Result<Self, Diagnostic<'s, ParseError>> {
let s = s.into();
match validate(&s) {
Ok(_) => Ok(Self(s.into_owned())),
Expand Down Expand Up @@ -1277,7 +1277,7 @@ impl ParseError {
}
}

impl<'s> Enrich<'s> for ParseError {
impl<'s> IntoDiagnostic<'s> for ParseError {
type Subject = Cow<'s, str>;

fn url() -> &'static str {
Expand Down

0 comments on commit cf3bcfa

Please sign in to comment.