<commit_before>// See LICENSE file for copyright and license details.

use serialize::{Decodable, json};
use error_context;
use core::misc::read_file;

pub struct Config {
    json: json::Object,
}

fn decode<A: Decodable<json::Decoder, json::DecoderError>>(json_obj: json::Json) -> A {
    let mut decoder = json::Decoder::new(json_obj);
    let decoded: A = Decodable::decode(&mut decoder).unwrap();
    decoded
}

impl Config {
    pub fn new(path: &Path) -> Config {
        set_error_context!("parsing config", path.as_str().unwrap());
        let json = match json::from_str(read_file(path).as_slice()) {
            Ok(json::Object(obj)) => obj,
            Err(msg) => fail!("Config parsing error: {}", msg),
            some_error => fail!("Unknown config parsing error: {}", some_error),
        };
        Config {
            json: *json,
        }
    }

    pub fn get<A: Decodable<json::Decoder, json::DecoderError>>(&self, name: &str) -> A {
        let owned_name_str = name.into_owned();
        decode(match self.json.find(&owned_name_str) {
            Some(val) => val.clone(),
            None => fail!("No field '{}", name),
        })
    }
}

// vim: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab:<commit_msg>core/conf.rs: Updated to latest Rust: ~str -> StrBuf<commit_after>// See LICENSE file for copyright and license details.

use serialize::{Decodable, json};
use error_context;
use core::misc::read_file;

pub struct Config {
    json: json::Object,
}

fn decode<A: Decodable<json::Decoder, json::DecoderError>>(json_obj: json::Json) -> A {
    let mut decoder = json::Decoder::new(json_obj);
    let decoded: A = Decodable::decode(&mut decoder).unwrap();
    decoded
}

impl Config {
    pub fn new(path: &Path) -> Config {
        set_error_context!("parsing config", path.as_str().unwrap());
        let json = match json::from_str(read_file(path).as_slice()) {
            Ok(json::Object(obj)) => obj,
            Err(msg) => fail!("Config parsing error: {}", msg),
            some_error => fail!("Unknown config parsing error: {}", some_error),
        };
        Config {
            json: *json,
        }
    }

    pub fn get<A: Decodable<json::Decoder, json::DecoderError>>(&self, name: &str) -> A {
        let owned_name_str = name.into_strbuf();
        decode(match self.json.find(&owned_name_str) {
            Some(val) => val.clone(),
            None => fail!("No field '{}", name),
        })
    }
}

// vim: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab:
{'fraction_non_alphanumeric': 0.1608419380460683, 'fraction_numerical': 0.0023828435266084196, 'mean_word_length': 2.9544740973312402, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 28, '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': '5127717', 'n_tokens_mistral': 798, 'n_tokens_neox': 800, 'n_words': 189}