aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Guillaume Gomez <guillaume1.gomez@gmail.com>2022-04-11 16:16:36 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2022-04-21 10:08:42 +0200
commitd9fcd348d121b6dd0568314b28ec187f11e32c77 (patch)
treee5bf64890cba10eeb6150033d3f43e35600b1a9e
parenta51433677d98ebdba2008315b6d79565530fdf7e (diff)
downloadaskama-d9fcd348d121b6dd0568314b28ec187f11e32c77.tar.gz
askama-d9fcd348d121b6dd0568314b28ec187f11e32c77.tar.bz2
askama-d9fcd348d121b6dd0568314b28ec187f11e32c77.zip
Add config option to derive macro so we can specify config file location
Diffstat (limited to '')
-rw-r--r--askama_shared/src/generator.rs9
-rw-r--r--askama_shared/src/input.rs1
-rw-r--r--askama_shared/src/lib.rs10
3 files changed, 17 insertions, 3 deletions
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs
index a09631c..9aec1d6 100644
--- a/askama_shared/src/generator.rs
+++ b/askama_shared/src/generator.rs
@@ -29,7 +29,7 @@ pub fn derive_template(input: TokenStream) -> TokenStream {
/// value as passed to the `template()` attribute.
fn build_template(ast: &syn::DeriveInput) -> Result<String, CompileError> {
let template_args = TemplateArgs::new(ast)?;
- let config_toml = read_config_file()?;
+ let config_toml = read_config_file(&template_args.config_path)?;
let config = Config::new(&config_toml)?;
let input = TemplateInput::new(ast, &config, template_args)?;
let source: String = match input.source {
@@ -82,6 +82,7 @@ pub(crate) struct TemplateArgs {
pub(crate) escaping: Option<String>,
pub(crate) ext: Option<String>,
pub(crate) syntax: Option<String>,
+ pub(crate) config_path: Option<String>,
}
impl TemplateArgs {
@@ -174,6 +175,12 @@ impl TemplateArgs {
} else {
return Err("syntax value must be string literal".into());
}
+ } else if ident == "config" {
+ if let syn::Lit::Str(ref s) = pair.lit {
+ args.config_path = Some(s.value())
+ } else {
+ return Err("config value must be string literal".into());
+ }
} else {
return Err(format!("unsupported attribute key {:?} found", ident).into());
}
diff --git a/askama_shared/src/input.rs b/askama_shared/src/input.rs
index 6c49156..350fc01 100644
--- a/askama_shared/src/input.rs
+++ b/askama_shared/src/input.rs
@@ -35,6 +35,7 @@ impl TemplateInput<'_> {
escaping,
ext,
syntax,
+ ..
} = args;
// Validate the `source` and `ext` value together, since they are
diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs
index 2aa7114..07331bc 100644
--- a/askama_shared/src/lib.rs
+++ b/askama_shared/src/lib.rs
@@ -327,12 +327,18 @@ struct RawEscaper<'a> {
extensions: Vec<&'a str>,
}
-fn read_config_file() -> std::result::Result<String, CompileError> {
+fn read_config_file(config_path: &Option<String>) -> std::result::Result<String, CompileError> {
let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
- let filename = root.join(CONFIG_FILE_NAME);
+ let filename = match config_path {
+ Some(config_path) => root.join(config_path),
+ None => root.join(CONFIG_FILE_NAME),
+ };
+
if filename.exists() {
fs::read_to_string(&filename)
.map_err(|_| format!("unable to read {:?}", filename.to_str().unwrap()).into())
+ } else if config_path.is_some() {
+ Err(format!("`{}` does not exist", root.display()).into())
} else {
Ok("".to_string())
}