Skip to content

Commit c1c5e9d

Browse files
committed
update template plugin to support custom template
1 parent 1f513e0 commit c1c5e9d

File tree

10 files changed

+104
-5
lines changed

10 files changed

+104
-5
lines changed

crates/template/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,15 @@ pub fn render(
170170
}
171171
}
172172

173+
pub fn render_custom(
174+
template_content: &str,
175+
ctx: &serde_json::Map<String, serde_json::Value>,
176+
) -> Result<String, crate::Error> {
177+
let env = get_environment();
178+
let tpl = env.template_from_str(template_content)?;
179+
tpl.render(ctx).map_err(Into::into)
180+
}
181+
173182
#[cfg(test)]
174183
mod tests {
175184
use super::*;

plugins/template/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const COMMANDS: &[&str] = &["render"];
1+
const COMMANDS: &[&str] = &["render", "render_custom"];
22

33
fn main() {
44
tauri_plugin::Builder::new(COMMANDS).build();

plugins/template/js/bindings.gen.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ async render(name: Template, ctx: Partial<{ [key in string]: JsonValue }>) : Pro
1414
if(e instanceof Error) throw e;
1515
else return { status: "error", error: e as any };
1616
}
17+
},
18+
async renderCustom(templateContent: string, ctx: Partial<{ [key in string]: JsonValue }>) : Promise<Result<string, string>> {
19+
try {
20+
return { status: "ok", data: await TAURI_INVOKE("plugin:template|render_custom", { templateContent, ctx }) };
21+
} catch (e) {
22+
if(e instanceof Error) throw e;
23+
else return { status: "error", error: e as any };
24+
}
1725
}
1826
}
1927

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Automatically generated - DO NOT EDIT!
2+
3+
"$schema" = "../../schemas/schema.json"
4+
5+
[[permission]]
6+
identifier = "allow-render-custom"
7+
description = "Enables the render_custom command without any pre-configured scope."
8+
commands.allow = ["render_custom"]
9+
10+
[[permission]]
11+
identifier = "deny-render-custom"
12+
description = "Denies the render_custom command without any pre-configured scope."
13+
commands.deny = ["render_custom"]

plugins/template/permissions/autogenerated/reference.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Default permissions for the plugin
55
#### This default permission set includes the following:
66

77
- `allow-render`
8+
- `allow-render-custom`
89

910
## Permission Table
1011

@@ -38,6 +39,32 @@ Enables the render command without any pre-configured scope.
3839

3940
Denies the render command without any pre-configured scope.
4041

42+
</td>
43+
</tr>
44+
45+
<tr>
46+
<td>
47+
48+
`template:allow-render-custom`
49+
50+
</td>
51+
<td>
52+
53+
Enables the render_custom command without any pre-configured scope.
54+
55+
</td>
56+
</tr>
57+
58+
<tr>
59+
<td>
60+
61+
`template:deny-render-custom`
62+
63+
</td>
64+
<td>
65+
66+
Denies the render_custom command without any pre-configured scope.
67+
4168
</td>
4269
</tr>
4370
</table>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[default]
22
description = "Default permissions for the plugin"
3-
permissions = ["allow-render"]
3+
permissions = ["allow-render", "allow-render-custom"]

plugins/template/permissions/schemas/schema.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,22 @@
307307
"markdownDescription": "Denies the render command without any pre-configured scope."
308308
},
309309
{
310-
"description": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-render`",
310+
"description": "Enables the render_custom command without any pre-configured scope.",
311+
"type": "string",
312+
"const": "allow-render-custom",
313+
"markdownDescription": "Enables the render_custom command without any pre-configured scope."
314+
},
315+
{
316+
"description": "Denies the render_custom command without any pre-configured scope.",
317+
"type": "string",
318+
"const": "deny-render-custom",
319+
"markdownDescription": "Denies the render_custom command without any pre-configured scope."
320+
},
321+
{
322+
"description": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-render`\n- `allow-render-custom`",
311323
"type": "string",
312324
"const": "default",
313-
"markdownDescription": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-render`"
325+
"markdownDescription": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-render`\n- `allow-render-custom`"
314326
}
315327
]
316328
}

plugins/template/src/commands.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,13 @@ pub async fn render<R: tauri::Runtime>(
99
) -> Result<String, String> {
1010
app.render(name, ctx)
1111
}
12+
13+
#[tauri::command]
14+
#[specta::specta]
15+
pub async fn render_custom<R: tauri::Runtime>(
16+
app: tauri::AppHandle<R>,
17+
template_content: String,
18+
ctx: serde_json::Map<String, serde_json::Value>,
19+
) -> Result<String, String> {
20+
app.render_custom(&template_content, ctx)
21+
}

plugins/template/src/ext.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ pub trait TemplatePluginExt<R: tauri::Runtime> {
44
name: hypr_template::Template,
55
ctx: serde_json::Map<String, serde_json::Value>,
66
) -> Result<String, String>;
7+
8+
fn render_custom(
9+
&self,
10+
template_content: &str,
11+
ctx: serde_json::Map<String, serde_json::Value>,
12+
) -> Result<String, String>;
713
}
814

915
impl<R: tauri::Runtime, T: tauri::Manager<R>> TemplatePluginExt<R> for T {
@@ -17,4 +23,15 @@ impl<R: tauri::Runtime, T: tauri::Manager<R>> TemplatePluginExt<R> for T {
1723
.map(|s| s.trim().to_string())
1824
.map_err(|e| e.to_string())
1925
}
26+
27+
#[tracing::instrument(skip_all)]
28+
fn render_custom(
29+
&self,
30+
template_content: &str,
31+
ctx: serde_json::Map<String, serde_json::Value>,
32+
) -> Result<String, String> {
33+
hypr_template::render_custom(template_content, &ctx)
34+
.map(|s| s.trim().to_string())
35+
.map_err(|e| e.to_string())
36+
}
2037
}

plugins/template/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ const PLUGIN_NAME: &str = "template";
1111
fn make_specta_builder<R: tauri::Runtime>() -> tauri_specta::Builder<R> {
1212
tauri_specta::Builder::<R>::new()
1313
.plugin_name(PLUGIN_NAME)
14-
.commands(tauri_specta::collect_commands![commands::render::<Wry>,])
14+
.commands(tauri_specta::collect_commands![
15+
commands::render::<Wry>,
16+
commands::render_custom::<Wry>,
17+
])
1518
.typ::<hypr_gbnf::Grammar>()
1619
.error_handling(tauri_specta::ErrorHandlingMode::Result)
1720
}

0 commit comments

Comments
 (0)