Skip to content

Commit

Permalink
refactor: move response formatting to seperate package (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Oct 30, 2023
1 parent 82624c0 commit 08efa2f
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = ["hitt-parser", "hitt-request", "hitt-cli"]
members = ["hitt-cli", "hitt-formatter", "hitt-parser", "hitt-request"]
resolver = "2"
2 changes: 1 addition & 1 deletion hitt-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ path = "src/main.rs"
[dependencies]
async-recursion = "1.0.5"
clap = { version = "4.3.24", features = ["derive"] }
hitt-formatter = { path = "../hitt-formatter" }
hitt-parser = { path = "../hitt-parser" }
hitt-request = { path = "../hitt-request" }
jsonformat = "2.0.0"
reqwest = "0.11.20"
tokio = { version = "1.32.0", features = ["fs", "macros", "rt-multi-thread"] }
22 changes: 11 additions & 11 deletions hitt-cli/src/printing/body.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::printing::{TEXT_RESET, TEXT_YELLOW};
use hitt_formatter::ContentType;

#[inline]
fn format_json(input: &str) -> String {
jsonformat::format(input, jsonformat::Indentation::TwoSpace)
}
use crate::printing::{TEXT_RESET, TEXT_YELLOW};

#[inline]
fn __print_body(body: &str) {
Expand All @@ -18,13 +15,16 @@ pub(crate) fn print_body(body: &str, content_type: Option<&str>, disable_pretty_
}

match content_type {
Some(content_type) => {
if content_type.starts_with("application/json") {
__print_body(&format_json(body));
} else {
__print_body(body);
Some(content_type) => match ContentType::from(content_type) {
ContentType::Unknown => __print_body(body),
i => {
if let Some(formatted) = hitt_formatter::format(body, i) {
__print_body(&formatted)
} else {
__print_body(body)
}
}
}
},
None => __print_body(body),
}
}
2 changes: 1 addition & 1 deletion hitt-cli/src/printing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ pub(crate) fn handle_response(response: HittResponse, args: &CliArguments) {
}

#[inline]
pub fn print_error(message: String) {
pub(crate) fn print_error(message: String) {
eprintln!("{TEXT_RED}hitt: {message}{TEXT_RESET}")
}
7 changes: 7 additions & 0 deletions hitt-formatter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "hitt-formatter"
version = "0.0.1"
edition = "2021"

[dependencies]
jsonformat = "2.0.0"
4 changes: 4 additions & 0 deletions hitt-formatter/src/json/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[inline]
pub(crate) fn format_json(input: &str) -> String {
jsonformat::format(input, jsonformat::Indentation::TwoSpace)
}
26 changes: 26 additions & 0 deletions hitt-formatter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use json::format_json;

pub mod json;

pub enum ContentType {
Json,
Unknown,
}

impl From<&str> for ContentType {
fn from(value: &str) -> Self {
if value.starts_with("application/json") {
return ContentType::Json;
}

ContentType::Unknown
}
}

#[inline]
pub fn format(input: &str, content_type: ContentType) -> Option<String> {
match content_type {
ContentType::Json => Some(format_json(input)),
ContentType::Unknown => None,
}
}

0 comments on commit 08efa2f

Please sign in to comment.