Skip to content

feat: Add compact json formatter, upgrade to 2024 edition #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ description = "MQTT CLI for AWS IoT"
keywords = ["cli", "mqtt", "aws-iot"]
authors = ["Deep Panchal <[email protected]>"]
version = "0.2.0"
edition = "2021"
edition = "2024"
documentation = "https://docs.rs/aws-iot-mqtt-cli"
homepage = "https://github.com/deepanchal/aws-iot-mqtt-cli"
repository = "https://github.com/deepanchal/aws-iot-mqtt-cli"
license = "MIT"

[dependencies]
aws-iot-device-sdk-rust = "0.6.0"
rumqttc = "0.24.0"
chrono = "0.4.40"
clap = { version = "4.5.31", features = ["derive", "env"] }
clap = { version = "4.5.32", features = ["derive", "env"] }
colored = "3.0.0"
env_logger = "0.11.7"
json-pretty-compact = "0.1.2"
log = "0.4.26"
regex = "1.11.1"
rumqttc = "0.24.0"
serde = "1.0.219"
serde_json = "1.0.140"
term_size = "0.3.2"
tokio = { version = "1.44.0", features = ["full"] }
env_logger = "0.11.6"
log = "0.4.26"
tokio = { version = "1.44.1", features = ["full"] }
14 changes: 12 additions & 2 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use colored::*;
use serde_json::Value;
use json_pretty_compact::PrettyCompactFormatter;
use serde::ser::Serialize;
use serde_json::{Serializer, Value};
use std::hash::{Hash, Hasher};

pub fn format_mqtt_log_entry(topic: &str, payload: &str) -> String {
Expand All @@ -22,7 +24,15 @@ pub fn format_mqtt_log_entry(topic: &str, payload: &str) -> String {

fn format_payload(payload: &str) -> String {
match serde_json::from_str::<Value>(payload) {
Ok(value) => serde_json::to_string_pretty(&value).unwrap_or_else(|_| payload.to_string()),
Ok(value) => {
let mut buffer = Vec::new();
let formatter = PrettyCompactFormatter::new();
let mut ser = Serializer::with_formatter(&mut buffer, formatter);
match value.serialize(&mut ser) {
Ok(_) => String::from_utf8_lossy(&buffer).into_owned(),
Err(_) => payload.to_string(),
}
}
Err(_) => payload.to_string(),
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod format;
use crate::format::format_mqtt_log_entry;
use aws_iot_device_sdk_rust::settings::MQTTOptionsOverrides;
use aws_iot_device_sdk_rust::{
async_event_loop_listener, AWSIoTAsyncClient, AWSIoTSettings, Packet, QoS,
AWSIoTAsyncClient, AWSIoTSettings, Packet, QoS, async_event_loop_listener,
};
use clap::{CommandFactory, Parser, Subcommand};
use colored::*;
Expand All @@ -15,7 +15,7 @@ use std::sync::Arc;
use tokio::signal;
use tokio::sync::Mutex;
use tokio::task;
use tokio::time::{sleep, Duration};
use tokio::time::{Duration, sleep};

/// MQTT CLI for AWS IoT
#[derive(Parser, Debug)]
Expand Down