| 1 |
// SPDX-FileCopyrightText: Peter Pentchev <[email protected]> |
| 2 |
// SPDX-License-Identifier: BSD-2-Clause |
| 3 |
//! Parse a test template. |
| 4 |
//! |
| 5 |
//! Define the hierarchy of the test data JSON file and provide |
| 6 |
//! a function to parse and deserialize it. |
| 7 |
|
| 8 |
use std::fs; |
| 9 |
|
| 10 |
use anyhow::Context as _; |
| 11 |
use handlebars::{Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderError}; |
| 12 |
use tracing::debug; |
| 13 |
|
| 14 |
use crate::defs::{Config, Error, TestData, TEMPLATE_NAME}; |
| 15 |
|
| 16 |
/// Output a boolean value as a Perl 0 or 1 constant. |
| 17 |
fn helper_perlbool( |
| 18 |
helper: &Helper<'_, '_>, |
| 19 |
_: &Handlebars<'_>, |
| 20 |
_: &Context, |
| 21 |
_: &mut RenderContext<'_, '_>, |
| 22 |
out: &mut (dyn Output), |
| 23 |
) -> HelperResult { |
| 24 |
let value = if helper |
| 25 |
.param(0) |
| 26 |
.ok_or_else(|| { |
| 27 |
RenderError::new(format!( |
| 28 |
"Internal error: perlbool: no parameter in {helper:?}" |
| 29 |
)) |
| 30 |
})? |
| 31 |
.value() |
| 32 |
.as_bool() |
| 33 |
.ok_or_else(|| { |
| 34 |
RenderError::new(format!( |
| 35 |
"Internal error: perlbool: not a boolean parameter in {helper:?}" |
| 36 |
)) |
| 37 |
})? { |
| 38 |
"1" |
| 39 |
} else { |
| 40 |
"0" |
| 41 |
}; |
| 42 |
out.write(value)?; |
| 43 |
Ok(()) |
| 44 |
} |
| 45 |
|
| 46 |
/// Output a boolean value as a Python "True" or "False" constant. |
| 47 |
fn helper_pybool( |
| 48 |
helper: &Helper<'_, '_>, |
| 49 |
_: &Handlebars<'_>, |
| 50 |
_: &Context, |
| 51 |
_: &mut RenderContext<'_, '_>, |
| 52 |
out: &mut (dyn Output), |
| 53 |
) -> HelperResult { |
| 54 |
let value = if helper |
| 55 |
.param(0) |
| 56 |
.ok_or_else(|| { |
| 57 |
RenderError::new(format!( |
| 58 |
"Internal error: pybool: no parameter in {helper:?}" |
| 59 |
)) |
| 60 |
})? |
| 61 |
.value() |
| 62 |
.as_bool() |
| 63 |
.ok_or_else(|| { |
| 64 |
RenderError::new(format!( |
| 65 |
"Internal error: perlbool: not a boolean parameter in {helper:?}" |
| 66 |
)) |
| 67 |
})? { |
| 68 |
"True" |
| 69 |
} else { |
| 70 |
"False" |
| 71 |
}; |
| 72 |
out.write(value)?; |
| 73 |
Ok(()) |
| 74 |
} |
| 75 |
|
| 76 |
/// Parse a Handlebars template file. |
| 77 |
/// |
| 78 |
/// # Errors |
| 79 |
/// |
| 80 |
/// Fails on template parsing errors from |
| 81 |
/// [`handlebars::Handlebars::register_template_file`]. |
| 82 |
#[allow(clippy::module_name_repetitions)] |
| 83 |
#[inline] |
| 84 |
pub fn parse_template(cfg: &Config) -> Result<Handlebars<'_>, Error> { |
| 85 |
debug!( |
| 86 |
"Parsing the {template} template", |
| 87 |
template = cfg.template_path |
| 88 |
); |
| 89 |
let mut renderer = Handlebars::new(); |
| 90 |
renderer.set_strict_mode(true); |
| 91 |
renderer.register_escape_fn(ToOwned::to_owned); |
| 92 |
renderer.register_helper("perlbool", Box::new(helper_perlbool)); |
| 93 |
renderer.register_helper("pybool", Box::new(helper_pybool)); |
| 94 |
renderer |
| 95 |
.register_template_file(TEMPLATE_NAME, &cfg.template_path) |
| 96 |
.map_err(Error::ParseTemplate)?; |
| 97 |
Ok(renderer) |
| 98 |
} |
| 99 |
|
| 100 |
/// Parse and deserialize a test data file. |
| 101 |
/// |
| 102 |
/// # Errors |
| 103 |
/// |
| 104 |
/// Fails on filesystem errors and parse errors, including an invalid version |
| 105 |
/// specified for the test data format. |
| 106 |
#[allow(clippy::module_name_repetitions)] |
| 107 |
#[inline] |
| 108 |
pub fn parse_test_data(cfg: &Config) -> Result<TestData, Error> { |
| 109 |
debug!("Parsing the {data} test data file", data = cfg.data_path); |
| 110 |
let contents = fs::read_to_string(&cfg.data_path) |
| 111 |
.with_context(|| format!("Could not read the {data} input file", data = cfg.data_path)) |
| 112 |
.map_err(Error::IO)?; |
| 113 |
|
| 114 |
let fver = typed_format_version::get_version_from_str(&contents, serde_json::from_str) |
| 115 |
.map_err(Error::VersionLoad)?; |
| 116 |
if fver.as_version_tuple() != (0, 1) { |
| 117 |
return Err(Error::VersionUnknown(fver.major(), fver.minor())); |
| 118 |
} |
| 119 |
|
| 120 |
// Now deserialize the data |
| 121 |
serde_json::from_str(&contents).map_err(Error::ParseJson) |
| 122 |
} |