Skip to content

Commit 5ea3d7c

Browse files
committed
initial commit
0 parents  commit 5ea3d7c

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
target/
3+
Cargo.lock

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "jsonlint"
3+
version = "1.0.0"
4+
authors = ["Philipp Speck <[email protected]>"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
clap = "3.2.0"
9+
json = "0.12.4"

LICENSE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2023 Typomedia, Philipp Speck
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
init:
2+
rustup update
3+
rustup component add rustfmt
4+
5+
build:
6+
cargo build --release

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# jsonlint
2+
3+
A simple `cli` utility to check the syntax of [JSON](https://www.json.org) files and/or pretty print them.
4+
5+
It's written in Rust, and uses the [json](https://crates.io/crates/json) library.
6+
7+
## USAGE
8+
jsonlint [OPTIONS] <file>
9+
10+
### ARGS
11+
<file> JSON file to parse
12+
13+
### OPTIONS
14+
-h, --help Print help information
15+
-p, --pretty Pretty prints a JSON file
16+
-V, --version Print version information
17+
18+
## Building
19+
20+
make build

src/main.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
extern crate clap;
2+
3+
use clap::{App, Arg};
4+
use std::fs;
5+
6+
fn main() {
7+
let matches = App::new("jsonlint")
8+
.version("1.0.0")
9+
.author("Philipp Speck <[email protected]>")
10+
.about("A clone of jsonlint, written in Rust")
11+
.arg(Arg::new("file")
12+
.required(true)
13+
.help("JSON file to parse")
14+
)
15+
.arg(Arg::new("pretty")
16+
.short('p')
17+
.long("pretty")
18+
.takes_value(false)
19+
.help("Pretty prints a JSON file")
20+
)
21+
.get_matches();
22+
23+
let path = matches.value_of("file").unwrap();
24+
let data = fs::read_to_string(path).expect("Error reading file");
25+
let pretty = matches.is_present("pretty");
26+
27+
if pretty {
28+
match json_read(&data) {
29+
Ok(v) => println!("{}", json::stringify_pretty(v, 4)),
30+
Err(e) => println!("{}", e),
31+
}
32+
} else {
33+
match json_lint(&data) {
34+
Some(e) => println!("{}", e),
35+
_ => println!("{}", data)
36+
}
37+
}
38+
}
39+
40+
fn json_read(json: &str) -> Result<json::JsonValue, json::JsonError> {
41+
json::parse(json)
42+
}
43+
44+
fn json_lint(json: &str) -> Option<json::JsonError> {
45+
let result: Result<json::JsonValue, json::JsonError> = json::parse(json);
46+
result.err()
47+
}

0 commit comments

Comments
 (0)