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
//! Square environment in which to operate

const PRODUCTION_URL: &str = "https://connect.squareup.com";
const SANDBOX_URL: &str = "https://connect.squareupsandbox.com";

/// Identifies the Square environment in which this app instance is operating
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Environment {
    Production,
    Sandbox,
    Custom(String),
}

impl Environment {
    /// Gets the base Square API URL for this environment
    pub fn get_base_url(&self) -> String {
        match self {
            Environment::Production => String::from(PRODUCTION_URL),
            Environment::Sandbox => String::from(SANDBOX_URL),
            Environment::Custom(custom_url) => custom_url.clone(),
        }
    }
}

impl Default for Environment {
    /// Sandbox is the default environment
    fn default() -> Self {
        Self::Sandbox
    }
}

#[cfg(test)]
mod tests {
    use crate::config::Environment;

    #[test]
    fn get_base_url_default() {
        let environment = Environment::default();
        assert_eq!(Environment::Sandbox, environment);
        assert_eq!(
            String::from("https://connect.squareupsandbox.com"),
            environment.get_base_url()
        )
    }

    #[test]
    fn get_base_url_custom() {
        assert_eq!(
            String::from("some_custom_url"),
            Environment::Custom(String::from("some_custom_url")).get_base_url()
        );
    }

    #[test]
    fn get_base_url_production() {
        assert_eq!(
            String::from("https://connect.squareup.com"),
            Environment::Production.get_base_url()
        )
    }

    #[test]
    fn get_base_url_sandbox() {
        assert_eq!(
            String::from("https://connect.squareupsandbox.com"),
            Environment::Sandbox.get_base_url()
        )
    }
}