~matthiasbeyer/serde-select

2362aa34cd7409ba02fc5f42e4e07bd318530275 — Matthias Beyer 4 years ago dc4c5a3
Add logging in resolver

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
1 files changed, 50 insertions(+), 20 deletions(-)

M src/resolver.rs
M src/resolver.rs => src/resolver.rs +50 -20
@@ 11,42 11,72 @@ pub fn resolve<'doc, O>(obj: &'doc O, tokens: &Token, error_if_not_found: bool)
    -> Result<Option<&'doc O>>
    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)),