From f7f98f5337d7538dd15804ff2d79da12dd366e3d Mon Sep 17 00:00:00 2001 From: Tyler K Date: Mon, 28 Oct 2024 19:44:43 -0700 Subject: [PATCH] Generate parameter name rather than type --- tool/src/demo_gen/terminus.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/tool/src/demo_gen/terminus.rs b/tool/src/demo_gen/terminus.rs index b9f101595..0a5c1f8c1 100644 --- a/tool/src/demo_gen/terminus.rs +++ b/tool/src/demo_gen/terminus.rs @@ -44,8 +44,8 @@ struct MethodDependency { /// Parameters to pass into the method. params: Vec, - /// 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, } pub(super) struct RenderTerminusContext<'ctx, 'tcx> { @@ -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) -> Self { MethodDependency { method_js, params: Vec::new(), - owning_type, + owning_param, } } } @@ -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: @@ -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() @@ -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); @@ -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) @@ -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; @@ -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); @@ -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 @@ -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")]