Skip to content

Commit bbf335c

Browse files
committed
Accept raw jsons as command line parameters
1 parent fba61a3 commit bbf335c

File tree

3 files changed

+198
-13
lines changed

3 files changed

+198
-13
lines changed

Cargo.lock

Lines changed: 142 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ edition = "2018"
99
[dependencies]
1010
serde_json = "1.0.41"
1111
maplit = "1.0.2"
12-
colored = "1.9.0"
12+
colored = "1.9.0"
13+
structopt = "0.3.5"

src/main.rs

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,59 @@ use serde_json::Map;
44
use serde_json::Value;
55
use std::collections::HashMap;
66
use std::collections::HashSet;
7-
use std::env;
7+
use std::fmt;
88
use std::fs;
9+
use std::str::FromStr;
10+
use structopt::StructOpt;
11+
12+
#[derive(Debug)]
13+
struct AppError {
14+
message: String,
15+
}
16+
impl fmt::Display for AppError {
17+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
18+
write!(f, "{}", self.message)
19+
}
20+
}
21+
22+
enum CliOptions {
23+
D,
24+
F,
25+
}
26+
impl FromStr for CliOptions {
27+
type Err = AppError;
28+
fn from_str(s: &str) -> Result<Self, Self::Err> {
29+
match s {
30+
"d" => Ok(CliOptions::D),
31+
"f" => Ok(CliOptions::F),
32+
_ => Err(Self::Err {
33+
message: "BAD option".to_string(),
34+
}),
35+
}
36+
}
37+
}
38+
39+
#[derive(StructOpt)]
40+
struct Cli {
41+
option: CliOptions,
42+
source1: String,
43+
source2: String,
44+
}
945

1046
fn main() {
11-
let args = env::args().collect::<Vec<String>>();
12-
let file1 = &args[1];
13-
let file2 = &args[2];
47+
let args = Cli::from_args();
1448

15-
let data1 =
16-
&fs::read_to_string(file1).expect(&format!("Error occurred while reading {}", file1));
17-
let data2 =
18-
&fs::read_to_string(file2).expect(&format!("Error occurred while reading {}", file2));
49+
let (data1, data2) = match args.option {
50+
CliOptions::D => (args.source1, args.source2),
51+
CliOptions::F => {
52+
(fs::read_to_string(args.source1)
53+
.expect(&format!("Error occurred while reading source1")),
54+
fs::read_to_string(args.source2)
55+
.expect(&format!("Error occurred while reading source2")))
56+
}
57+
};
58+
display_output(compare_jsons(&data1, &data2));
1959

20-
display_output(compare_jsons(data1, data2));
2160
}
2261

2362
fn display_output(result: Mismatch) {
@@ -87,9 +126,12 @@ impl KeyNode {
87126
let nil_key = |key: Option<String>| key.unwrap_or(String::new());
88127
match self {
89128
KeyNode::Nil => keys.push(nil_key(key_from_root)),
90-
KeyNode::Value(a, b) => {
91-
keys.push(format!("{} [ {} :: {} ]", val_key(key_from_root), a.to_string().blue().bold(), b.to_string().cyan().bold()))
92-
}
129+
KeyNode::Value(a, b) => keys.push(format!(
130+
"{} [ {} :: {} ]",
131+
val_key(key_from_root),
132+
a.to_string().blue().bold(),
133+
b.to_string().cyan().bold()
134+
)),
93135
KeyNode::Node(map) => {
94136
for (key, value) in map {
95137
value.absolute_keys(

0 commit comments

Comments
 (0)