Skip to content

Commit

Permalink
update schema (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
rvantonder authored Oct 3, 2024
1 parent 661f0a6 commit 3c6bdab
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
61 changes: 49 additions & 12 deletions mvr-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ struct MoveRegistryDependency {
packages: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct MoveToml {
dependencies: HashMap<String, toml::Value>,
#[derive(Debug, Deserialize, Serialize)]
struct RConfig {
network: String,
}

#[derive(PartialEq)]
Expand Down Expand Up @@ -75,11 +75,55 @@ impl fmt::Display for PackageInfoNetwork {
}
}

fn find_mvr_package(value: &toml::Value) -> Option<String> {
value
.as_table()
.and_then(|table| table.get("r"))
.and_then(|r| r.as_table())
.and_then(|r_table| r_table.get("mvr"))
.and_then(|mvr| mvr.as_str())
.map(String::from)
}

/// Parse out packages with the following structure:
///
/// [dependencies]
/// mvr = { r.mvr = "@mvr-tst/first-app" }
///
/// ...
///
/// [r.mvr]
/// network = "mainnet"
fn parse_move_toml(content: &str) -> Result<MoveRegistryDependency> {
use toml::Value;
let toml_value: Value = toml::from_str(content)?;

// Get the network from the [r.mvr] section
let network = toml_value
.get("r")
.and_then(|r| r.get("mvr"))
.and_then(|mvr| mvr.get("network"))
.and_then(|n| n.as_str())
.ok_or_else(|| anyhow!("Expected [r.mvr].network to be a string"))?
.to_string();

let mut packages = Vec::new();
if let Some(dependencies) = toml_value.get("dependencies").and_then(|d| d.as_table()) {
for (_, dep_value) in dependencies {
if let Some(package) = find_mvr_package(dep_value) {
packages.push(package);
}
}
}

Ok(MoveRegistryDependency { network, packages })
}

/// Resolves move registry packages. This function is invoked by `sui move build` and given the
/// a key associated with the external resolution in a Move.toml file. For example, this
/// TOML would trigger sui move build to call this binary and hit this function with value `mvr`:
///
/// mvr = { r."./mvr" = "./mvr", network = "mainnet", packages = [ "@mvr-tst/first-app" ] }
/// mvr = { r.mvr = "@mvr-tst/first-app" }
///
/// The high-level logic of this function is as follows:
/// 1) Fetch on-chain data for `packages`: the GitHub repository, branch, and subpath
Expand All @@ -88,14 +132,7 @@ impl fmt::Display for PackageInfoNetwork {
/// for each package, allow the `sui move build` command to resolve packages from GitHub.
pub async fn resolve_move_dependencies(key: &str) -> Result<()> {
let content = fs::read_to_string("Move.toml")?;
let move_toml: MoveToml = toml::from_str(&content)?;
let dependency: MoveRegistryDependency = move_toml
.dependencies
.get(key)
.ok_or_else(|| anyhow!("Expected dependency '{key}' in [dependencies]"))?
.clone()
.try_into()
.map_err(|_| anyhow!("Unable to parse TOML for external dependency '{key}'"))?;
let dependency: MoveRegistryDependency = parse_move_toml(&content)?;
eprintln!(
"Resolving key = '{}', packages = {:?}",
key, dependency.packages
Expand Down
6 changes: 5 additions & 1 deletion packages/depends-on-demo-on-chain/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ name = "nftmaker"
edition = "2024.beta"

[dependencies]
mvr = { r."./mvr" = "./mvr", network = "mainnet", packages = [ "mvr-tst/first-app" ] }
mvr = { r.mvr = "@mvr-tst/first-app/1" }

[r.mvr]
network = "mainnet"

Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/mainnet" }

[addresses]
Expand Down

0 comments on commit 3c6bdab

Please sign in to comment.