From 047e593350412ca5f9ba820ba04cc788101f6f4f Mon Sep 17 00:00:00 2001 From: Daniel Vigovszky Date: Sat, 23 Mar 2024 09:40:19 +0100 Subject: [PATCH] Stub generator fixes --- wasm-rpc-stubgen/src/cargo.rs | 38 +++++++++++++++++++++++++++++------ wasm-rpc-stubgen/src/lib.rs | 10 ++++----- wasm-rpc-stubgen/src/rust.rs | 4 ++-- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/wasm-rpc-stubgen/src/cargo.rs b/wasm-rpc-stubgen/src/cargo.rs index 1a9217a5..a22a97ff 100644 --- a/wasm-rpc-stubgen/src/cargo.rs +++ b/wasm-rpc-stubgen/src/cargo.rs @@ -34,16 +34,32 @@ struct MetadataRoot { #[derive(Serialize, Deserialize)] struct ComponentMetadata { package: Option, - target: ComponentTarget, + target: Option, } #[derive(Serialize, Deserialize)] struct ComponentTarget { world: Option, + #[serde(default = "default_path")] path: String, + #[serde(default)] dependencies: HashMap, } +fn default_path() -> String { + "wit".to_string() +} + +impl Default for ComponentTarget { + fn default() -> Self { + Self { + world: None, + path: "wit".to_string(), + dependencies: HashMap::new(), + } + } +} + #[derive(Serialize, Deserialize)] struct WitDependency { path: String, @@ -97,11 +113,11 @@ pub fn generate_cargo_toml(def: &StubDefinition) -> anyhow::Result<()> { "{}:{}", def.root_package_name.namespace, def.root_package_name.name )), - target: ComponentTarget { + target: Some(ComponentTarget { world: Some(def.target_world_name()?), path: "wit".to_string(), dependencies: wit_dependencies, - }, + }), }), }; @@ -201,7 +217,11 @@ pub fn is_cargo_workspace_toml(path: &Path) -> anyhow::Result { pub fn add_workspace_members(path: &Path, members: &[String]) -> anyhow::Result<()> { let mut manifest = Manifest::from_path(path)?; if let Some(workspace) = manifest.workspace.as_mut() { - workspace.members.extend(members.iter().cloned()); + for member in members { + if !workspace.members.contains(member) { + workspace.members.push(member.to_string()); + } + } } let cargo_toml = toml::to_string(&manifest)?; @@ -216,7 +236,9 @@ pub fn add_dependencies_to_cargo_toml(cargo_path: &Path, names: &[String]) -> an if let Some(ref mut package) = manifest.package { if let Some(ref mut metadata) = package.metadata { if let Some(ref mut component) = metadata.component { - let existing: HashSet<_> = component.target.dependencies.keys().cloned().collect(); + let mut new_target = ComponentTarget::default(); + let target = component.target.as_mut().unwrap_or(&mut new_target); + let existing: HashSet<_> = target.dependencies.keys().cloned().collect(); for name in names { if !existing.contains(name) { let relative_path = format!("wit/deps/{}", name); @@ -226,7 +248,7 @@ pub fn add_dependencies_to_cargo_toml(cargo_path: &Path, names: &[String]) -> an .join(&relative_path); let package_name = wit::get_package_name(&path)?; - component.target.dependencies.insert( + target.dependencies.insert( format!("{}:{}", package_name.namespace, package_name.name), WitDependency { path: relative_path, @@ -235,6 +257,10 @@ pub fn add_dependencies_to_cargo_toml(cargo_path: &Path, names: &[String]) -> an } } + if component.target.is_none() { + component.target = Some(new_target); + } + let cargo_toml = toml::to_string(&manifest)?; println!("Writing updated Cargo.toml to {:?}", cargo_path); diff --git a/wasm-rpc-stubgen/src/lib.rs b/wasm-rpc-stubgen/src/lib.rs index 2a1ee435..db493ffa 100644 --- a/wasm-rpc-stubgen/src/lib.rs +++ b/wasm-rpc-stubgen/src/lib.rs @@ -37,7 +37,7 @@ use tempdir::TempDir; use wasm_compose::config::Dependency; #[derive(Parser, Debug)] -#[command(name = "wasm-rpc-stubgen")] +#[command(name = "wasm-rpc-stubgen", version)] #[command(bin_name = "wasm-rpc-stubgen")] pub enum Command { /// Generate a Rust RPC stub crate for a WASM component @@ -272,13 +272,13 @@ pub fn add_stub_dependency(args: AddStubDependencyArgs) -> anyhow::Result<()> { if let Some(target_parent) = args.dest_wit_root.parent() { let target_cargo_toml = target_parent.join("Cargo.toml"); - if target_cargo_toml.exists() - && target_cargo_toml.is_file() - && cargo::is_cargo_component_toml(&target_cargo_toml).is_ok() - { + if target_cargo_toml.exists() && target_cargo_toml.is_file() { if !args.update_cargo_toml { eprintln!("Warning: the newly copied dependencies have to be added to {}. Use the --update-cargo-toml flag to update it automatically.", target_cargo_toml.to_string_lossy()); } else { + cargo::is_cargo_component_toml(&target_cargo_toml).context(format!( + "The file {target_cargo_toml:?} is not a valid cargo-component project" + ))?; let mut names = Vec::new(); for action in actions { names.push(action.get_dep_dir_name()?); diff --git a/wasm-rpc-stubgen/src/rust.rs b/wasm-rpc-stubgen/src/rust.rs index 5a80ca5b..24ef2e48 100644 --- a/wasm-rpc-stubgen/src/rust.rs +++ b/wasm-rpc-stubgen/src/rust.rs @@ -769,7 +769,7 @@ fn wit_enum_value_builder( let mut cases = Vec::new(); for (n, case) in enum_def.cases.iter().enumerate() { - let case_name = Ident::new(&case.name.to_shouty_snake_case(), Span::call_site()); + let case_name = Ident::new(&case.name.to_upper_camel_case(), Span::call_site()); let case_idx = n as u32; cases.push(quote! { #enum_type::#case_name => #case_idx @@ -1074,7 +1074,7 @@ fn extract_from_enum_value( let mut case_extractors = Vec::new(); for (n, case) in enum_def.cases.iter().enumerate() { - let case_name = Ident::new(&case.name.to_shouty_snake_case(), Span::call_site()); + let case_name = Ident::new(&case.name.to_upper_camel_case(), Span::call_site()); let case_idx = n as u32; case_extractors.push(quote! { #case_idx => #enum_type::#case_name