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
//! # The base library components
//!
//!
use std::fmt;

/// Base Error for Crate
#[derive(Debug, Clone)]
pub struct RomadError {
    msg: String,
}

impl RomadError {
    pub fn new(msg: String) -> RomadError {
        RomadError { msg: msg }
    }
}

impl fmt::Display for RomadError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "RomadError: {}", self.msg)
    }
}

/// Connection object
/// Default values http://localhost:4646/v1
pub struct Connection<'a> {
    pub address: &'a str,
    pub port: &'a str,
    pub token: Option<&'a str>, // TODO: Token authentication
    pub timeout: isize,
    pub version: &'a str,
}

impl Default for Connection<'_> {
    /// Default implementation for a localhost nomad instance
    /// ```
    /// use romad::base::Connection;
    ///
    /// let con: Connection = Default::default();
    /// ```
    fn default() -> Self {
        Connection {
            address: "http://localhost",
            port: "4646",
            token: None,
            timeout: 0,
            version: "1",
        }
    }
}

impl Connection<'_> {
    /// Build the base url for the connection
    /// ```
    /// use romad::base::Connection;
    ///
    /// let con: Connection = Default::default();
    /// let url = con.build_base_url();
    /// assert_eq!("http://localhost:4646/v1".to_string(), url);
    /// ```
    pub fn build_base_url(&self) -> String {
        format!("{}:{}/v{}", self.address, self.port, self.version)
    }
}

#[cfg(test)]
mod test {

    use super::*;

    #[test]
    fn test_build_base_url() {
        let con: Connection = Default::default();

        let expected_url = "http://localhost:4646/v1".to_string();

        assert_eq!(expected_url, con.build_base_url());
    }
}