From 2362aa34cd7409ba02fc5f42e4e07bd318530275 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 9 May 2020 10:32:15 +0200 Subject: Add logging in resolver Signed-off-by: Matthias Beyer --- src/resolver.rs | 70 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/src/resolver.rs b/src/resolver.rs index ea7fbd3..acbaeb6 100644 --- a/src/resolver.rs +++ b/src/resolver.rs @@ -11,42 +11,72 @@ pub fn resolve<'doc, O>(obj: &'doc O, tokens: &Token, error_if_not_found: bool) -> Result> where O: Object<'doc> { + trace!("Resolving Token: {:?}", tokens); match obj.get_type() { ObjectType::Map => { + trace!("Found object type Map"); match tokens { - Token::Identifier { ref ident, .. } => match obj.at_key(ident)? { - None => if error_if_not_found { - Err(Error::IdentifierNotFoundInDocument(ident.to_owned())) - } else { - Ok(None) + Token::Identifier { ref ident, .. } => { + trace!("Checking object at ident {}", ident); + match obj.at_key(ident)? { + None => if error_if_not_found { + trace!("Not found, erroring"); + Err(Error::IdentifierNotFoundInDocument(ident.to_owned())) + } else { + trace!("Not found, not erroring"); + Ok(None) + } + Some(sub_document) => { + trace!("Found sub-document, getting next token"); + match tokens.next() { + Some(next) => resolve(sub_document, next, error_if_not_found), + None => { + trace!("No next token, returning"); + Ok(Some(sub_document)) + }, + } + }, } - Some(sub_document) => match tokens.next() { - Some(next) => resolve(sub_document, next, error_if_not_found), - None => Ok(Some(sub_document)), - }, }, - Token::Index { idx, .. } => Err(Error::NoIndexInTable(*idx)), + Token::Index { idx, .. } => { + trace!("Token is index, but cannot index at document, erroring..."); + Err(Error::NoIndexInTable(*idx)) + }, } }, ObjectType::Array => { + trace!("Found object type Array"); match tokens { - Token::Index { idx, .. } => match tokens.next() { - Some(next) => { - if let Some(subobj) = obj.at_index(*idx)? { - resolve(subobj, next, error_if_not_found) - } else { - Ok(None) - } - }, - None => Ok(Some(obj)), + Token::Index { idx, .. } => { + trace!("Checking object at index {}", idx); + match tokens.next() { + Some(next) => { + trace!("There is another token..."); + if let Some(subobj) = obj.at_index(*idx)? { + trace!("And there's a subobject, resolving..."); + resolve(subobj, next, error_if_not_found) + } else { + trace!("But no subobject at this index. Returning"); + Ok(None) + } + }, + None => { + trace!("No more tokens, returning array at index {}", idx); + obj.at_index(*idx) + }, + } + }, + Token::Identifier { ref ident, .. } => { + trace!("Token is identifier, but cannot get identifier when having array, erroring..."); + Err(Error::NoIdentifierInArray(ident.clone())) }, - Token::Identifier { ref ident, .. } => Err(Error::NoIdentifierInArray(ident.clone())), } }, ObjectType::Atom => { + trace!("Object is an atom, cannot resolve further"); match tokens { Token::Identifier { ref ident, .. } => Err(Error::QueryingValueAsTable(ident.clone())), Token::Index { idx, .. } => Err(Error::QueryingValueAsArray(*idx)), -- cgit v1.2.3