config.rs 707 B

12345678910111213141516171819202122232425262728293031
  1. use rustc_serialize::json;
  2. use std::fs::File;
  3. use std::io::prelude::*;
  4. use std::io::Error;
  5. #[derive(RustcDecodable, Debug)]
  6. pub struct Config {
  7. pub port: Option<u16>,
  8. pub email: String,
  9. pub token: String,
  10. pub whitelist: Vec<String>
  11. }
  12. fn stringify(err: Error) -> String {
  13. format!("{}", err)
  14. }
  15. pub fn load(path: &str) -> Result<Config, String> {
  16. File::open(path)
  17. .map_err(stringify)
  18. .and_then(|mut f| {
  19. let mut text = String::new();
  20. f.read_to_string(&mut text)
  21. .map_err(stringify)
  22. .map(|_| text)
  23. })
  24. .and_then(|text| {
  25. json::decode(&text).map_err(|e| format!("{}", e))
  26. })
  27. }