refactor script format
This commit is contained in:
parent
97a41578a1
commit
2013b32f94
4 changed files with 116 additions and 102 deletions
|
@ -11,11 +11,11 @@ structopt = "0.3.23"
|
|||
tera = "1.12.1"
|
||||
|
||||
[dependencies.minify-html]
|
||||
version = "0.6.8"
|
||||
version = "0.8.0"
|
||||
features = ["js-esbuild"]
|
||||
|
||||
[dependencies.mlua]
|
||||
version = "0.6.3"
|
||||
version = "0.7.3"
|
||||
features = ["luajit", "macros", "serialize"]
|
||||
|
||||
[dependencies.serde]
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<title>Plan</title>
|
||||
|
@ -30,8 +31,9 @@
|
|||
border-collapse: collapse;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
{% for i in range(end=repeat) %}
|
||||
<table>
|
||||
<thead>
|
||||
|
@ -103,5 +105,6 @@
|
|||
{% if i != repeat - 1 %}
|
||||
<hr />
|
||||
{% endif %}{% endfor %}
|
||||
</body>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use anyhow::Context;
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
|
||||
|
@ -13,12 +12,7 @@ struct Opt {
|
|||
fn main() -> anyhow::Result<()> {
|
||||
let opt = Opt::from_args();
|
||||
|
||||
let s_data = script::run_buildscript(opt.infile)?;
|
||||
let path = s_data.outfile.clone();
|
||||
let rendered = renderer::render(s_data)?;
|
||||
|
||||
std::fs::write(path, rendered)
|
||||
.context("failed to write outfile")?;
|
||||
script::run_buildscript(opt.infile)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,34 +1,51 @@
|
|||
use mlua::{Lua, LuaSerdeExt};
|
||||
use mlua::{DeserializeOptions, Lua, LuaSerdeExt, Table, Value};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
use std::{fs, path::PathBuf, sync::Arc};
|
||||
|
||||
pub fn run_buildscript(infile: PathBuf) -> anyhow::Result<ScriptData> {
|
||||
use crate::renderer;
|
||||
|
||||
pub fn run_buildscript(infile: PathBuf) -> anyhow::Result<()> {
|
||||
let script = std::fs::read(infile)?;
|
||||
|
||||
// required to allow C lua libs
|
||||
let lua = unsafe { Lua::unsafe_new() };
|
||||
|
||||
lua.globals()
|
||||
.set("render", lua.create_function(lua_render)?)?;
|
||||
|
||||
lua.load(&script).exec()?;
|
||||
|
||||
let sdata = lua.from_value_with(
|
||||
mlua::Value::Table(lua.globals()),
|
||||
mlua::DeserializeOptions::default()
|
||||
.deny_recursive_tables(false)
|
||||
.deny_unsupported_types(false),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Ok(sdata)
|
||||
fn lua_render<'lua>(lua: &'lua Lua, args: Table<'lua>) -> Result<(), mlua::Error> {
|
||||
let args =
|
||||
lua.from_value_with::<ScriptData>(Value::Table(args), DeserializeOptions::default())?;
|
||||
let path = args.outfile.clone();
|
||||
|
||||
let rendered =
|
||||
renderer::render(args).map_err(|e| mlua::Error::ExternalError(Arc::from(Box::from(e))))?;
|
||||
|
||||
fs::write(path, rendered)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "PascalCase")]
|
||||
pub struct ScriptData {
|
||||
pub days: Days,
|
||||
pub outfile: PathBuf,
|
||||
#[serde(default)]
|
||||
pub locale: Locale,
|
||||
#[serde(default = "default_repeat_count")]
|
||||
pub repeat_count: usize,
|
||||
pub times: Vec<String>,
|
||||
}
|
||||
|
||||
fn default_repeat_count() -> usize {
|
||||
1
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Days {
|
||||
pub mo: Vec<Class>,
|
||||
|
|
Loading…
Reference in a new issue