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
use std::collections::BTreeMap as Map;
use std::io;
use std::path::Path;

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("⛔ ️parsing environments.toml: {0}")]
    ParsingToml(io::Error),
    #[error("⛔ ️no settings for current LOAM_ENV ({0:?}) found in environments.toml")]
    NoSettingsForCurrentEnv(String),
}

type Environments = Map<Box<str>, Environment>;

#[derive(Debug, serde::Deserialize, Clone)]
pub struct Environment {
    pub accounts: Option<Vec<Account>>,
    pub network: Network,
    pub contracts: Option<Map<Box<str>, Contract>>,
}

#[derive(Debug, serde::Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct Network {
    pub name: Option<String>,
    pub rpc_url: Option<String>,
    pub network_passphrase: Option<String>,
    // run_locally: Option<bool>,
}

#[derive(Debug, serde::Deserialize, Clone)]
pub struct Account {
    pub name: String,
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub default: bool,
}

#[derive(Debug, serde::Deserialize, Clone)]
pub struct Contract {
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub workspace: bool,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub init: Option<String>,
}

impl Environment {
    pub fn get(workspace_root: &Path, loam_env: &str) -> Result<Option<Environment>, Error> {
        let env_toml = workspace_root.join("environments.toml");

        if !env_toml.exists() {
            return Ok(None);
        }

        let toml_str = std::fs::read_to_string(env_toml).map_err(Error::ParsingToml)?;
        let mut parsed_toml: Environments = toml::from_str(&toml_str).unwrap();
        let current_env = parsed_toml.remove(loam_env);
        if current_env.is_none() {
            return Err(Error::NoSettingsForCurrentEnv(loam_env.to_string()));
        };
        Ok(current_env)
    }
}