Skip to content

make PointerBuf::parse error basic by default #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParseError>> */ = 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<ParseError>> */ = PointerBuf::parse(ptr_str).unwrap_err();
let err /* Result<PointerBuf, Report<ParseError>> */ = 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 |
Expand Down
2 changes: 1 addition & 1 deletion src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ where
F: FnOnce() -> S,
S: Into<<Self::Error as Diagnostic>::Subject>,
{
self.diagnose(f())
self.map_err(|error| error.into_report(f()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
26 changes: 8 additions & 18 deletions src/pointer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
diagnostic::{diagnostic_url, Diagnostic, Label, Report},
diagnostic::{diagnostic_url, Diagnostic, Label},
token::EncodingError,
Components, InvalidEncoding, Token, Tokens,
};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -679,7 +678,7 @@ impl PartialEq<&str> for Pointer {
&&self.0 == other
}
}
impl<'p> PartialEq<String> for &'p Pointer {
impl PartialEq<String> for &Pointer {
fn eq(&self, other: &String) -> bool {
self.0.eq(other)
}
Expand Down Expand Up @@ -869,7 +868,7 @@ impl PartialOrd<&str> for PointerBuf {
}
}

impl<'p> PartialOrd<PointerBuf> for &'p Pointer {
impl PartialOrd<PointerBuf> for &Pointer {
fn partial_cmp(&self, other: &PointerBuf) -> Option<Ordering> {
self.0.partial_cmp(other.0.as_str())
}
Expand Down Expand Up @@ -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<String>) -> Result<Self, RichParseError> {
pub fn parse<'s>(s: impl Into<Cow<'s, str>>) -> Result<Self, ParseError> {
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),
}
}

Expand Down Expand Up @@ -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<ParseError>;

/// Returned from [`PointerBuf::replace`] when the provided index is out of
/// bounds.
#[derive(Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -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));
}
}