Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Stub generator fixes #23

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions wasm-rpc-stubgen/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,32 @@ struct MetadataRoot {
#[derive(Serialize, Deserialize)]
struct ComponentMetadata {
package: Option<String>,
target: ComponentTarget,
target: Option<ComponentTarget>,
}

#[derive(Serialize, Deserialize)]
struct ComponentTarget {
world: Option<String>,
#[serde(default = "default_path")]
path: String,
#[serde(default)]
dependencies: HashMap<String, WitDependency>,
}

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,
Expand Down Expand Up @@ -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,
},
}),
}),
};

Expand Down Expand Up @@ -201,7 +217,11 @@ pub fn is_cargo_workspace_toml(path: &Path) -> anyhow::Result<bool> {
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)?;
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions wasm-rpc-stubgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()?);
Expand Down
4 changes: 2 additions & 2 deletions wasm-rpc-stubgen/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading