pub fn parse_first_kp_value_segment(keypath: &str) -> Option<(Segment, String)> {
        let mut unescaped = String::with_capacity(50);
        // The length of the escaped sequence. It always starts with a dot '.'
        let mut len_bytes = 1;
        let mut chars = keypath.chars();

        // first char must be a . or a $ or we've exceeded the keypath
        match chars.next() {
            Some('.') => {
                loop {
                    match chars.next() {
                        Some(backslash @ '\\') => {
                            len_bytes += backslash.len_utf8();
                            if let Some(c) = chars.next() {
                                len_bytes += c.len_utf8();
                                unescaped.push(c);
                            } else {
                                panic!("Escape char found as last char in keypath");
                            }
                        }
                        Some('.') | Some('$') => {
                            break;
                        }
                        Some(c) => {
                            len_bytes += c.len_utf8();
                            unescaped.push(c);
                        }
                        None => {
                            break;
                        }
                    }
                }
                Some((
                    Segment::ObjectKey(unescaped),
                    keypath[..len_bytes].to_string(),
                ))
            }
            Some('$') => {
                let mut i = String::new();
                for c in chars {
                    if ('0'..='9').contains(&c) {
                        i.push(c);
                    } else {
                        break;
                    }
                }
                Some((
                    Segment::Array(i.parse().unwrap()),
                    keypath[..1 + i.len()].to_string(),
                ))
            }
            Some(_) => None, // we must be past the keypath portion of string. done.
            None => None,
        }
    }
// returns the unescaped segment as Segment and the escaped segment as a String
returns the unescaped segment as Segment and the escaped segment as a String
returns the unescaped segment as Segment and the escaped segment as a String
pub fn parse_first_kp_value_segment(keypath: &str) -> Option<(Segment, String)> {
        let mut unescaped = String::with_capacity(50);
        // The length of the escaped sequence. It always starts with a dot '.'
        let mut len_bytes = 1;
        let mut chars = keypath.chars();
        // first char must be a . or a $ or we've exceeded the keypath
        match chars.next() {
            Some('.') => {
                loop {
                    match chars.next() {
                        Some(backslash @ '\\') => {
                            len_bytes += backslash.len_utf8();
                            if let Some(c) = chars.next() {
                                len_bytes += c.len_utf8();
                                unescaped.push(c);
                            } else {
                                panic!("Escape char found as last char in keypath");
                            }
                        }
                        Some('.') | Some('$') => {
                            break;
                        }
                        Some(c) => {
                            len_bytes += c.len_utf8();
                            unescaped.push(c);
                        }
                        None => {
                            break;
                        }
                    }
                }
                Some((
                    Segment::ObjectKey(unescaped),
                    keypath[..len_bytes].to_string(),
                ))
            }
            Some('$') => {
                let mut i = String::new();
                for c in chars {
                    if ('0'..='9').contains(&c) {
                        i.push(c);
                    } else {
                        break;
                    }
                }
                Some((
                    Segment::Array(i.parse().unwrap()),
                    keypath[..1 + i.len()].to_string(),
                ))
            }
            Some(_) => None, // we must be past the keypath portion of string. done.
            None => None,
        }
    }
pub fn parse_first_kp_value_segment ( keypath : & str ) -> Option < ( Segment , String ) > { let mut unescaped = String :: with_capacity ( 50 ) ; // The length of the escaped sequence. It always starts with a dot '.' let mut len_bytes = 1 ; let mut chars = keypath . chars ( ) ; // first char must be a . or a $ or we've exceeded the keypath match chars . next ( ) { Some ( '.' ) => { loop { match chars . next ( ) { Some ( backslash @ '\\' ) => { len_bytes += backslash . len_utf8 ( ) ; if let Some ( c ) = chars . next ( ) { len_bytes += c . len_utf8 ( ) ; unescaped . push ( c ) ; } else { panic ! ( " " ) ; } } Some ( '.' ) | Some ( '$' ) => { break ; } Some ( c ) => { len_bytes += c . len_utf8 ( ) ; unescaped . push ( c ) ; } None => { break ; } } } Some ( ( Segment :: ObjectKey ( unescaped ) , keypath [ .. len_bytes ] . to_string ( ) , ) ) } Some ( '$' ) => { let mut i = String :: new ( ) ; for c in chars { if ( '0' ..= '9' ) . contains ( & c ) { i . push ( c ) ; } else { break ; } } Some ( ( Segment :: Array ( i . parse ( ) . unwrap ( ) ) , keypath [ .. 1 + i . len ( ) ] . to_string ( ) , ) ) } Some ( _ ) => None , // we must be past the keypath portion of string. done. None => None , } }
returns the unescaped segment as Segment and the escaped segment as a String
returns the unescaped segment as Segment and the escaped segment as a String
keypath
&str
keypath
&str