net_utils/net/
config.rs

1//! Configuration for connection object
2//! #![unstable]
3use std::default::Default;
4use std::fs::File;
5use std::io::Read;
6use std::time::Duration;
7extern crate serde;
8extern crate serde_json;
9
10///Configuration data.
11#[derive(Serialize, Deserialize, Debug, Clone)]
12pub struct Config {
13    /// The server to connect to.
14    pub server: String,
15    /// The port to connect to.
16    pub port: u16,
17    /// Connect timeout.
18    pub connect_timeout: Option<u64>,
19    /// Read timeout.
20    pub read_timeout: Option<u64>,
21    /// Write timeout.
22    pub write_timeout: Option<u64>,
23    ///If true, it will assume ssl is enabled
24    pub use_ssl: Option<bool>,
25    /// SSL Protocol
26    //pub ssl_protocol : Option<>,
27    /// Certificate File
28    pub certificate_file: Option<String>,
29    /// Private Key File
30    pub private_key_file: Option<String>,
31    /// CA File
32    pub ca_file: Option<String>,
33    /// Verify certificate
34    pub verify: Option<bool>,
35    /// Verify depth
36    pub verify_depth: Option<u32>,
37}
38
39impl Default for Config {
40    fn default() -> Config {
41        Config {
42            server: "localhost".to_string(),
43            port: 21950,
44            connect_timeout: Some(60_000),
45            read_timeout: Some(60_000),
46            write_timeout: Some(60_000),
47            use_ssl: Some(false),
48            // ssl_protocol:
49            certificate_file: None,
50            private_key_file: None,
51            ca_file: None,
52            verify: None,
53            verify_depth: None,
54        }
55    }
56}
57impl Config {
58    fn from_file(file: &str) -> Config {
59        let mut f = File::open(file).unwrap();
60        let mut buffer = String::new();
61        f.read_to_string(&mut buffer).unwrap();
62        //debug!("Config file: {}", buffer.as_str());
63        serde_json::from_str(&buffer).unwrap()
64
65    }
66}
67
68#[cfg(test)]
69pub mod test {
70    use std::default::Default;
71    use std::time::Duration;
72    #[test]
73    fn test_config() {
74        let c = super::Config {
75            server: "localhost".to_string(),
76            port: 2195,
77            ..Default::default()
78        };
79        assert_eq!(c.port, 2195);
80        assert_eq!(c.read_timeout, Some(60_000));
81    }
82
83}