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
use crate::{error::DeployError, file::ContractInfo};
use lazy_static::lazy_static;
use serde::{de::DeserializeOwned, Serialize};
use serde_json::Value;
lazy_static! {
pub static ref BIN_NAME: String = std::env::current_exe()
.unwrap()
.file_stem()
.unwrap()
.to_owned()
.into_string()
.unwrap();
}
pub fn replace_strings(value: &mut Value, contracts: &Vec<ContractInfo>) -> anyhow::Result<()> {
match value {
Value::String(string) => {
if let Some((_, new)) = string.split_once('&') {
if let Some(contract) = contracts.iter().find(|x| x.name == new) {
match &contract.addr {
Some(addr) => *string = addr.clone(),
None => {
return Err(DeployError::AddrNotFound {
name: contract.name.clone(),
}
.into())
}
}
}
}
}
Value::Array(array) => {
for value in array {
replace_strings(value, contracts)?;
}
}
Value::Object(map) => {
for (_, value) in map {
replace_strings(value, contracts)?;
}
}
_ => {}
}
Ok(())
}
pub fn replace_strings_any<T: Serialize + DeserializeOwned + Clone>(
object: &mut T,
contracts: &Vec<ContractInfo>,
) -> anyhow::Result<()> {
let mut value = serde_json::to_value(object.clone())?;
replace_strings(&mut value, contracts)?;
*object = serde_json::from_value(value)?;
Ok(())
}