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
use std::fmt::{Display, Error, Formatter};

use anyhow::{Context, Result};
use serde_derive::Deserialize;
use tokio::fs::read_to_string;

use crate::constants::{DEFAULT_GAS, DEFAULT_GAS_PRICE};

#[derive(Deserialize)]
pub struct Deploy {
    pub url: String,
    pub private: String,
    pub address: String,
    pub gas: Option<usize>,
    pub gas_price: Option<usize>,
}

impl Display for Deploy {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        writeln!(f, "url       : {}", self.url)?;
        writeln!(f, "private   : {}", self.private)?;
        writeln!(f, "address   : {}", self.address)?;
        writeln!(f, "gas       : {}", self.gas.unwrap_or(DEFAULT_GAS))?;
        writeln!(
            f,
            "gas price : {}",
            self.gas_price.unwrap_or(DEFAULT_GAS_PRICE)
        )?;
        Ok(())
    }
}

#[derive(Deserialize)]
pub struct Package {
    pub name: String,
}

#[derive(Deserialize)]
pub struct CargoToml {
    pub package: Package,
}

#[derive(Deserialize)]
pub struct DeployToml {
    pub deploy: Deploy,
}

pub async fn get_deploy_config() -> Result<Deploy> {
    let config_contents = read_to_string("sewup.toml")
        .await
        .context("can not read sewup.toml")?;
    let config: DeployToml = toml::from_str(config_contents.as_str())?;

    Ok(config.deploy)
}