diff --git a/README.md b/README.md index a971337..3455e45 100644 --- a/README.md +++ b/README.md @@ -150,22 +150,17 @@ and the [`String`] which failed to parse or the [`PointerBuf`] which failed to resolve or assign. ```rust - use jsonptr::{Pointer, Diagnose}; + use jsonptr::{Pointer, PointerBuf, Diagnose}; let ptr_str = "foo/bar"; let err /* Result<&Pointer, Report> */ = Pointer::parse(ptr_str).diagnose(ptr_str).unwrap_err(); assert!(err.original().is_no_leading_slash()); -``` - -In the case of [`PointerBuf::parse`], the [`ParseError`] is always wrapped in a -[`Report`] so that the input `String` is not dropped. -```rust - use jsonptr::{PointerBuf}; - let ptr_str = "foo/bar"; - let err /* Result<&PointerBuf, Report> */ = PointerBuf::parse(ptr_str).unwrap_err(); + let err /* Result> */ = PointerBuf::parse(ptr_str).diagnose_with(|| ptr_str.to_owned()).unwrap_err(); assert!(err.original().is_no_leading_slash()); ``` +Note that when using this feature, allocation is unavoidable when parsing. + ## Feature Flags | Flag | Description | Enables | Default | diff --git a/src/diagnostic.rs b/src/diagnostic.rs index be65970..71d4643 100644 --- a/src/diagnostic.rs +++ b/src/diagnostic.rs @@ -220,7 +220,7 @@ where F: FnOnce() -> S, S: Into<::Subject>, { - self.diagnose(f()) + self.map_err(|error| error.into_report(f())) } } diff --git a/src/lib.rs b/src/lib.rs index 1339c83..0b84c63 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,7 +68,7 @@ pub mod diagnostic; pub use diagnostic::{Diagnose, Report}; mod pointer; -pub use pointer::{ParseError, Pointer, PointerBuf, RichParseError}; +pub use pointer::{ParseError, Pointer, PointerBuf}; mod token; pub use token::{EncodingError, InvalidEncoding, Token, Tokens}; diff --git a/src/pointer.rs b/src/pointer.rs index d3be5ed..c1e5a16 100644 --- a/src/pointer.rs +++ b/src/pointer.rs @@ -1,5 +1,5 @@ use crate::{ - diagnostic::{diagnostic_url, Diagnostic, Label, Report}, + diagnostic::{diagnostic_url, Diagnostic, Label}, token::EncodingError, Components, InvalidEncoding, Token, Tokens, }; @@ -368,7 +368,6 @@ impl Pointer { /// [`ResolveError`]: `crate::resolve::ResolveError` /// [`Token`]: `crate::Token` /// [`Index`]: `crate::index::Index` - #[cfg(feature = "resolve")] pub fn resolve_mut<'v, R: crate::ResolveMut>( &self, @@ -679,7 +678,7 @@ impl PartialEq<&str> for Pointer { &&self.0 == other } } -impl<'p> PartialEq for &'p Pointer { +impl PartialEq for &Pointer { fn eq(&self, other: &String) -> bool { self.0.eq(other) } @@ -869,7 +868,7 @@ impl PartialOrd<&str> for PointerBuf { } } -impl<'p> PartialOrd for &'p Pointer { +impl PartialOrd for &Pointer { fn partial_cmp(&self, other: &PointerBuf) -> Option { self.0.partial_cmp(other.0.as_str()) } @@ -923,11 +922,12 @@ impl PointerBuf { /// /// ## Errors /// Returns a [`RichParseError`] if the string is not a valid JSON Pointer. - pub fn parse(s: impl Into) -> Result { + pub fn parse<'s>(s: impl Into>) -> Result { let s = s.into(); - match validate(&s) { - Ok(_) => Ok(Self(s)), - Err(err) => Err(err.into_report(s)), + // must explicitly match due to borrow checker limitations + match validate(s.as_ref()) { + Ok(_) => Ok(Self(s.into_owned())), + Err(err) => Err(err), } } @@ -1328,9 +1328,6 @@ impl std::error::Error for ParseError { } } -/// A rich error type that includes the original string that failed to parse. -pub type RichParseError = Report; - /// Returned from [`PointerBuf::replace`] when the provided index is out of /// bounds. #[derive(Debug, PartialEq, Eq)] @@ -2343,11 +2340,4 @@ mod tests { let unboxed = boxed.into_buf(); assert_eq!(subjectal, unboxed); } - - #[test] - #[cfg(feature = "miette")] - fn quick_miette_spike() { - let err = PointerBuf::parse("hello-world").unwrap_err(); - println!("{:?}", miette::Report::from(err)); - } }