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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! Daemon configuration structures and parsing.
#![feature(try_trait)]

#[macro_use]
extern crate failure;
#[macro_use]
extern crate serde_derive;
extern crate hashbrown;
extern crate serde;
extern crate toml;

#[macro_use]
extern crate err_convert_macro;
extern crate cxmr_api;
extern crate cxmr_exchanges;
#[cfg(feature = "chat")]
extern crate cxmr_telegram;

mod parse;

#[cfg(feature = "chat")]
mod telegram;

#[cfg(feature = "chat")]
pub use self::telegram::*;

use std::fs::read_to_string;
use std::net::SocketAddr;
use std::path::Path;
use std::str::FromStr;

use hashbrown::HashMap;

use cxmr_api::Account;
use cxmr_exchanges::Exchange;

use self::parse::ParsedConfig;

/// Crypto market daemon config error type.
#[derive(Debug, Fail)]
pub enum Error {
    /// Standard IO error.
    #[fail(display = "std io error: {:?}", _0)]
    Io(#[cause] std::io::Error),

    /// TOML Deserialization error.
    #[fail(display = "toml de error: {:?}", _0)]
    TomlDe(#[cause] ::toml::de::Error),
}

err_converter!(Io, std::io::Error);
err_converter!(TomlDe, ::toml::de::Error);

/// Crypto-bank configuration.
#[derive(Clone)]
pub struct Config {
    pub exchanges: Vec<Exchange>,
    pub accounts_map: HashMap<String, Account>,
    pub events_logging: bool,
    pub server: ServerConfig,
    pub tectonic: TectonicConfig,
    #[cfg(feature = "chat")]
    pub telegram: Option<TelegramConfig>,
    #[cfg(feature = "pubsub")]
    pub zmq: Option<pubsub::ZmqConfig>,
}

impl Config {
    /// Opens configuration file.
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
        Ok(read_to_string(path)?.parse()?)
    }

    /// Returns config account.
    pub fn accounts(&self) -> Vec<Account> {
        self.accounts_map
            .iter()
            .map(|(_, account)| account.clone())
            .collect()
    }
}

impl FromStr for Config {
    type Err = toml::de::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(::toml::from_str::<ParsedConfig>(s)?.into())
    }
}

#[cfg(feature = "pubsub")]
pub mod pubsub {
    /// ZMQ configuration.
    #[derive(Clone, Deserialize, Serialize)]
    pub struct ZmqConfig {
        pub enable: bool,
        pub address: String,
    }
}

/// Tectonic configuration.
#[derive(Clone, Deserialize, Serialize)]
pub struct TectonicConfig {
    pub send: bool,
    pub addr: SocketAddr,
    pub threads: usize,
}

/// Server configuration.
#[derive(Clone, Deserialize, Serialize)]
pub struct ServerConfig {
    pub listen: SocketAddr,
}

#[cfg(test)]
mod test {
    use std::net::SocketAddr;
    use std::str::FromStr;
    #[cfg(feature = "chat")]
    use std::time::Duration;

    use super::*;

    #[test]
    fn from_str() {
        let cfg = Config::from_str(include_str!("../../../../config.example.toml")).unwrap();
        let tectonic = cfg.tectonic;
        assert_eq!(tectonic.send, false);
        assert_eq!(
            tectonic.addr,
            SocketAddr::from_str("127.0.0.1:9001").unwrap()
        );
        assert_eq!(tectonic.threads, 4);

        #[cfg(feature = "chat")]
        {
            let telegram = cfg.telegram.unwrap();
            assert_eq!(telegram.balances.start(), Duration::from_secs(60));
            assert_eq!(telegram.balances.interval(), Duration::from_secs(60 * 60));
        }
    }
}