Skip to content

Instantly share code, notes, and snippets.

@lilyball
Forked from daboross/config.rs
Last active August 29, 2015 14:01
Show Gist options
  • Save lilyball/e987a98e095a13f8e086 to your computer and use it in GitHub Desktop.
Save lilyball/e987a98e095a13f8e086 to your computer and use it in GitHub Desktop.
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())
}
}
@lilyball
Copy link
Author

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 an enum with error codes and return that instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment