<commit_before>use rustc_serialize::json::{Json,Array};

pub fn path<'a>(json: &'a Json, path: &str) -> &'a Json {
    let parts = path.split(".");
    let mut cur = json;
    for p in parts {
        cur = cur.as_object().unwrap().get(p).unwrap();
    }
    cur
}

pub fn array<'a>(json: &'a Json, path: &str) -> &'a Array {
    self::path(json, path).as_array().unwrap()
}

pub fn string<'a>(json: &'a Json, path: &str) -> &'a str{
    self::path(json, path).as_string().unwrap()
}
<commit_msg>matrix: json: provide more useful errors with bad paths<commit_after>use rustc_serialize::json::{Json,Array};

pub fn path<'a>(json: &'a Json, path: &str) -> &'a Json {
    let parts = path.split(".");
    let mut cur = json;
    for p in parts {
        cur = match cur.as_object().unwrap().get(p) {
            Some(c) => c,
            None => panic!("Could not find {} in {:?} (lost at {})", path, json, p)
        }
    }
    cur
}

pub fn array<'a>(json: &'a Json, path: &str) -> &'a Array {
    match self::path(json, path).as_array() {
        Some(p) => p,
        None => panic!("{} in {:?} is not an array", path, json)
    }
}

pub fn string<'a>(json: &'a Json, path: &str) -> &'a str{
    match self::path(json, path).as_string() {
        Some(p) => p,
        None => panic!("{} in {:?} is not an array", path, json)
    }
}

{'fraction_non_alphanumeric': 0.2118263473053892, 'fraction_numerical': 0.0, 'mean_word_length': 2.75561797752809, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4581971', 'n_tokens_mistral': 488, 'n_tokens_neox': 500, 'n_words': 139}