diff --git a/Cargo.lock b/Cargo.lock index a439ac4..8bf55d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -106,7 +106,7 @@ checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" [[package]] name = "codegen" -version = "0.1.1" +version = "0.2.0" dependencies = [ "anyhow", "argh", @@ -133,31 +133,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "crossbeam-deque" -version = "0.8.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5181e0de7b61eb03a81e347d6dd8797bae9da5146707b51077e2d71a54ec0ceb" -dependencies = [ - "crossbeam-epoch", - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-epoch" -version = "0.9.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d6914041f254d6e9176c01941b21115dcfb7089e55135a35411081bd106ef3f" -dependencies = [ - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-utils" -version = "0.8.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61803da095bee82a81bb1a452ecc25d3b2f1416d1897eb86430c6159ef717c17" - [[package]] name = "difflib" version = "0.4.0" @@ -234,17 +209,6 @@ dependencies = [ "regex-syntax", ] -[[package]] -name = "globwalk" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bf760ebf69878d9fd8f110c89703d90ce35095324d1f1edcb595c63945ee757" -dependencies = [ - "bitflags", - "ignore", - "walkdir", -] - [[package]] name = "hashbrown" version = "0.17.1" @@ -260,22 +224,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "ignore" -version = "0.4.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f8a7b8211e695a1d0cd91cace480d4d0bd57667ab10277cc412c5f7f4884f83" -dependencies = [ - "crossbeam-deque", - "globset", - "log", - "memchr", - "regex-automata", - "same-file", - "walkdir", - "winapi-util", -] - [[package]] name = "indexmap" version = "2.14.0" @@ -314,12 +262,6 @@ dependencies = [ "libc", ] -[[package]] -name = "lazy_static" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" - [[package]] name = "libc" version = "0.2.189" @@ -374,48 +316,6 @@ version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" -[[package]] -name = "pest" -version = "2.8.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7df728be843c7070fab6ab7c328c4e9e9d78e23bf749c0669c86ee7ebfa050a2" -dependencies = [ - "memchr", - "ucd-trie", -] - -[[package]] -name = "pest_derive" -version = "2.8.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e2dd6fc3b26b3462ee188aac870f5a41d398f1cd5e2408d16531bd71c9591fd" -dependencies = [ - "pest", - "pest_generator", -] - -[[package]] -name = "pest_generator" -version = "2.8.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a7a9205cfb6f596a9e8b689c0a15f9ceb7a1aafae7aaf788150ac65b29975b6" -dependencies = [ - "pest", - "pest_meta", - "proc-macro2", - "quote", - "syn 2.0.119", -] - -[[package]] -name = "pest_meta" -version = "2.8.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85abd351c0de1e8384fc791a0737111a350394937e92b956b743dac12429f57c" -dependencies = [ - "pest", -] - [[package]] name = "pkg-config" version = "0.3.33" @@ -644,18 +544,13 @@ dependencies = [ [[package]] name = "tera" -version = "1.20.1" +version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8004bca281f2d32df3bacd59bc67b312cb4c70cea46cbd79dbe8ac5ed206722" +checksum = "511f07fd91a70e92efbe4793d111aaa9035f8474dd157aaa1e31e7c27f5051da" dependencies = [ - "globwalk", - "lazy_static", - "pest", - "pest_derive", - "regex", + "globset", "serde", - "serde_json", - "unicode-segmentation", + "walkdir", ] [[package]] @@ -738,24 +633,12 @@ dependencies = [ "walkdir", ] -[[package]] -name = "ucd-trie" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971" - [[package]] name = "unicode-ident" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" -[[package]] -name = "unicode-segmentation" -version = "1.13.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8" - [[package]] name = "vcpkg" version = "0.2.15" diff --git a/Cargo.toml b/Cargo.toml index a6e442f..2aea1a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "codegen" description = "A tool to generate Rust source code from tera templates" -version = "0.1.1" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -15,7 +15,7 @@ globset = { version = "0.4", default-features = false } rustfmt-wrapper = { version = "0.2" } serde = { version = "1.0", default-features = false, features = ["std"] } serde_json = { version = "1.0", default-features = false, features = ["std"] } -tera = { version = "1", default-features = false } +tera = { version = "2", default-features = false, features = ["glob_fs"] } [dev-dependencies] insta = "1" diff --git a/README.md b/README.md index 2b248d7..3a96388 100644 --- a/README.md +++ b/README.md @@ -20,15 +20,15 @@ by `glam` users. ## Tera templates -Source files are generated using [`Tera`] templates. These kinds of templates +Source files are generated using [`Tera v2`] templates. These kinds of templates are typically used to generate static web pages, but here we are using them to generate Rust source. The main advantages are a lot of the template looks like Rust code. The templates are fairly declarative and easier to follow than the macros (in my opinion). I try to stick to basic features of the [templating language] to keep things simple. -[`Tera`]: https://keats.github.io/tera/ -[templating language]: https://keats.github.io/tera/docs/ +[`Tera v2`]: https://keats.github.io/tera/ +[templating language]: https://keats.github.io/tera/#template ### Control variables @@ -77,7 +77,8 @@ Each template starts with setting up a number of common variables based on the inputs from the `codegen` program. Commonly used variables are: * `self_t` - the name of the type being generated -* `unsigned_scalar_t` - the unsigned version of `scalar_t` (e.g. `u8`, `u16`, `u32`) +* `unsigned_scalar_t` - the unsigned version of `scalar_t` (e.g. `u8`, `u16`, + `u32`) * `inner_t` - the inner storage type used by this type (e.g. `__m128` or `core::storage::XYZ`) * `deref_t` - the type used by the `Deref` and `DerefMut` implementation - not @@ -95,7 +96,8 @@ inputs from the `codegen` program. Commonly used variables are: ## Running `codegen` -The easiest way to run `codegen` on a clone of the glam repo is to first initialize the submodule with `git`: +The easiest way to run `codegen` on a clone of the glam repo is to first +initialize the submodule with `git`: ```sh git submodule init @@ -104,13 +106,13 @@ git submodule init Then run it via `cargo`: ```sh -cargo run -p codegen +cargo run --release -p codegen ``` To pass additional parameters, e.g. `-h` for help: ```sh -cargo r -p codegen -- -h +cargo r --release -p codegen -- -h ``` `codegen` will generate all files by default or if a glob pattern is specified, @@ -148,3 +150,4 @@ Licensed under either of or [http://opensource.org/licenses/MIT]) at your option. + diff --git a/src/main.rs b/src/main.rs index 604f5ef..90f7268 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ use rustfmt_wrapper::rustfmt; use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; use std::path::{Path, PathBuf}; -use tera::{from_value, to_value}; // use outputs::build_output_pairs; @@ -53,11 +52,11 @@ impl Config { context.insert("template_path", template_path); for (prop_key, prop_value) in template.properties.iter() { if let Some(prop_override) = output.properties.get(prop_key) { - context.insert(prop_key, prop_override); + context.insert(prop_key.clone(), prop_override); } else { // TODO: error message if let Some(prop_value) = prop_value { - context.insert(prop_key, prop_value); + context.insert(prop_key.clone(), prop_value); } else { return Err(anyhow::Error::msg("Missing property override")); } @@ -235,14 +234,13 @@ fn main() -> anyhow::Result<()> { let template_path = Path::new(config_root) .join(&config.template_root) .join("**/*.rs.tera"); - let mut tera = - tera::Tera::new(template_path.to_str().unwrap()).context("tera parsing error(s)")?; + let mut tera = tera::Tera::new(); + tera.load_from_glob(template_path.to_str().unwrap()) + .context("tera parsing error(s)")?; tera.register_filter( "snake_case", - |value: &tera::Value, _: &_| -> tera::Result { - let input = from_value::(value.clone())?; - let mut iter = input.chars(); - + |value: &str, _: tera::Kwargs, _: &tera::State| -> String { + let mut iter = value.chars(); let mut string = String::new(); if let Some(first) = iter.next() { @@ -255,7 +253,7 @@ fn main() -> anyhow::Result<()> { string.push(char.to_ascii_lowercase()); } } - tera::Result::Ok(to_value(string)?) + string }, );