Skip to content

Commit

Permalink
feat: add --simplify flag (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinohmann authored Jun 2, 2024
1 parent 331caee commit 9cb5a24
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/bin/dts/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ pub struct InputOptions {
/// not a directory.
#[arg(short = 'C', long, help_heading = "Input Options")]
pub continue_on_error: bool,

/// Simplify input if the encoding supports it.
///
/// Some encodings like HCL support partial expression evaluation, where an expression like
/// `1 + 2` can be evaluated to `3`. This flag controls if input simplifications like this
/// should be performed or not.
#[arg(long, help_heading = "Input Options")]
pub simplify: bool,
}

impl From<&InputOptions> for DeserializeOptions {
Expand All @@ -131,6 +139,7 @@ impl From<&InputOptions> for DeserializeOptions {
csv_without_headers: opts.csv_without_headers,
csv_delimiter: opts.csv_input_delimiter,
text_split_pattern: opts.text_split_pattern.clone(),
simplify: opts.simplify,
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! encodings into a `Value`.
use crate::{key::expand_keys, parsers::gron, Encoding, Result};
use hcl::eval::Evaluate;
use regex::Regex;
use serde::Deserialize;
use serde_json::{Map, Value};
Expand All @@ -21,6 +22,8 @@ pub struct DeserializeOptions {
pub csv_delimiter: Option<u8>,
/// Optional regex pattern to split text input at.
pub text_split_pattern: Option<Regex>,
/// Simplify input if the encoding supports it.
pub simplify: bool,
}

impl DeserializeOptions {
Expand Down Expand Up @@ -82,6 +85,12 @@ impl DeserializerBuilder {
self
}

/// Simplify input if the encoding supports it.
pub fn simplifiy(&mut self, yes: bool) -> &mut Self {
self.opts.simplify = yes;
self
}

/// Builds the `Deserializer` for the given reader.
pub fn build<R>(&self, reader: R) -> Deserializer<R>
where
Expand Down Expand Up @@ -255,7 +264,16 @@ where
}

fn deserialize_hcl(&mut self) -> Result<Value> {
Ok(hcl::from_reader(&mut self.reader)?)
let value = if self.opts.simplify {
let mut body: hcl::Body = hcl::from_reader(&mut self.reader)?;
let ctx = hcl::eval::Context::new();
let _ = body.evaluate_in_place(&ctx);
hcl::from_body(body)?
} else {
hcl::from_reader(&mut self.reader)?
};

Ok(value)
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/math.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a = 1 + 2
b = 3
c = a + b
5 changes: 5 additions & 0 deletions tests/fixtures/math.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"a": "${1 + 2}",
"b": 3,
"c": "${a + b}"
}
5 changes: 5 additions & 0 deletions tests/fixtures/math.simplified.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"a": 3,
"b": 3,
"c": "${a + b}"
}
22 changes: 22 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ fn json_to_hcl_compact() {
.stdout(read("tests/fixtures/example.compact.hcl").unwrap());
}

#[test]
fn hcl_to_json() {
Command::cargo_bin("dts")
.unwrap()
.arg("tests/fixtures/math.hcl")
.args(&["-o", "json"])
.assert()
.success()
.stdout(read("tests/fixtures/math.json").unwrap());
}

#[test]
fn hcl_to_json_simplified() {
Command::cargo_bin("dts")
.unwrap()
.arg("tests/fixtures/math.hcl")
.args(&["-o", "json", "--simplify"])
.assert()
.success()
.stdout(read("tests/fixtures/math.simplified.json").unwrap());
}

#[test]
fn gron_to_json() {
Command::cargo_bin("dts")
Expand Down

0 comments on commit 9cb5a24

Please sign in to comment.