Skip to content

Commit

Permalink
Merge pull request #19 from chanced/fix-pop-back-on-single-token
Browse files Browse the repository at this point in the history
fix: `pop_back` was not properly popping the last token when it was the last
  • Loading branch information
chanced authored Oct 13, 2023
2 parents 35455a6 + fcb69aa commit f5e4f80
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
21 changes: 21 additions & 0 deletions src/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,24 @@ impl Delete for Value {
ptr.delete(self)
}
}

#[cfg(test)]
mod tests {
use serde_json::json;

use super::*;

#[test]
fn test_issue_18() {
let mut data = json!(
{
"Example": 21,
"test": "test"
}
);
let pointer = Pointer::new(["Example"]);
println!("{}", pointer);
pointer.delete(&mut data);
assert_eq!(json!({"test": "test"}), data);
}
}
31 changes: 19 additions & 12 deletions src/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Pointer {
}
}
/// Parses `value` as a JSON Pointer.
///
///
/// # Errors
/// returns `MalformedPointerError` if `value` is not able to be parsed as a
/// valid JSON Pointer.
Expand Down Expand Up @@ -160,18 +160,15 @@ impl Pointer {
.map_or(Some((&self.inner[1..], "")), Option::Some)
.map(|(f, b)| (f.to_owned(), b.to_owned()))
.map(|(front, back)| {
if !front.is_empty() {
self.inner = String::from("/") + &front;
} else {
if back.is_empty() {
self.inner = String::default();
Token::from_encoded(front)
} else {
self.inner = String::from("/") + &front;
Token::from_encoded(back)
}
Token::from_encoded(back)

})

// self.rsplit_once().map(|(front, back)| {
// self.inner = maybe_prepend_slash(&front);

// })
}
/// Removes and returns the first `Token` in the `Pointer` if it exists.
pub fn pop_front(&mut self) -> Option<Token> {
Expand Down Expand Up @@ -1326,6 +1323,16 @@ mod tests {
ptr, "/foo/bar/~1baz",
"pointer should equal \"/foo/bar/~1baz\" after push_back"
);

let mut ptr = Pointer::new(["foo", "bar"]);
assert_eq!(ptr.pop_back(), Some("bar".into()));
assert_eq!(ptr, "/foo", "pointer should equal \"/foo\" after pop_back");
assert_eq!(
ptr.pop_back(),
Some("foo".into()),
"\"foo\" should have been popped from the back"
);
assert_eq!(ptr, "", "pointer should equal \"\" after pop_back");
}

#[test]
Expand Down Expand Up @@ -1368,8 +1375,8 @@ mod tests {
assert_eq!(ptr.pop_back(), Some("bar".into()));
assert_eq!(ptr, "/foo");
assert_eq!(ptr.count(), 1);

ptr.pop_front();
assert_eq!(ptr.pop_front(), Some("foo".into()));
assert_eq!(ptr, "");
}

#[test]
Expand Down
3 changes: 1 addition & 2 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ impl Token {
///
/// ## Parameters
/// - `len` - current length of the array / vector.
/// - `max_size` - a optional maximum allowed size for the array / vector.
///
/// ## Errors
/// - `IndexError::Parse` - if the token is not a valid index.
/// - `IndexError::OutOfBounds` - if the token is a valid index but exceeds
/// `max_size` or `len`.
/// `len`.
///
/// ## Examples
///```
Expand Down

0 comments on commit f5e4f80

Please sign in to comment.