Remove string_template
All checks were successful
lint/clippy Checking for common mistakes and opportunities for code improvement
build/linux Build for the target platform: linux
build/msvc Build for the target platform: msvc

Use minijinja for all templates
This commit is contained in:
Lucas Schwiderski 2024-07-10 18:41:38 +02:00
parent 94b64b4619
commit 0f14834e2d
Signed by: lucas
GPG key ID: AA12679AAA6DF4D8
4 changed files with 18 additions and 12 deletions

1
Cargo.lock generated
View file

@ -910,7 +910,6 @@ dependencies = [
"sdk",
"serde",
"serde_sjson",
"string_template",
"strip-ansi-escapes",
"time",
"tokio",

View file

@ -29,7 +29,6 @@ path-slash = "0.2.1"
sdk = { path = "../../lib/sdk", version = "*" }
serde = { version = "1.0.152", features = ["derive", "rc"] }
serde_sjson = { path = "../../lib/serde_sjson", version = "*" }
string_template = "0.2.1"
strip-ansi-escapes = "0.2.0"
time = { version = "0.3.20", features = ["serde", "serde-well-known", "local-offset"] }
tokio = { version = "1.23.0", features = ["rt", "fs", "tracing", "sync"] }

View file

@ -17,7 +17,7 @@ local require_store = {}
-- This token is treated as a string template and filled by DTMM during deployment.
-- This allows hiding unsafe I/O functions behind a setting.
-- It's also a valid table definition, thereby degrading gracefully when not replaced.
-- When not replaced, it's also a valid table definition, thereby degrading gracefully.
local is_io_enabled = {{ is_io_enabled }} -- luacheck: ignore 113
local lua_libs = {
debug = debug,
@ -207,3 +207,5 @@ function init()
Main:init()
end
-- vim: ft=lua

View file

@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::io::{Cursor, ErrorKind};
use std::path::{Path, PathBuf};
use std::str::FromStr;
@ -16,7 +15,6 @@ use sdk::{
Bundle, BundleDatabase, BundleFile, BundleFileType, BundleFileVariant, FromBinary, ToBinary,
};
use serde::{Deserialize, Serialize};
use string_template::Template;
use time::OffsetDateTime;
use tokio::fs::{self, DirEntry};
use tokio::io::AsyncWriteExt;
@ -572,12 +570,17 @@ async fn patch_boot_bundle(state: Arc<ActionState>) -> Result<Vec<Bundle>> {
let span = tracing::debug_span!("Importing mod main script");
let _enter = span.enter();
let is_io_enabled = format!("{}", state.is_io_enabled);
let mut data = HashMap::new();
data.insert("is_io_enabled", is_io_enabled.as_str());
let mut env = Environment::new();
env.add_template("mod_main.lua", include_str!("../../assets/mod_main.lua.j2"))
.wrap_err("Failed to compile template for `mod_main.lua`")?;
let tmpl = env
.get_template("mod_main.lua")
.wrap_err("Failed to get template `mod_main.lua`")?;
let lua = tmpl
.render(minijinja::context!(is_io_enabled => if state.is_io_enabled { "true" } else {"false"}))
.wrap_err("Failed to render template `mod_main.lua`")?;
let tmpl = include_str!("../../assets/mod_main.lua");
let lua = Template::new(tmpl).render(&data);
tracing::trace!("Main script rendered:\n===========\n{}\n=============", lua);
let file =
lua::compile(MOD_BOOT_SCRIPT, lua).wrap_err("Failed to compile mod main Lua file")?;
@ -707,7 +710,7 @@ pub(crate) async fn deploy_mods(state: ActionState) -> Result<()> {
},
async {
let path = state.game_dir.join(DEPLOYMENT_DATA_PATH);
match read_sjson_file::<_, DeploymentData>(path).await {
match read_sjson_file::<_, DeploymentData>(&path).await {
Ok(data) => Ok(Some(data)),
Err(err) => {
if let Some(err) = err.downcast_ref::<std::io::Error>()
@ -715,7 +718,10 @@ pub(crate) async fn deploy_mods(state: ActionState) -> Result<()> {
{
Ok(None)
} else {
Err(err).wrap_err("Failed to read deployment data")
Err(err).wrap_err(format!(
"Failed to read deployment data from: {}",
path.display()
))
}
}
}