Skip to content
Draft
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
91 changes: 48 additions & 43 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,56 @@ pub fn populate_env(
// === Custom functions ===
env.add_function("vec", crate::cli_v2::value_vec::new_value_vec);

env.add_function(
// allows overriding the summary filename
"set_summary_filename",
|state: &State, filename: Cow<'_, str>| {
state.set_temp("summary_filename", filename.into());
},
);

env.add_function(
// For java lib we need to create extra files.
"generate_extra_file",
|state: &State, filename: Cow<'_, str>, file_contents: Cow<'_, str>| {
fs::write(&*filename, file_contents.as_bytes()).unwrap();
state.set_temp("extra_generated_file", filename.into());
},
);

env.add_function(
"panic",
|message: Cow<'_, str>| -> Result<String, minijinja::Error> {
Err(minijinja::Error::new(
minijinja::ErrorKind::InvalidOperation,
message.to_string(),
))
},
);

// === Custom filters ===

// --- String utilities ---
env.add_filter("strip_trailing_comma", |s: Cow<'_, str>| {
match s.trim_end().strip_suffix(",") {
Some(stripped) => stripped.to_string(),
None => s.into_owned(),
}
});
env.add_filter(
"strip_trailing_str",
|s: Cow<'_, str>, trailing_str: Cow<'_, str>| match s
.trim_end()
.strip_suffix(&trailing_str.to_string())
{
Some(stripped) => stripped.to_string(),
None => s.into_owned(),
},
);
env.add_filter("remove_suffix", |s: Cow<'_, str>, suffix: Cow<'_, str>| {
Value::from(s.strip_suffix(&*suffix))
});

// --- Case conversion ---
env.add_filter("to_upper_snake_case", |s: Cow<'_, str>| {
s.to_shouty_snake_case()
Expand Down Expand Up @@ -132,22 +180,6 @@ pub fn populate_env(
);

// --- Miscellaneous ---
env.add_filter("strip_trailing_comma", |s: Cow<'_, str>| {
match s.trim_end().strip_suffix(",") {
Some(stripped) => stripped.to_string(),
None => s.into_owned(),
}
});
env.add_filter(
"strip_trailing_str",
|s: Cow<'_, str>, trailing_str: Cow<'_, str>| match s
.trim_end()
.strip_suffix(&trailing_str.to_string())
{
Some(stripped) => stripped.to_string(),
None => s.into_owned(),
},
);
env.add_filter(
"generate_kt_path_str",
|s: Cow<'_, str>, path_params: &Vec<Value>| -> Result<String, minijinja::Error> {
Expand Down Expand Up @@ -202,23 +234,6 @@ pub fn populate_env(
},
);

env.add_function(
// allows overriding the summary filename
"set_summary_filename",
|state: &State, filename: Cow<'_, str>| {
state.set_temp("summary_filename", filename.into());
},
);

env.add_function(
// For java lib we need to create extra files.
"generate_extra_file",
|state: &State, filename: Cow<'_, str>, file_contents: Cow<'_, str>| {
fs::write(&*filename, file_contents.as_bytes()).unwrap();
state.set_temp("extra_generated_file", filename.into());
},
);

env.add_filter(
"struct_enum_ref_variants",
|variants: Vec<Value>| -> Result<Vec<Value>, minijinja::Error> {
Expand Down Expand Up @@ -255,16 +270,6 @@ pub fn populate_env(
},
);

env.add_function(
"panic",
|message: Cow<'_, str>| -> Result<String, minijinja::Error> {
Err(minijinja::Error::new(
minijinja::ErrorKind::InvalidOperation,
message.to_string(),
))
},
);

env.add_filter(
// TODO: fix this ugly ass code :(
// TLDR: If I have a number serde_json::Value, it does not get passed the the template correctly
Expand Down
Loading