Skip to content

Commit 1b2b9db

Browse files
author
Gidi
committed
release(parameters): release new version with parameter support for patters
1 parent 86e79fa commit 1b2b9db

File tree

6 files changed

+37
-39
lines changed

6 files changed

+37
-39
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "jg"
3-
version = "0.1.6"
3+
version = "0.1.7"
44
authors = ["Gidi Meir Morris <[email protected]>"]
55
edition = "2018"
66
description = "Jeff Goldblum (jg) is a command-line JSON processor. jg searches for structural patterns in json input and prints each json object that matches the pattern."

src/input/parameter_substitution.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub fn apply_substitution(sources: &Vec<&str>, params: &Vec<&str>) -> Vec<String> {
2-
let mut params = params.iter().peekable();
2+
let mut params = params.iter().peekable();
33
sources
44
.iter()
55
.map(move |&src| {
@@ -11,10 +11,10 @@ pub fn apply_substitution(sources: &Vec<&str>, params: &Vec<&str>) -> Vec<String
1111
} else {
1212
break;
1313
}
14-
};
14+
}
1515
}
1616
src
17-
})
17+
})
1818
.collect()
1919
}
2020

@@ -24,49 +24,46 @@ mod tests {
2424

2525
#[test]
2626
fn substitution_should_return_string_as_is_if_no_parameters_are_present() {
27-
assert_eq!(
28-
vec!["Jeff"],
29-
apply_substitution(&vec!["Jeff"],&vec![])
30-
);
27+
assert_eq!(vec!["Jeff"], apply_substitution(&vec!["Jeff"], &vec![]));
3128
}
3229

3330
#[test]
3431
fn substitution_should_return_string_as_is_if_no_substitution_flag_is_present() {
3532
assert_eq!(
3633
vec!["Jeff"],
37-
apply_substitution(&vec!["Jeff"],&vec!["Goldbloom"])
34+
apply_substitution(&vec!["Jeff"], &vec!["Goldbloom"])
3835
);
3936
}
4037

4138
#[test]
4239
fn substitution_should_replace_a_single_substitution_flag() {
4340
assert_eq!(
4441
vec!["Jeff Goldbloom"],
45-
apply_substitution(&vec!["Jeff {}"],&vec!["Goldbloom"])
42+
apply_substitution(&vec!["Jeff {}"], &vec!["Goldbloom"])
4643
);
4744
}
4845

4946
#[test]
5047
fn substitution_should_replace_multiple_substitution_flag() {
5148
assert_eq!(
5249
vec!["Jeff Goldbloom"],
53-
apply_substitution(&vec!["{} {}"],&vec!["Jeff", "Goldbloom"])
50+
apply_substitution(&vec!["{} {}"], &vec!["Jeff", "Goldbloom"])
5451
);
5552
}
5653

5754
#[test]
5855
fn substitution_leave_substitution_flag_untouched_if_tere_are_no_more_parameters() {
5956
assert_eq!(
6057
vec!["Jeff {}"],
61-
apply_substitution(&vec!["{} {}"],&vec!["Jeff"])
58+
apply_substitution(&vec!["{} {}"], &vec!["Jeff"])
6259
);
6360
}
6461

6562
#[test]
6663
fn substitution_should_replace_across_multiple_sources() {
6764
assert_eq!(
68-
vec!["Jeff","Goldbloom"],
69-
apply_substitution(&vec!["{}","{}"],&vec!["Jeff", "Goldbloom"])
65+
vec!["Jeff", "Goldbloom"],
66+
apply_substitution(&vec!["{}", "{}"], &vec!["Jeff", "Goldbloom"])
7067
);
7168
}
7269
}

src/lib.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ pub mod input;
88
mod selection;
99

1010
pub fn json_grep(config: input::Config) -> Result<(), Option<String>> {
11-
1211
let lens_patterns = match config.params {
13-
Some(ref params) => input::parameter_substitution::apply_substitution(
14-
&config.matchers,
15-
params
16-
),
17-
None => config.matchers.iter().map(|s| s.to_string()).collect()
12+
Some(ref params) => {
13+
input::parameter_substitution::apply_substitution(&config.matchers, params)
14+
}
15+
None => config.matchers.iter().map(|s| s.to_string()).collect(),
1816
};
19-
17+
2018
let matched_filters: Result<Vec<_>, String> = lens_patterns
2119
.iter()
2220
.map(|pattern| input::in_configured_case(pattern, &config))

src/main.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,11 @@ fn main() {
101101
})
102102
.expect("No matcher pattern has been specified");
103103

104-
105104
let config = jg::input::Config {
106105
matchers: matched_filters,
107-
params: matches.values_of("params").map(|values| values.collect::<Vec<_>>()),
106+
params: matches
107+
.values_of("params")
108+
.map(|values| values.collect::<Vec<_>>()),
108109
input: matches.value_of("file"),
109110
print_only_count: matches.is_present("count"),
110111
highlight_matches: match (matches.value_of("colour"), stdout_isatty()) {

tests/parameters.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,18 @@ mod parameters {
3131
cmd.arg("--params").arg(r#"mp4"#);
3232
cmd.arg("--params").arg(r#"aac"#);
3333
let mut stdin_cmd = cmd.with_stdin();
34-
let mut assert_cmd = stdin_cmd.buffer(r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
34+
let mut assert_cmd = stdin_cmd.buffer(
35+
r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
3536
{"audio":{"format":"aac"}}
3637
{"audio":{"format":"{}"}}
37-
"#);
38+
"#,
39+
);
3840

39-
assert_cmd
40-
.assert()
41-
.success()
42-
.stdout(r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
41+
assert_cmd.assert().success().stdout(
42+
r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
4343
{"audio":{"format":"aac"}}
44-
"#);
44+
"#,
45+
);
4546
}
4647

4748
#[test]
@@ -52,18 +53,19 @@ mod parameters {
5253
cmd.arg("-e").arg(r#".video.mimes[*="{}"]"#);
5354
cmd.arg("-e").arg(r#".audio{"format"*:"{}"}"#);
5455
cmd.arg("--params").arg(r#"mp4"#);
55-
56+
5657
let mut stdin_cmd = cmd.with_stdin();
57-
let mut assert_cmd = stdin_cmd.buffer(r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
58+
let mut assert_cmd = stdin_cmd.buffer(
59+
r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
5860
{"audio":{"format":"aac"}}
5961
{"audio":{"format":"---{}---"}}
60-
"#);
62+
"#,
63+
);
6164

62-
assert_cmd
63-
.assert()
64-
.success()
65-
.stdout(r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
65+
assert_cmd.assert().success().stdout(
66+
r#"{"video":{"mimes":["audio/mp4","image/jpg","image/gif"]}}
6667
{"audio":{"format":"---{}---"}}
67-
"#);
68+
"#,
69+
);
6870
}
6971
}

0 commit comments

Comments
 (0)