Skip to content

Commit

Permalink
feat(jinja): using rust-embed
Browse files Browse the repository at this point in the history
Signed-off-by: DragonBillow <[email protected]>
  • Loading branch information
cathaysia committed Mar 11, 2024
1 parent 7da6331 commit e12e469
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 8 deletions.
100 changes: 100 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ lazy-regex = "3.1.0"
chrono = "0.4.35"
derive = { path = "./derive" }
lazy_static = "1.4.0"
rust-embed = "8.3.0"
22 changes: 14 additions & 8 deletions src/jinja.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ pub use system::*;

use minijinja::Environment;

#[derive(rust_embed::RustEmbed)]
#[folder = "src/jinja/template/"]
struct Asset;

#[derive(Debug, Clone)]
pub struct Generator<'a> {
pub engine: Environment<'a>,
}

impl Generator<'_> {
pub const NIX_EXPRESSION: (&'static str, &'static str) = (
"nix_expression",
include_str!("./jinja/template/nix_expression.nix.j2"),
);
pub const CODELLDB: (&'static str, &'static str) =
("codelldb", include_str!("./jinja/template/codelldb.j2"));
}
Expand All @@ -38,9 +38,15 @@ impl<'a> Generator<'a> {
pub fn new() -> Self {
let mut engine = Environment::new();

engine
.add_template(Self::NIX_EXPRESSION.0, Self::NIX_EXPRESSION.1)
.unwrap();
engine.set_loader(
move |name| match Asset::get(format!("{name}.j2").as_str()) {
Some(file) => match String::from_utf8(file.data.to_vec()) {
Ok(val) => Ok(Some(val)),
Err(_) => unreachable!(),
},
None => Ok(None),
},
);

add_filter!(engine, nixfmt);
add_filter!(engine, to_string);
Expand All @@ -62,7 +68,7 @@ impl<'a> Generator<'a> {
pub fn render(&mut self, ctx: &GeneratorContext) -> anyhow::Result<String> {
Ok(self
.engine
.get_template(Self::NIX_EXPRESSION.0)?
.get_template("nix_expression.nix")?
.render(minijinja::Value::from_serializable(ctx))?)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ async fn get_matched_versoin(
.get_asset_url(&item.publisher.publisher_name, &item.extension_name)
{
Some(url) => {
debug!(url);
let url = generator.render_asset_url(
&url,
&AssetUrlContext::new(
Expand Down

0 comments on commit e12e469

Please sign in to comment.