-
-
Save lilyball/e987a98e095a13f8e086 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate serialize; | |
extern crate std; | |
extern crate collections; | |
use self::serialize::json; | |
use self::serialize::json::Json; | |
use self::std::io::File; | |
use self::collections::TreeMap; | |
pub fn load_config_map() -> Result<TreeMap<~str, Json>,~str> { | |
let config: Json = match json::from_reader(&mut File::open(&Path::new("config.json"))) { | |
Ok(v) => v, | |
Err(e) => return Err(format!("Failed to decode config: {}", e)) | |
}; | |
match config { | |
json::Object(obj) => Ok(*obj), | |
_ => Err("Config isn't an object!".to_owned()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note, the
~str
error type is kind of unfortunate because it's not machine-parsable, although you could present it to the user. A better approach is to define anenum
with error codes and return that instead.