Skip to content
Snippets Groups Projects
Commit 1a50febf authored by Nathan Ringo's avatar Nathan Ringo
Browse files

Adds push and pop.

parent 5b2a6d6c
No related branches found
No related tags found
No related merge requests found
......@@ -96,6 +96,18 @@ impl<S: AsRef<str>, C: AsRef<[S]>> JsonPointer<S, C> {
}
}
impl<S: AsRef<str>> JsonPointer<S, Vec<S>> {
/// Adds a component to the JSON pointer.
pub fn push(&mut self, component: S) {
self.ref_toks.push(component);
}
/// Removes and returns the last component from the JSON pointer.
pub fn pop(&mut self) -> Option<S> {
self.ref_toks.pop()
}
}
impl<S: AsRef<str>, C: AsRef<[S]>> Display for JsonPointer<S, C> {
fn fmt(&self, fmt: &mut Formatter) -> FmtResult {
for part in self.ref_toks.as_ref().iter() {
......
extern crate json_pointer;
#[macro_use]
extern crate quickcheck;
use json_pointer::JsonPointer;
use quickcheck::TestResult;
quickcheck! {
/// Pushing then popping should not affect the pointer or the pushed/popped
/// value.
fn push_then_pop_is_identity(s: String, t: String) -> TestResult {
match s.parse::<JsonPointer<_, _>>() {
Ok(mut ptr) => {
let str1 = ptr.to_string();
ptr.push(t.clone());
let t2 = ptr.pop();
let str2 = ptr.to_string();
if Some(t) == t2 && str1 == str2 {
TestResult::passed()
} else {
TestResult::failed()
}
},
Err(_) => TestResult::discard(),
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment