Skip to content

Commit b83be14

Browse files
Use macro for implementations of ops traits on references (#585)
1 parent bef32ca commit b83be14

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

src/main.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use anyhow::{bail, Context};
44
use clap::{arg, command};
55
use rustfmt_wrapper::rustfmt;
66
use std::path::Path;
7+
use tera::{from_value, to_value, Value};
78

89
use outputs::build_output_pairs;
910

@@ -60,7 +61,28 @@ fn main() -> anyhow::Result<()> {
6061
} else {
6162
None
6263
};
63-
let tera = tera::Tera::new("templates/**/*.rs.tera").context("tera parsing error(s)")?;
64+
let mut tera = tera::Tera::new("templates/**/*.rs.tera").context("tera parsing error(s)")?;
65+
tera.register_filter(
66+
"snake_case",
67+
|value: &Value, _: &_| -> tera::Result<Value> {
68+
let input = from_value::<String>(value.clone())?;
69+
let mut iter = input.chars();
70+
71+
let mut string = String::new();
72+
73+
if let Some(first) = iter.next() {
74+
string.push(first.to_ascii_lowercase());
75+
76+
for char in iter {
77+
if char.is_uppercase() {
78+
string.push('_');
79+
}
80+
string.push(char.to_ascii_lowercase());
81+
}
82+
}
83+
tera::Result::Ok(to_value(string)?)
84+
},
85+
);
6486

6587
let repo = git2::Repository::open(GLAM_ROOT).context("failed to open git repo")?;
6688
let workdir = repo.workdir().unwrap();

0 commit comments

Comments
 (0)