cargo_sewup/
config.rs

1use std::fmt::{Display, Error, Formatter};
2
3use anyhow::{Context, Result};
4use serde_derive::Deserialize;
5use tokio::fs::read_to_string;
6
7use crate::constants::{DEFAULT_GAS, DEFAULT_GAS_PRICE};
8
9#[derive(Deserialize)]
10pub struct Deploy {
11    pub url: String,
12    pub private: String,
13    pub address: String,
14    pub gas: Option<usize>,
15    pub gas_price: Option<usize>,
16}
17
18impl Display for Deploy {
19    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
20        writeln!(f, "url       : {}", self.url)?;
21        writeln!(f, "private   : {}", self.private)?;
22        writeln!(f, "address   : {}", self.address)?;
23        writeln!(f, "gas       : {}", self.gas.unwrap_or(DEFAULT_GAS))?;
24        writeln!(
25            f,
26            "gas price : {}",
27            self.gas_price.unwrap_or(DEFAULT_GAS_PRICE)
28        )?;
29        Ok(())
30    }
31}
32
33#[derive(Deserialize)]
34pub struct Package {
35    pub name: String,
36    pub version: String,
37}
38
39#[derive(Deserialize)]
40pub struct Features {
41    pub constructor: Option<Vec<String>>,
42    #[serde(rename(deserialize = "constructor-test"))]
43    pub constructor_test: Option<Vec<String>>,
44}
45
46#[derive(Deserialize)]
47pub struct Release {
48    pub incremental: Option<bool>,
49    pub panic: Option<String>,
50    pub lto: Option<bool>,
51    #[serde(rename(deserialize = "opt-level"))]
52    pub opt_level: Option<String>,
53}
54
55#[derive(Deserialize)]
56pub struct Profile {
57    pub release: Option<Release>,
58}
59
60#[derive(Deserialize)]
61pub struct CargoToml {
62    pub package: Package,
63    pub features: Option<Features>,
64    pub profile: Option<Profile>,
65}
66
67#[derive(Deserialize)]
68pub struct CargoLock {
69    pub package: Vec<Package>,
70}
71
72#[derive(Deserialize)]
73pub struct DeployToml {
74    pub deploy: Deploy,
75}
76
77pub async fn get_deploy_config() -> Result<Deploy> {
78    let config_contents = read_to_string("sewup.toml")
79        .await
80        .context("can not read sewup.toml")?;
81    let config: DeployToml = toml::from_str(config_contents.as_str())?;
82
83    Ok(config.deploy)
84}