Skip to content

Commit

Permalink
Generate parameter name rather than type
Browse files Browse the repository at this point in the history
  • Loading branch information
ambiguousname committed Oct 29, 2024
1 parent 81518ef commit f7f98f5
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions tool/src/demo_gen/terminus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ struct MethodDependency {
/// Parameters to pass into the method.
params: Vec<ParamInfo>,

/// The type name that this method belongs to. Currently used by [`OutParam`] for better default parameter names.
owning_type: String,
/// The Rust parameter that we're attempting to construct with this method. Currently used by [`OutParam`] for better default parameter names.
owning_param: Option<String>,
}

pub(super) struct RenderTerminusContext<'ctx, 'tcx> {
Expand All @@ -59,11 +59,11 @@ pub(super) struct RenderTerminusContext<'ctx, 'tcx> {
}

impl MethodDependency {
pub fn new(method_js: String, owning_type: String) -> Self {
pub fn new(method_js: String, owning_param: Option<String>) -> Self {
MethodDependency {
method_js,
params: Vec::new(),
owning_type,
owning_param,
}
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
// which I find easier easier to represent as a parameter to each function than something like an updating the current node in the struct.
let mut root = MethodDependency::new(
self.get_constructor_js(type_name.clone(), method),
type_name.clone(),
None,
);

// And then we just treat the terminus as a regular constructor method:
Expand Down Expand Up @@ -195,9 +195,12 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
let attrs_default = attrs.unwrap_or_default();
// This only works for enums, since otherwise we break the type into its component parts.
let label = if attrs_default.input_cfg.label.is_empty() {
let owning_str = node.owning_param.as_ref().map(|p| {
format!("{}:", heck::AsUpperCamelCase(p))
}).unwrap_or_default();
format!(
"{}:{}",
heck::AsUpperCamelCase(node.owning_type.clone()),
"{}{}",
owning_str,
heck::AsUpperCamelCase(param_name.clone())
)
.to_string()
Expand Down Expand Up @@ -298,7 +301,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
return;
}

self.evaluate_op_constructors(op, type_name.to_string(), node);
self.evaluate_op_constructors(op, type_name.to_string(), param_name, node);
}
Type::Struct(s) => {
let st = s.resolve(self.tcx);
Expand All @@ -309,7 +312,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
.push_error(format!("Found usage of disabled type {type_name}"))
}

self.evaluate_struct_fields(st, type_name.to_string(), node);
self.evaluate_struct_fields(st, type_name.to_string(), param_name, node);
}
Type::DiplomatOption(ref inner) => {
self.evaluate_param(inner, param_name, node, param_attrs)
Expand Down Expand Up @@ -353,6 +356,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
&mut self,
op: &OpaqueDef,
type_name: String,
param_name : String,
node: &mut MethodDependency,
) {
let mut usable_constructor = false;
Expand Down Expand Up @@ -380,7 +384,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {

let mut child = MethodDependency::new(
self.get_constructor_js(type_name.to_string(), method),
type_name,
Some(param_name),
);

let call = self.evaluate_constructor(method, &mut child);
Expand Down Expand Up @@ -415,6 +419,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
&mut self,
st: &StructDef,
type_name: String,
param_name: String,
node: &mut MethodDependency,
) {
self.terminus_info
Expand All @@ -425,7 +430,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
self.relative_import_path.clone(),
));

let mut child = MethodDependency::new("".to_string(), type_name.clone());
let mut child = MethodDependency::new("".to_string(), Some(param_name));

#[derive(Template)]
#[template(path = "demo_gen/struct.js.jinja", escape = "none")]
Expand Down

0 comments on commit f7f98f5

Please sign in to comment.