Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding handlebar support for yaml structs #1136

Merged
merged 10 commits into from
Jan 13, 2025
10 changes: 6 additions & 4 deletions crates/settings/src/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use strict_yaml_rust::StrictYaml;
use thiserror::Error;
use typeshare::typeshare;
use yaml::{
default_document, optional_string, require_hash, require_string, YamlError, YamlParsableHash,
context::Context, default_document, optional_string, require_hash, require_string, YamlError,
YamlParsableHash,
};

#[cfg(target_family = "wasm")]
Expand Down Expand Up @@ -101,6 +102,7 @@ impl DeployerConfigSource {
impl YamlParsableHash for Deployer {
fn parse_all_from_yaml(
documents: Vec<Arc<RwLock<StrictYaml>>>,
_: Option<&Context>,
) -> Result<HashMap<String, Self>, YamlError> {
let mut deployers = HashMap::new();

Expand All @@ -123,7 +125,7 @@ impl YamlParsableHash for Deployer {
Some(network_name) => network_name,
None => deployer_key.clone(),
};
let network = Network::parse_from_yaml(documents.clone(), &network_name)?;
let network = Network::parse_from_yaml(documents.clone(), &network_name, None)?;

let deployer = Deployer {
document: document.clone(),
Expand Down Expand Up @@ -236,7 +238,7 @@ deployers:
"#;

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

assert_eq!(deployers.len(), 2);
assert!(deployers.contains_key("DeployerOne"));
Expand Down Expand Up @@ -272,7 +274,7 @@ deployers:
"#;

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

assert_eq!(
error,
Expand Down
71 changes: 45 additions & 26 deletions crates/settings/src/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use std::{
use strict_yaml_rust::StrictYaml;
use thiserror::Error;
use typeshare::typeshare;
use yaml::{default_document, require_hash, require_string, YamlError, YamlParsableHash};
use yaml::{
context::Context, default_document, require_hash, require_string, YamlError, YamlParsableHash,
};

#[cfg(target_family = "wasm")]
use rain_orderbook_bindings::{impl_all_wasm_traits, wasm_traits::prelude::*};
Expand All @@ -31,6 +33,7 @@ impl_all_wasm_traits!(Deployment);
impl YamlParsableHash for Deployment {
fn parse_all_from_yaml(
documents: Vec<Arc<RwLock<StrictYaml>>>,
_: Option<&Context>,
) -> Result<HashMap<String, Self>, YamlError> {
let mut deployments = HashMap::new();

Expand All @@ -41,26 +44,30 @@ impl YamlParsableHash for Deployment {
for (key_yaml, deployment_yaml) in deployments_hash {
let deployment_key = key_yaml.as_str().unwrap_or_default().to_string();

let scenario = Scenario::parse_from_yaml(
let order = Order::parse_from_yaml(
documents.clone(),
&require_string(
deployment_yaml,
Some("scenario"),
Some("order"),
Some(format!(
"scenario string missing in deployment: {deployment_key}"
"order string missing in deployment: {deployment_key}"
)),
)?,
None,
)?;

let order = Order::parse_from_yaml(
let context = Context::with_order(Arc::new(order.clone()));

let scenario = Scenario::parse_from_yaml(
documents.clone(),
&require_string(
deployment_yaml,
Some("order"),
Some("scenario"),
Some(format!(
"order string missing in deployment: {deployment_key}"
"scenario string missing in deployment: {deployment_key}"
)),
)?,
Some(&context),
)?;

if let Some(deployer) = &order.deployer {
Expand Down Expand Up @@ -238,7 +245,7 @@ mod tests {
let yaml = r#"
test: test
"#;
let error = Deployment::parse_all_from_yaml(vec![get_document(yaml)]).unwrap_err();
let error = Deployment::parse_all_from_yaml(vec![get_document(yaml)], None).unwrap_err();
assert_eq!(
error,
YamlError::ParseError("missing field: deployments".to_string())
Expand All @@ -249,10 +256,10 @@ deployments:
deployment1:
test: test
"#;
let error = Deployment::parse_all_from_yaml(vec![get_document(yaml)]).unwrap_err();
let error = Deployment::parse_all_from_yaml(vec![get_document(yaml)], None).unwrap_err();
assert_eq!(
error,
YamlError::ParseError("scenario string missing in deployment: deployment1".to_string())
YamlError::ParseError("order string missing in deployment: deployment1".to_string())
);

let yaml = r#"
Expand All @@ -264,20 +271,26 @@ deployers:
deployer1:
address: 0x0000000000000000000000000000000000000000
network: network1
scenarios:
scenario1:
bindings:
test: test
tokens:
token1:
address: 0x0000000000000000000000000000000000000000
network: network1
orders:
order1:
inputs:
- token: token1
outputs:
- token: token1
deployer: deployer1
deployments:
deployment1:
scenario: scenario1
order: order1
test: test
"#;
let error = Deployment::parse_all_from_yaml(vec![get_document(yaml)]).unwrap_err();
let error = Deployment::parse_all_from_yaml(vec![get_document(yaml)], None).unwrap_err();
assert_eq!(
error,
YamlError::ParseError("order string missing in deployment: deployment1".to_string())
YamlError::ParseError("scenario string missing in deployment: deployment1".to_string())
);

let yaml = r#"
Expand Down Expand Up @@ -316,7 +329,7 @@ deployments:
scenario: scenario1
order: order1
"#;
let error = Deployment::parse_all_from_yaml(vec![get_document(yaml)]).unwrap_err();
let error = Deployment::parse_all_from_yaml(vec![get_document(yaml)], None).unwrap_err();
assert_eq!(
error.to_string(),
YamlError::ParseDeploymentConfigSourceError(ParseDeploymentConfigSourceError::NoMatch)
Expand Down Expand Up @@ -385,10 +398,13 @@ deployments:
order: order2
"#;

let deployments = Deployment::parse_all_from_yaml(vec![
get_document(&format!("{PREFIX}{yaml_one}")),
get_document(yaml_two),
])
let deployments = Deployment::parse_all_from_yaml(
vec![
get_document(&format!("{PREFIX}{yaml_one}")),
get_document(yaml_two),
],
None,
)
.unwrap();

assert_eq!(deployments.len(), 2);
Expand Down Expand Up @@ -420,10 +436,13 @@ deployments:
order: order2
"#;

let error = Deployment::parse_all_from_yaml(vec![
get_document(&format!("{PREFIX}{yaml_one}")),
get_document(yaml_two),
])
let error = Deployment::parse_all_from_yaml(
vec![
get_document(&format!("{PREFIX}{yaml_one}")),
get_document(yaml_two),
],
None,
)
.unwrap_err();

assert_eq!(
Expand Down
Loading
Loading