-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
205 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
use dotrain::{error::ComposeError, RainDocument}; | ||
use rain_orderbook_app_settings::{ | ||
config_source::{ConfigSource, ConfigSourceError}, | ||
merge::MergeError, | ||
Config, ParseConfigSourceError, | ||
}; | ||
use thiserror::Error; | ||
|
||
use crate::rainlang::compose_to_rainlang; | ||
|
||
pub struct DotrainOrder { | ||
pub config: Config, | ||
pub dotrain: String, | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum DotrainOrderError { | ||
#[error(transparent)] | ||
ConfigSourceError(#[from] ConfigSourceError), | ||
|
||
#[error(transparent)] | ||
ParseConfigSourceError(#[from] ParseConfigSourceError), | ||
|
||
#[error("Scenario {0} not found")] | ||
ScenarioNotFound(String), | ||
|
||
#[error(transparent)] | ||
ComposeError(#[from] ComposeError), | ||
|
||
#[error(transparent)] | ||
MergeConfigError(#[from] MergeError), | ||
} | ||
|
||
impl DotrainOrder { | ||
pub async fn new(dotrain: String, config: Option<String>) -> Result<Self, DotrainOrderError> { | ||
match config { | ||
Some(config) => { | ||
let config_string = ConfigSource::try_from_string(config).await?; | ||
let frontmatter = RainDocument::get_front_matter(&dotrain).unwrap(); | ||
let mut frontmatter_config = | ||
ConfigSource::try_from_string(frontmatter.to_string()).await?; | ||
frontmatter_config.merge(config_string)?; | ||
Ok(Self { | ||
dotrain, | ||
config: frontmatter_config.try_into()?, | ||
}) | ||
} | ||
None => { | ||
let frontmatter = RainDocument::get_front_matter(&dotrain).unwrap(); | ||
let config_string = ConfigSource::try_from_string(frontmatter.to_string()).await?; | ||
let config: Config = config_string.try_into()?; | ||
Ok(Self { dotrain, config }) | ||
} | ||
} | ||
} | ||
|
||
pub async fn compose_scenario_to_rainlang( | ||
&self, | ||
scenario: String, | ||
) -> Result<String, DotrainOrderError> { | ||
let scenario = self | ||
.config | ||
.scenarios | ||
.get(&scenario) | ||
.ok_or_else(|| DotrainOrderError::ScenarioNotFound(scenario))?; | ||
|
||
Ok(compose_to_rainlang( | ||
self.dotrain.clone(), | ||
scenario.bindings.clone(), | ||
)?) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_config_parse() { | ||
let dotrain = r#" | ||
networks: | ||
polygon: | ||
rpc: https://rpc.ankr.com/polygon | ||
chain-id: 137 | ||
network-id: 137 | ||
currency: MATIC | ||
deployers: | ||
polygon: | ||
address: 0x1234567890123456789012345678901234567890 | ||
scenarios: | ||
polygon: | ||
--- | ||
#calculate-io | ||
_ _: 0 0; | ||
#handle-io | ||
:;"#; | ||
|
||
let dotrain_order = DotrainOrder::new(dotrain.to_string(), None).await.unwrap(); | ||
|
||
assert_eq!( | ||
dotrain_order.config.networks.get("polygon").unwrap().rpc, | ||
"https://rpc.ankr.com/polygon".parse().unwrap() | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_rainlang_from_scenario() { | ||
let dotrain = r#" | ||
networks: | ||
polygon: | ||
rpc: https://rpc.ankr.com/polygon | ||
chain-id: 137 | ||
network-id: 137 | ||
currency: MATIC | ||
deployers: | ||
polygon: | ||
address: 0x1234567890123456789012345678901234567890 | ||
scenarios: | ||
polygon: | ||
--- | ||
#calculate-io | ||
_ _: 0 0; | ||
#handle-io | ||
:;"#; | ||
|
||
let dotrain_order = DotrainOrder::new(dotrain.to_string(), None).await.unwrap(); | ||
|
||
let rainlang = dotrain_order | ||
.compose_scenario_to_rainlang("polygon".to_string()) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
rainlang, | ||
r#"/* 0. calculate-io */ | ||
_ _: 0 0; | ||
/* 1. handle-io */ | ||
:;"# | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_config_merge() { | ||
let dotrain = r#" | ||
networks: | ||
polygon: | ||
rpc: https://rpc.ankr.com/polygon | ||
chain-id: 137 | ||
network-id: 137 | ||
currency: MATIC | ||
--- | ||
#calculate-io | ||
_ _: 00; | ||
#handle-io | ||
:;"#; | ||
|
||
let settings = r#" | ||
networks: | ||
mainnet: | ||
rpc: https://1rpc.io/eth | ||
chain-id: 1 | ||
network-id: 1 | ||
currency: ETH"#; | ||
|
||
let merged_dotrain_order = | ||
DotrainOrder::new(dotrain.to_string(), Some(settings.to_string())) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
merged_dotrain_order | ||
.config | ||
.networks | ||
.get("mainnet") | ||
.unwrap() | ||
.rpc, | ||
"https://1rpc.io/eth".parse().unwrap() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters