aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Pasquet <dev@etenil.net>2022-02-24 23:39:33 +0000
committerGuillaume Pasquet <dev@etenil.net>2022-02-24 23:39:33 +0000
commit3517f9d9ff69eebae14e32a3b1550bd920ea3238 (patch)
treeb56d16e0b757bc8868e9f49a7cc20c0ae546c6ad
parent649299965d2368f65b45d7f51b89eb69b491c7a4 (diff)
Fix #5: Add JSON syntax coloring
-rw-r--r--Cargo.lock14
-rw-r--r--Cargo.toml1
-rw-r--r--src/barbfile.rs2
-rw-r--r--src/executor.rs13
-rw-r--r--src/main.rs11
-rw-r--r--src/output.rs31
-rw-r--r--test_api/test_api.py8
-rw-r--r--test_api/test_api_get_long.barb2
8 files changed, 68 insertions, 14 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 2bcbd21..74a3555 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -40,6 +40,7 @@ version = "0.1.5"
dependencies = [
"clap 3.0.14",
"colored",
+ "colored_json",
"jq-rs",
"jsonformat",
"serde",
@@ -140,6 +141,19 @@ dependencies = [
]
[[package]]
+name = "colored_json"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1fd32eb54d016e203b7c2600e3a7802c75843a92e38ccc4869aefeca21771a64"
+dependencies = [
+ "ansi_term",
+ "atty",
+ "libc",
+ "serde",
+ "serde_json",
+]
+
+[[package]]
name = "crc32fast"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 5ae0756..9879254 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -19,3 +19,4 @@ serde_json = "1.0.70"
jsonformat = "1.2.0"
jq-rs = "0.4.1"
colored = "2.0.0"
+colored_json = "2.1.0"
diff --git a/src/barbfile.rs b/src/barbfile.rs
index 19a24cd..66a8756 100644
--- a/src/barbfile.rs
+++ b/src/barbfile.rs
@@ -119,7 +119,7 @@ impl BarbFile {
pub fn url(&self) -> &String {
&self.preamble.url
}
-
+
pub fn filter(&self) -> &Option<String> {
&self.preamble.filter
}
diff --git a/src/executor.rs b/src/executor.rs
index 4cfa59e..f6ca4f5 100644
--- a/src/executor.rs
+++ b/src/executor.rs
@@ -5,7 +5,6 @@ use std::collections::HashMap;
use ureq;
use ureq::Error as UreqError;
-
pub struct Context {
vars: HashMap<String, String>,
}
@@ -60,7 +59,10 @@ impl Executor {
}
fn make_req(&self, bfile: &BarbFile, output: &BarbOutput) -> ureq::Request {
- output.req(bfile.method_as_string(), self.context.substitute(&bfile.url()));
+ output.req(
+ bfile.method_as_string(),
+ self.context.substitute(&bfile.url()),
+ );
let mut req = ureq::request(
bfile.method_as_string().as_str(),
self.context.substitute(&bfile.url()).as_str(),
@@ -68,15 +70,12 @@ impl Executor {
for header in bfile.headers() {
let hdr_val = self.context.substitute(header.value());
- req = req.set(
- header.name(),
- hdr_val.as_str(),
- );
+ req = req.set(header.name(), hdr_val.as_str());
output.req_hdr(header.name().to_string(), hdr_val);
}
output.end_req();
-
+
req
}
diff --git a/src/main.rs b/src/main.rs
index a02938a..b7ce5ba 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -77,11 +77,18 @@ fn run_file(args: &Args, executor: &mut Executor, file_name: &String) -> Result<
output.status(response.status(), response.status_text());
for header_name in response.headers_names() {
- output.resp_hdr(header_name.to_string(), response.header(header_name.as_str()).unwrap());
+ output.resp_hdr(
+ header_name.to_string(),
+ response.header(header_name.as_str()).unwrap(),
+ );
}
output.end_resp_hdr();
- output.body(apply_filters(args, &bfile, response.into_string().unwrap())?);
+ output.body(apply_filters(
+ args,
+ &bfile,
+ response.into_string().unwrap(),
+ )?);
Ok(())
}
diff --git a/src/output.rs b/src/output.rs
index d181d0f..d7ab74e 100644
--- a/src/output.rs
+++ b/src/output.rs
@@ -1,4 +1,5 @@
use colored::*;
+use colored_json::prelude::*;
use jsonformat::{format_json, Indentation};
use std::fmt::Display;
@@ -13,11 +14,21 @@ pub struct BarbOutput {
impl BarbOutput {
fn _print_header<T, I>(&self, name: T, value: I)
- where T: Display, I: Display{
+ where
+ T: Display,
+ I: Display,
+ {
println!("{}: {}", name, value);
}
- pub fn new(request: bool, req_headers: bool, headers: bool, body: bool, raw_body: bool, color: bool) -> BarbOutput {
+ pub fn new(
+ request: bool,
+ req_headers: bool,
+ headers: bool,
+ body: bool,
+ raw_body: bool,
+ color: bool,
+ ) -> BarbOutput {
BarbOutput {
request,
req_headers,
@@ -88,16 +99,28 @@ impl BarbOutput {
}
}
+ fn _format_body(&self, body: String) -> String {
+ if self.raw_body {
+ return body;
+ }
+
+ let formatted = format_json(body.as_str(), Indentation::Default);
+ match self.color {
+ true => formatted.to_colored_json_auto().unwrap_or(formatted),
+ _ => formatted,
+ }
+ }
+
pub fn body(&self, body: String) {
if !self.body {
return;
}
-
+
println!(
"{}",
match self.raw_body {
true => body,
- false => format_json(body.as_str(), Indentation::Default),
+ _ => self._format_body(body),
}
);
}
diff --git a/test_api/test_api.py b/test_api/test_api.py
index bd8303d..cce55f4 100644
--- a/test_api/test_api.py
+++ b/test_api/test_api.py
@@ -4,6 +4,14 @@ from bottle import get, post, run, HTTPError
def get_root():
return {"status": "OK"}
+@get("/long")
+def get_long():
+ return {"data": [
+ {"name": "apple", "calories": 5},
+ {"name": "orange", "calories": 120},
+ {"name": "pear", "calories": 45},
+ ]}
+
@post("/")
def post_root():
return {"status": "SUCCESS"}
diff --git a/test_api/test_api_get_long.barb b/test_api/test_api_get_long.barb
new file mode 100644
index 0000000..96133b3
--- /dev/null
+++ b/test_api/test_api_get_long.barb
@@ -0,0 +1,2 @@
+#GET^http://localhost:8080/long
+#Foo: Bar