1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::io;
use std::env;
use std::io::prelude::*;
use std::fs::File;
use rustc_serialize::json;
#[derive(Debug)]
pub enum ConfigError {
    Io(io::Error),
    Json(json::DecoderError),
    Path(String),
}
map_to_error!(ConfigError, io::Error, ConfigError::Io);
map_to_error!(ConfigError, json::DecoderError, ConfigError::Json);
#[derive(RustcDecodable, RustcEncodable)]
pub struct Config {
    pub username: String,
    pub api_server: String,
    pub http_proxy: String,
    pub https_proxy: String,
    pub api_key: String,
    pub api_version: String,
}
pub fn read_config() -> Result<Config, ConfigError> {
    let mut path = try!(env::home_dir().ok_or(ConfigError::Path("cant get homedir".to_string())));
    path.push(".config/passivetotal/api_config.json");
    let mut file = try!(File::open(path));
    let mut contents = String::new();
    try!(file.read_to_string(&mut contents));
    Ok(try!(json::decode(contents.as_str())))
}