Skip to content

Commit

Permalink
Strip out comments in config files before passing to shellexpand (#38)
Browse files Browse the repository at this point in the history
Applied the fix to all config files with comment support.

The one downside is, that I had to repeat the same code in each function. That is due to `#[proc_macro]` crate rules: according to Rust's restrictions, these crates can only export items that are procedural macros themselves.

Fixes #37
  • Loading branch information
mre authored Feb 12, 2024
1 parent 963523d commit 6ba43c5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ fn build_toml_branch() -> proc_macro2::TokenStream {
#[cfg(feature = "toml")]
let toml_branch = quote! { ::twelf::Layer::Toml(filepath) => {
let file_content = std::fs::read_to_string(filepath)?;
// Strip out comments (lines starting with #)
let file_content = file_content.lines().filter(|line| !line.trim().starts_with("#")).collect::<Vec<_>>().join("\n");

let content = ::twelf::reexports::shellexpand::env(&file_content)?;
(::twelf::reexports::toml::from_str(&content)?,None)
}, };
Expand All @@ -310,6 +313,8 @@ fn build_yaml_branch() -> proc_macro2::TokenStream {
#[cfg(feature = "yaml")]
let yaml_branch = quote! { ::twelf::Layer::Yaml(filepath) => {
let file_content = std::fs::read_to_string(filepath)?;
// Strip out comments (lines starting with #)
let file_content = file_content.lines().filter(|line| !line.trim().starts_with("#")).collect::<Vec<_>>().join("\n");
let content = ::twelf::reexports::shellexpand::env(&file_content)?;
(::twelf::reexports::serde_yaml::from_str(&content)?,None)
}, };
Expand All @@ -322,6 +327,9 @@ fn build_dhall_branch() -> proc_macro2::TokenStream {
#[cfg(feature = "dhall")]
let dhall_branch = quote! { ::twelf::Layer::Dhall(filepath) => {
let file_content = std::fs::read_to_string(filepath)?;
// Strip out comments (lines starting with --)
let file_content = file_content.lines().filter(|line| !line.trim().starts_with("--")).collect::<Vec<_>>().join("\n");

let content = ::twelf::reexports::shellexpand::env(&file_content)?;
(::twelf::reexports::serde_dhall::from_str(&content).parse()?,None)
}, };
Expand All @@ -334,6 +342,8 @@ fn build_dhall_branch() -> proc_macro2::TokenStream {
fn build_ini_branch(opt_struct_name: &Ident, struct_gen: &Generics) -> proc_macro2::TokenStream {
quote! { ::twelf::Layer::Ini(filepath) => {
let file_content = std::fs::read_to_string(filepath)?;
// Strip out comments (lines starting with ;)
let file_content = file_content.lines().filter(|line| !line.trim().starts_with(";")).collect::<Vec<_>>().join("\n");
let content = ::twelf::reexports::shellexpand::env(&file_content)?;
let tmp_cfg: #opt_struct_name #struct_gen = ::twelf::reexports::serde_ini::from_str(&content)?;
(::twelf::reexports::serde_json::to_value(tmp_cfg)?,None)
Expand Down
1 change: 1 addition & 0 deletions twelf/tests/fixtures/test.dhall
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
test = "from file",
another = 25,
-- Single-line comment ${FOO}
elements = {
key = "value",
key2 = "value2"
Expand Down
1 change: 1 addition & 0 deletions twelf/tests/fixtures/test.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test=from file
another=25
; This is a comment ${FOO}

[elements]
key=value
Expand Down
1 change: 1 addition & 0 deletions twelf/tests/fixtures/test.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# private_key = "${FOO}"
test = 'from file'
another = 25

Expand Down
1 change: 1 addition & 0 deletions twelf/tests/fixtures/test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
test: from file
another: 25
# This is a comment ${FOO}
elements:
key: value
key2: value2
Expand Down

0 comments on commit 6ba43c5

Please sign in to comment.