Skip to content

Commit 8e03e54

Browse files
committed
trait calls
1 parent 506022b commit 8e03e54

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

crates/bevy_api_gen/src/template.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,34 @@ pub(crate) fn configure_tera_env(tera: &mut Tera, crate_name: &str) {
317317
let case = expect_str(expect_arg(args, "case")?)?;
318318
Ok(Value::String(str.to_case(case_from_str(case)?)))
319319
},
320+
);
321+
322+
tera.register_filter(
323+
"to_arg_pattern",
324+
|val: &Value, _args: &HashMap<String, Value>| {
325+
let ty = expect_str(val)?;
326+
if ty == "self" {
327+
return Ok(Value::String("_self".to_owned()));
328+
}
329+
Ok(Value::String(ty.to_owned()))
330+
},
331+
);
332+
333+
tera.register_function(
334+
"function_call_expression",
335+
|args: &HashMap<String, Value>| {
336+
let ident = expect_str(expect_arg(args, "type")?)?;
337+
let function_name = expect_str(expect_arg(args, "function")?)?;
338+
let trait_name: Option<&str> = args.get("trait").and_then(|v| v.as_str());
339+
340+
if let Some(trait_name) = trait_name {
341+
Ok(Value::String(format!(
342+
"<{ident} as {trait_name}>::{function_name}"
343+
)))
344+
} else {
345+
Ok(Value::String(format!("{ident}::{function_name}")))
346+
}
347+
},
320348
)
321349
}
322350

crates/bevy_api_gen/templates/footer.tera

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,23 @@ impl ::bevy::app::Plugin for {{ "ScriptingPlugin" | prefix_cratename | convert_c
1818
{%- for arg in function.args -%}
1919
{%- if arg.proxy_ty is matching("Mut.*")-%}
2020
mut {% endif -%}
21-
{%- if arg.ident != "self" -%}
22-
{{- arg.ident -}}
23-
{%- else -%}
24-
_{{- arg.ident -}}
25-
{%- endif -%}
21+
{{- arg.ident | to_arg_pattern() -}}
2622
: {{- arg.proxy_ty -}},
2723
{%- endfor -%}
2824
| {
29-
let output: {{ function.output.proxy_ty }} = ::{{ item.import_path }}::{{ function.ident }}(
25+
let output: {{ function.output.proxy_ty }} =
26+
{%- if function.from_trait_path -%}
27+
{{- function_call_expression(type=item.import_path, trait=function.from_trait_path, function=function.ident) -}}
28+
{%- else -%}
29+
{{- function_call_expression(type=item.import_path, function=function.ident) -}}
30+
{%- endif -%}
31+
(
3032
{%- for arg in function.args -%}
3133
{%- if arg.proxy_ty is matching("Ref.*")-%}
3234
&{% endif -%}
3335
{%- if arg.proxy_ty is matching ("Mut.*")-%}
3436
&mut {% endif -%}
35-
{%- if arg.ident != "self" -%}
36-
{{- arg.ident -}}
37-
{%- else -%}
38-
_{{- arg.ident -}}
39-
{%- endif -%}
37+
{{- arg.ident | to_arg_pattern() -}}
4038
{%- if arg.proxy_ty is matching("Val.*")-%}
4139
.into_inner()
4240
{%- endif -%},

0 commit comments

Comments
 (0)