rmqtt-utils
rmqtt-utils
is a lightweight Rust utility crate designed to support common system-level operations with a focus on performance, reliability, and developer ergonomics. It is especially useful in distributed systems like MQTT brokers but is general-purpose enough for any Rust project.
✨ Features
-
📦 Byte Size Parsing & Formatting
Handle human-readable byte strings like2G512M
via theBytesize
type, with full serde support. -
⏳ Flexible Duration Parsing
Convert strings like1h30m15s
or2w3d12h
intostd::time::Duration
objects. -
🕒 Timestamp Utilities
Retrieve and format current timestamps with second or millisecond precision. -
🌐 Cluster Node Addressing
Parse and represent cluster node addresses in the formatID@host:port
viaNodeAddr
. -
📈 Thread-Safe Counter
MergeableCounter
type with internal atomic implementation, useful for metrics collection. -
🧰 Serde Helpers
Custom deserialization logic for durations, socket addresses, and other types.
🧩 Key Components
Component | Description |
---|---|
Bytesize |
Parses 2G512M -style strings and converts them to byte values |
to_duration |
Parses complex duration strings like 2w3d12h into Duration |
NodeAddr |
Parses and stores cluster node IDs and addresses (1@host:port ) |
Counter |
Thread-safe integer counter with merge support |
timestamp_secs |
Returns current UNIX timestamp in seconds |
format_timestamp_now |
Returns the current UTC timestamp as a formatted string |
🛠 Usage Examples
use ;
// Byte size parsing
let size = try_from.unwrap;
assert_eq!;
// Duration parsing
let duration = to_duration.unwrap;
assert_eq!;
// Node address parsing
let node: NodeAddr = "1@mqtt-node:1883".parse.unwrap;
assert_eq!;
// Current timestamp formatting
let now = format_timestamp_now;
println!;
🛡 Safety & Reliability
- ✅
#![deny(unsafe_code)]
enforced — no unsafe blocks allowed - ✅ Robust error handling for parsing operations
- ✅ Cross-platform compatibility
- ✅ Accurate UTC timestamp formatting using
chrono
📦 Crate Usage
Add the following to your Cargo.toml
:
[]
= "0.1" # Replace with the latest version
Then, in your code:
use ;
🔗 Related Crates
This crate is designed to be used as part of the rmqtt
MQTT broker project, but it is modular and reusable on its own.