Skip to content

Commit

Permalink
Upgrade handlebars to 5.1 to support chained else expression
Browse files Browse the repository at this point in the history
Also update handlebars_misc_helpers to be compatible with the new
handlebars version.

Refactor RenderErrors to RenderErrorReasons to fix deprecation warnings.
  • Loading branch information
SuperTux88 committed Jan 19, 2024
1 parent a46ed61 commit 0ba2d17
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 49 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ clap = { version = "4.0.26", features = ["derive"] }
clap_complete = "4.0.5"
crossterm = "0.25.0"
diff = "0.1.*"
handlebars = "4.*"
handlebars = "5.*"
hostname = "0.3.*"
log = "0.4.*"
maplit = "1.*"
Expand All @@ -37,7 +37,7 @@ scripting = ["handlebars/script_helper"]
watch = ["watchexec", "watchexec-events", "watchexec-filterer-tagged"]

[dependencies.handlebars_misc_helpers]
version = "0.13.*"
version = "0.15.*"
default-features = false
features = ["string", "json"]

Expand Down
69 changes: 42 additions & 27 deletions src/handlebars_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use anyhow::{Context as AnyhowContext, Result};

use handlebars::{Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderError};
use handlebars::{
Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderErrorReason,
};
use toml::value::{Table, Value};

use std::collections::{BTreeMap, BTreeSet};
Expand Down Expand Up @@ -65,7 +67,7 @@ fn eval_condition(handlebars: &Handlebars, variables: &Variables, condition: &st
}

fn math_helper(
h: &Helper<'_, '_>,
h: &Helper<'_>,
_: &Handlebars<'_>,
_: &Context,
_: &mut RenderContext<'_, '_>,
Expand All @@ -81,7 +83,7 @@ fn math_helper(
out.write(
&evalexpr::eval(&expression)
.map_err(|e| {
RenderError::new(format!(
RenderErrorReason::Other(format!(
"Cannot evaluate expression {} because {}",
expression, e
))
Expand All @@ -92,7 +94,7 @@ fn math_helper(
}

fn include_template_helper(
h: &Helper<'_, '_>,
h: &Helper<'_>,
handlebars: &Handlebars<'_>,
ctx: &Context,
rc: &mut RenderContext<'_, '_>,
Expand All @@ -101,27 +103,31 @@ fn include_template_helper(
let mut params = h.params().iter();
let path = params
.next()
.ok_or_else(|| RenderError::new("include_template: No path given"))?
.ok_or(RenderErrorReason::ParamNotFoundForIndex(
"include_template",
0,
))?
.render();
if params.next().is_some() {
return Err(RenderError::new(
"include_template: More than one parameter given",
));
return Err(RenderErrorReason::Other(
"include_template: More than one parameter given".to_owned(),
)
.into());
}

let included_file = std::fs::read_to_string(path)
.map_err(|e| RenderError::from_error("include_template", e))?;
let included_file =
std::fs::read_to_string(path).map_err(|e| RenderErrorReason::NestedError(Box::new(e)))?;
let rendered_file = handlebars
.render_template_with_context(&included_file, rc.context().as_deref().unwrap_or(ctx))
.map_err(|e| RenderError::from_error("include_template", e))?;
.map_err(|e| RenderErrorReason::NestedError(Box::new(e)))?;

out.write(&rendered_file)?;

Ok(())
}

fn is_executable_helper(
h: &Helper<'_, '_>,
h: &Helper<'_>,
_: &Handlebars<'_>,
_: &Context,
_: &mut RenderContext<'_, '_>,
Expand All @@ -130,16 +136,17 @@ fn is_executable_helper(
let mut params = h.params().iter();
let executable = params
.next()
.ok_or_else(|| RenderError::new("is_executable: No executable name given"))?
.ok_or(RenderErrorReason::ParamNotFoundForIndex("is_executable", 0))?
.render();
if params.next().is_some() {
return Err(RenderError::new(
"is_executable: More than one parameter given",
));
return Err(RenderErrorReason::Other(
"is_executable: More than one parameter given".to_owned(),
)
.into());
}

let status =
is_executable(&executable).map_err(|e| RenderError::from_error("is_executable", e))?;
is_executable(&executable).map_err(|e| RenderErrorReason::NestedError(Box::new(e)))?;
if status {
out.write("true")?;
}
Expand All @@ -149,7 +156,7 @@ fn is_executable_helper(
}

fn command_success_helper(
h: &Helper<'_, '_>,
h: &Helper<'_>,
_: &Handlebars<'_>,
_: &Context,
_: &mut RenderContext<'_, '_>,
Expand All @@ -158,12 +165,16 @@ fn command_success_helper(
let mut params = h.params().iter();
let command = params
.next()
.ok_or_else(|| RenderError::new("command_success: No executable name given"))?
.ok_or(RenderErrorReason::ParamNotFoundForIndex(
"command_success",
0,
))?
.render();
if params.next().is_some() {
return Err(RenderError::new(
"command_success: More than one parameter given",
));
return Err(RenderErrorReason::Other(
"command_success: More than one parameter given".to_owned(),
)
.into());
}

let status = os_shell()
Expand All @@ -182,7 +193,7 @@ fn command_success_helper(
}

fn command_output_helper(
h: &Helper<'_, '_>,
h: &Helper<'_>,
_: &Handlebars<'_>,
_: &Context,
_: &mut RenderContext<'_, '_>,
Expand All @@ -191,12 +202,16 @@ fn command_output_helper(
let mut params = h.params().iter();
let command = params
.next()
.ok_or_else(|| RenderError::new("command_success: No executable name given"))?
.ok_or(RenderErrorReason::ParamNotFoundForIndex(
"command_success",
0,
))?
.render();
if params.next().is_some() {
return Err(RenderError::new(
"command_success: More than one parameter given",
));
return Err(RenderErrorReason::Other(
"command_success: More than one parameter given".to_owned(),
)
.into());
}

let output = os_shell()
Expand Down

0 comments on commit 0ba2d17

Please sign in to comment.