forked from dojoengine/dojo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: make
DojoMetadata
to handle metadata specific to dojo eas…
…ier (dojoengine#707)
- Loading branch information
Showing
18 changed files
with
169 additions
and
104 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
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
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 |
---|---|---|
@@ -1,10 +1,111 @@ | ||
use scarb::core::Workspace; | ||
use toml::Value; | ||
use scarb::core::{ManifestMetadata, Workspace}; | ||
use serde::Deserialize; | ||
|
||
pub mod account; | ||
pub mod starknet; | ||
pub mod world; | ||
|
||
pub(super) fn dojo_metadata_from_workspace(ws: &Workspace<'_>) -> Option<Value> { | ||
ws.current_package().ok()?.manifest.metadata.tool_metadata.as_ref()?.get("dojo").cloned() | ||
pub(crate) fn dojo_metadata_from_workspace(ws: &Workspace<'_>) -> Option<DojoMetadata> { | ||
Some(ws.current_package().ok()?.manifest.metadata.dojo()) | ||
} | ||
|
||
#[derive(Default, Deserialize, Debug, Clone)] | ||
pub(crate) struct DojoMetadata { | ||
env: Option<Environment>, | ||
} | ||
|
||
#[derive(Default, Deserialize, Clone, Debug)] | ||
pub struct Environment { | ||
rpc_url: Option<String>, | ||
account_address: Option<String>, | ||
private_key: Option<String>, | ||
keystore_path: Option<String>, | ||
keystore_password: Option<String>, | ||
world_address: Option<String>, | ||
} | ||
|
||
impl Environment { | ||
pub fn world_address(&self) -> Option<&str> { | ||
self.world_address.as_deref() | ||
} | ||
|
||
pub fn rpc_url(&self) -> Option<&str> { | ||
self.rpc_url.as_deref() | ||
} | ||
|
||
pub fn account_address(&self) -> Option<&str> { | ||
self.account_address.as_deref() | ||
} | ||
|
||
pub fn private_key(&self) -> Option<&str> { | ||
self.private_key.as_deref() | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn keystore_path(&self) -> Option<&str> { | ||
self.keystore_path.as_deref() | ||
} | ||
|
||
pub fn keystore_password(&self) -> Option<&str> { | ||
self.keystore_password.as_deref() | ||
} | ||
} | ||
|
||
impl DojoMetadata { | ||
pub fn env(&self) -> Option<&Environment> { | ||
self.env.as_ref() | ||
} | ||
} | ||
trait MetadataExt { | ||
fn dojo(&self) -> DojoMetadata; | ||
} | ||
|
||
impl MetadataExt for ManifestMetadata { | ||
fn dojo(&self) -> DojoMetadata { | ||
self.tool_metadata | ||
.as_ref() | ||
.and_then(|e| e.get("dojo")) | ||
.cloned() | ||
.map(|v| v.try_into::<DojoMetadata>().unwrap_or_default()) | ||
.unwrap_or_default() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
#[test] | ||
fn check_deserialization() { | ||
let metadata: DojoMetadata = toml::from_str( | ||
r#" | ||
[env] | ||
rpc_url = "http://localhost:5050/" | ||
account_address = "0x03ee9e18edc71a6df30ac3aca2e0b02a198fbce19b7480a63a0d71cbd76652e0" | ||
private_key = "0x0300001800000000300000180000000000030000000000003006001800006600" | ||
keystore_path = "test/" | ||
keystore_password = "dojo" | ||
world_address = "0x0248cacaeac64c45be0c19ee8727e0bb86623ca7fa3f0d431a6c55e200697e5a" | ||
"#, | ||
) | ||
.unwrap(); | ||
|
||
assert!(metadata.env.is_some()); | ||
let env = metadata.env.unwrap(); | ||
|
||
assert_eq!(env.rpc_url(), Some("http://localhost:5050/")); | ||
assert_eq!( | ||
env.account_address(), | ||
Some("0x03ee9e18edc71a6df30ac3aca2e0b02a198fbce19b7480a63a0d71cbd76652e0") | ||
); | ||
assert_eq!( | ||
env.private_key(), | ||
Some("0x0300001800000000300000180000000000030000000000003006001800006600") | ||
); | ||
assert_eq!(env.keystore_path(), Some("test/")); | ||
assert_eq!(env.keystore_password(), Some("dojo")); | ||
assert_eq!( | ||
env.world_address(), | ||
Some("0x0248cacaeac64c45be0c19ee8727e0bb86623ca7fa3f0d431a6c55e200697e5a") | ||
); | ||
} | ||
} |
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
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
Oops, something went wrong.