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
64
65
66
67
68
69
70
//! libracerd provides an http server and set of completion engines for consumption by rust
//! developer tools. The http server itself is a few JSON endpoints providing completion, definition
//! look-up, and compilation. The endpoints are backed by an object implementing `SemanticEngine`
//!
//! Documentation for the HTTP endpoints can be found in the http module header.
//!
//! This project's source code is [available on GitHub](https://github.com/jwilm/racerd).

#![deny(warnings)]

extern crate rustc_serialize;

#[macro_use]
extern crate router;     // Iron routing handler
extern crate bodyparser; // Iron body parsing middleware
extern crate persistent; // Iron storage middleware
extern crate logger;     // Iron logging middleware
extern crate iron;       // http framework
extern crate iron_hmac;

extern crate hyper;      // Provides `Listening` type returned by Iron

#[macro_use]
extern crate log;        // log macros
extern crate racer;      // rust code analysis

extern crate rand;

pub mod util;
pub mod http;
pub mod engine;

use std::io::Read;
use std::fs::File;

/// Configuration flags and values
///
/// This object contains all switches the consumer has control of.
#[derive(Debug, Default)]
pub struct Config {
    pub port: u16,
    pub secret_file: Option<String>,
    pub print_http_logs: bool,
    pub rust_src_path: Option<String>
}

impl Config {
    /// Build a default config object
    pub fn new() -> Config {
        Default::default()
    }

    /// Return contents of secret file
    ///
    /// panics if self.secret_file is None or an error is encountered while reading the file.
    pub fn read_secret_file(&self) -> String {
        self.secret_file.as_ref().map(|secret_file_path| {
            let buf = {
                let mut f = File::open(secret_file_path).unwrap();
                let mut buf = String::new();
                f.read_to_string(&mut buf).unwrap();
                buf
            };

            ::std::fs::remove_file(secret_file_path).unwrap();

            buf
        }).unwrap()
    }
}