Skip to content

Commit

Permalink
Merge pull request #1127 from rainlanguage/2025-01-07-yaml-structs-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
hardyjosh authored Jan 7, 2025
2 parents 795b287 + 1baf280 commit 77fd363
Show file tree
Hide file tree
Showing 13 changed files with 1,534 additions and 650 deletions.
164 changes: 96 additions & 68 deletions crates/settings/src/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,44 +100,53 @@ impl DeployerConfigSource {

impl YamlParsableHash for Deployer {
fn parse_all_from_yaml(
document: Arc<RwLock<StrictYaml>>,
documents: Vec<Arc<RwLock<StrictYaml>>>,
) -> Result<HashMap<String, Self>, YamlError> {
let document_read = document.read().map_err(|_| YamlError::ReadLockError)?;
let deployers_hash = require_hash(
&document_read,
Some("deployers"),
Some("missing field: deployers".to_string()),
)?;

deployers_hash
.iter()
.map(|(key_yaml, deployer_yaml)| {
let deployer_key = key_yaml.as_str().unwrap_or_default().to_string();

let address = Deployer::validate_address(&require_string(
deployer_yaml,
Some("address"),
Some(format!(
"address string missing in deployer: {deployer_key}"
)),
)?)?;

let network_name = match optional_string(deployer_yaml, "network") {
Some(network_name) => network_name,
None => deployer_key.clone(),
};
let network = Network::parse_from_yaml(document.clone(), &network_name)?;

let deployer = Deployer {
document: document.clone(),
key: deployer_key.clone(),
address,
network: Arc::new(network),
};

Ok((deployer_key, deployer))
})
.collect()
let mut deployers = HashMap::new();

for document in &documents {
let document_read = document.read().map_err(|_| YamlError::ReadLockError)?;

if let Ok(deployers_hash) = require_hash(&document_read, Some("deployers"), None) {
for (key_yaml, deployer_yaml) in deployers_hash {
let deployer_key = key_yaml.as_str().unwrap_or_default().to_string();

let address = Deployer::validate_address(&require_string(
deployer_yaml,
Some("address"),
Some(format!(
"address string missing in deployer: {deployer_key}"
)),
)?)?;

let network_name = match optional_string(deployer_yaml, "network") {
Some(network_name) => network_name,
None => deployer_key.clone(),
};
let network = Network::parse_from_yaml(documents.clone(), &network_name)?;

let deployer = Deployer {
document: document.clone(),
key: deployer_key.clone(),
address,
network: Arc::new(network),
};

if deployers.contains_key(&deployer_key) {
return Err(YamlError::KeyShadowing(deployer_key));
}
deployers.insert(deployer_key, deployer);
}
}
}

if deployers.is_empty() {
return Err(YamlError::ParseError(
"missing field: deployers".to_string(),
));
}

Ok(deployers)
}
}

Expand Down Expand Up @@ -208,47 +217,66 @@ mod tests {
}

#[test]
fn test_parse_deployers_from_yaml() {
let yaml = r#"
test: test
fn test_parse_deployers_from_yaml_multiple_files() {
let yaml_one = r#"
networks:
TestNetwork:
rpc: https://rpc.com
chain-id: 1
deployers:
DeployerOne:
address: 0x1234567890123456789012345678901234567890
network: TestNetwork
"#;
let error = Deployer::parse_all_from_yaml(get_document(yaml)).unwrap_err();
assert_eq!(
error,
YamlError::ParseError("missing field: deployers".to_string())
);

let yaml = r#"
let yaml_two = r#"
deployers:
TestDeployer:
DeployerTwo:
address: 0x0987654321098765432109876543210987654321
network: TestNetwork
"#;
let error = Deployer::parse_all_from_yaml(get_document(yaml)).unwrap_err();

let documents = vec![get_document(yaml_one), get_document(yaml_two)];
let deployers = Deployer::parse_all_from_yaml(documents).unwrap();

assert_eq!(deployers.len(), 2);
assert!(deployers.contains_key("DeployerOne"));
assert!(deployers.contains_key("DeployerTwo"));

assert_eq!(
error,
YamlError::ParseError("address string missing in deployer: TestDeployer".to_string())
deployers.get("DeployerOne").unwrap().address.to_string(),
"0x1234567890123456789012345678901234567890"
);
assert_eq!(
deployers.get("DeployerTwo").unwrap().address.to_string(),
"0x0987654321098765432109876543210987654321"
);
}

let yaml = r#"
deployers:
TestDeployer:
address: not_a_valid_address
"#;
let error = Deployer::parse_all_from_yaml(get_document(yaml));
assert!(error.is_err());

let error = Deployer::parse_all_from_yaml(get_document(
r#"
#[test]
fn test_parse_deployers_from_yaml_duplicate_key() {
let yaml_one = r#"
networks:
TestNetwork:
rpc: https://rpc.com
chain-id: 1
deployers:
TestDeployer:
address: "0x1234567890123456789012345678901234567890"
network: SomeNetwork
"#,
))
.unwrap_err();
assert_eq!(error, YamlError::KeyNotFound("SomeNetwork".to_string()));
DuplicateDeployer:
address: 0x1234567890123456789012345678901234567890
network: TestNetwork
"#;
let yaml_two = r#"
deployers:
DuplicateDeployer:
address: 0x0987654321098765432109876543210987654321
network: TestNetwork
"#;

let documents = vec![get_document(yaml_one), get_document(yaml_two)];
let error = Deployer::parse_all_from_yaml(documents).unwrap_err();

assert_eq!(
error,
YamlError::KeyShadowing("DuplicateDeployer".to_string())
);
}
}
Loading

0 comments on commit 77fd363

Please sign in to comment.