Skip to content

Commit

Permalink
Fix Rust struct naming (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejressel authored Aug 11, 2024
1 parent 12da86b commit 36c46a3
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 118 deletions.
168 changes: 84 additions & 84 deletions providers/pulumi_wasm_provider_cloudflare_rust/src/types.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions providers/pulumi_wasm_provider_docker_rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ pub struct VolumeLabel {
}

#[derive(serde::Serialize)]
pub struct getNetworkIpamConfig {
pub struct GetNetworkIpamConfig {
#[serde(rename = "auxAddress")]
pub r#aux_address: Box<Option<std::collections::HashMap<String, String>>>,
#[serde(rename = "gateway")]
Expand All @@ -799,7 +799,7 @@ pub struct getNetworkIpamConfig {
}

#[derive(serde::Serialize)]
pub struct registryAuth {
pub struct RegistryAuth {
#[serde(rename = "address")]
pub r#address: Box<String>,
#[serde(rename = "authDisabled")]
Expand Down
32 changes: 29 additions & 3 deletions pulumi_wasm_generator_lib/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@ pub(crate) enum Type {
Option(Box<Type>),
}

impl Type {
pub(crate) fn get_rust_type(&self) -> String {
match self {
Type::Boolean => "bool".into(),
Type::Integer => "i32".into(),
Type::Number => "f64".into(),
Type::String => "String".into(),
Type::Array(type_) => format!("Vec<{}>", type_.get_rust_type()),
Type::Object(type_) => {
format!(
"std::collections::HashMap<String, {}>",
type_.get_rust_type()
)
}
Type::Ref(r) => match r {
Ref::Type(tpe) => format!("crate::types::{}", tpe.get_rust_struct_name()),
Ref::Archive => "String".to_string(), //FIXME
Ref::Asset => "String".to_string(), //FIXME
Ref::Any => "String".to_string(), //FIXME
},
Type::Option(type_) => format!("Option<{}>", type_.get_rust_type()),
}
}
}

#[derive(Debug, PartialEq, Hash, Ord, PartialOrd, Eq)]
pub(crate) struct InputProperty {
pub(crate) name: String,
Expand Down Expand Up @@ -101,6 +126,10 @@ pub(crate) struct ElementId {
}

impl ElementId {
pub(crate) fn get_rust_struct_name(&self) -> String {
self.name.clone().to_case(Case::Pascal)
}

pub(crate) fn get_rust_function_name(&self) -> String {
self.name
.clone()
Expand All @@ -114,9 +143,6 @@ impl ElementId {
Self::create_valid_id(&vec.join("-"))
}

pub(crate) fn get_wit_argument_name(&self) -> String {
Self::create_valid_wit_id(self.name.as_str())
}
pub(crate) fn get_wit_interface_name(&self) -> String {
let mut vec = self.namespace.clone();
vec.push(self.name.clone());
Expand Down
22 changes: 0 additions & 22 deletions pulumi_wasm_generator_lib/src/output/rust/mod.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,4 @@
use crate::model::{Ref, Type};

pub(crate) mod cargo;
pub(crate) mod source_code_librs;
pub(crate) mod source_code_resource;
pub(crate) mod source_code_types;

fn convert_type(type_or_ref: &Type) -> String {
match type_or_ref {
Type::Boolean => "bool".into(),
Type::Integer => "i32".into(),
Type::Number => "f64".into(),
Type::String => "String".into(),
Type::Array(type_) => format!("Vec<{}>", convert_type(type_)), // "Vec<{}>
Type::Object(type_) => {
format!("std::collections::HashMap<String, {}>", convert_type(type_))
}
Type::Ref(r) => match r {
Ref::Type(tpe) => format!("crate::types::{}", tpe.name),
Ref::Archive => "String".to_string(), //FIXME
Ref::Asset => "String".to_string(), //FIXME
Ref::Any => "String".to_string(), //FIXME
},
Type::Option(type_) => format!("Option<{}>", convert_type(type_)),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn convert_model(package: &crate::model::Package) -> Package {
interfaces: package
.resources
.iter()
.map(|(element_id, resource)| Interface {})
.map(|(_, _)| Interface {})
.collect(),
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::output::rust::convert_type;
use handlebars::Handlebars;
use serde::Serialize;
use serde_json::json;
Expand Down Expand Up @@ -50,7 +49,7 @@ fn convert_model(package: &crate::model::Package) -> Package {
.map(|input_property| InputProperty {
name: input_property.name.clone(),
arg_name: input_property.get_rust_argument_name(),
type_: convert_type(&input_property.r#type),
type_: input_property.r#type.get_rust_type(),
})
.collect(),
output_properties: resource
Expand All @@ -59,7 +58,7 @@ fn convert_model(package: &crate::model::Package) -> Package {
.map(|output_property| OutputProperty {
name: output_property.name.clone(),
arg_name: output_property.get_rust_argument_name(),
type_: convert_type(&output_property.r#type),
type_: output_property.r#type.get_rust_type(),
})
.collect(),
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::model::GlobalType;
use crate::output::rust::convert_type;
use convert_case::{Case, Casing};
use handlebars::Handlebars;
use serde::Serialize;
Expand Down Expand Up @@ -44,7 +43,7 @@ fn convert_model(package: &crate::model::Package) -> Package {
.for_each(|(element_id, resource)| match resource {
GlobalType::Object(properties) => {
let ref_type = RefType {
struct_name: element_id.name.clone(),
struct_name: element_id.get_rust_struct_name(),
fields: properties
.iter()
.map(|global_type_property| Property {
Expand All @@ -54,7 +53,7 @@ fn convert_model(package: &crate::model::Package) -> Package {
.from_case(Case::Camel)
.to_case(Case::Snake),
original_name: global_type_property.name.clone(),
type_: convert_type(&global_type_property.r#type),
type_: global_type_property.r#type.get_rust_type(),
})
.collect(),
};
Expand Down

0 comments on commit 36c46a3

Please sign in to comment.