node: update pointer method in NodeCollection to match Node

This commit is contained in:
Matt Bilker
2019-01-14 20:36:41 +00:00
parent eb0cfd4045
commit 387fc2ea30
2 changed files with 7 additions and 14 deletions

View File

@@ -130,27 +130,20 @@ impl NodeCollection {
Ok(node)
}
pub fn pointer<'a>(&'a self, pointer: &str) -> Option<&'a NodeCollection> {
if pointer == "" {
pub fn pointer<'a>(&'a self, pointer: &[&str]) -> Option<&'a NodeCollection> {
if pointer.is_empty() {
return Some(self);
}
if !pointer.starts_with('/') {
return None;
}
let tokens = pointer
.split('/')
.skip(1)
.map(|x| x.replace("~1", "/").replace("~0", "~"));
let mut target = self;
for token in tokens {
let target_opt = if let Some(index) = parse_index(&token) {
for token in pointer {
let target_opt = if let Some(index) = parse_index(token) {
eprintln!("index: {}", index);
target.children().get(index)
} else {
eprintln!("token: {:?}", token);
target.children().iter().find(|ref child| {
child.base().key().ok().and_then(|x| x).expect("key not parseable") == token
child.base().key().ok().and_then(|x| x).expect("key not parseable") == *token
})
};

View File

@@ -253,7 +253,7 @@ impl Node {
None
}
pub fn pointer(&self, pointer: &[&str]) -> Option<&Node> {
pub fn pointer<'a>(&'a self, pointer: &[&str]) -> Option<&'a Node> {
if pointer.is_empty() {
return Some(self);
}
@@ -265,7 +265,7 @@ impl Node {
None => return None,
};
let target_opt = if let Some(index) = parse_index(&token) {
let target_opt = if let Some(index) = parse_index(token) {
eprintln!("index: {}", index);
children.get(index)
} else {