Expand description
Utilities module providing essential types and functions for common system operations
§Core Features:
- Byte Size Handling: Human-readable byte size parsing/formatting with
Bytesize
- Duration Conversion: String-to-Duration parsing supporting multiple time units
- Timestamp Utilities: Precise timestamp handling with millisecond resolution
- Network Addressing: Cluster node address parsing (
NodeAddr
) and socket address handling - Counter Implementation: Thread-safe counter with merge modes (
Counter
)
§Key Components:
Bytesize
: Handles 2G512M-style conversions with serialization support- Time functions:
timestamp_secs()
,format_timestamp_now()
, and datetime parsing NodeAddr
: Cluster node representation (ID@Address) with parser- Network address utilities with proper error handling
- Custom serde helpers for duration and address types
§Usage Examples:
use rmqtt_utils::{Bytesize, NodeAddr, to_bytesize, to_duration, format_timestamp_now};
// Byte size parsing
let size = Bytesize::try_from("2G512M").unwrap();
assert_eq!(size.as_usize(), 2_684_354_560);
// Duration conversion
let duration = to_duration("1h30m15s");
assert_eq!(duration.as_secs(), 5415);
// Node address parsing
let node: NodeAddr = "1@mqtt-node:1883".parse().unwrap();
assert_eq!(node.id, 1);
// Timestamp formatting
let now = format_timestamp_now();
assert!(now.contains("2025")); // Current year
§Safety Guarantees:
- Zero
unsafe
code usage (enforced by#![deny(unsafe_code)]
) - Comprehensive error handling for parsing operations
- Platform-agnostic network address handling
- Chrono-based timestamp calculations with proper timezone handling
Overall usage example:
use rmqtt_utils::{
Bytesize, NodeAddr,
to_bytesize, to_duration,
timestamp_secs, format_timestamp_now
};
// Parse byte size from string
let size = Bytesize::try_from("2G512M");
// Convert duration string
let duration = to_duration("1h30m15s");
// Parse node address
let node: NodeAddr = "123@127.0.0.1:1883".parse().unwrap();
// Get formatted timestamp
let now = format_timestamp_now();
Structs§
- Bytesize
- Human-readable byte size representation with parsing/serialization support
- Counter
- A counter with current and maximum tracking, and optional merging behavior.
- Node
Addr - Cluster node address representation (ID@Address)
Enums§
- Parse
Size Error - Stats
Merge Mode - Merge behavior modes for
Counter
.
Functions§
- deserialize_
addr - Deserialize SocketAddr with error handling
- deserialize_
addr_ option - Deserialize optional SocketAddr with port handling
- deserialize_
datetime_ option - Deserialize optional datetime from string
- deserialize_
duration - Deserialize Duration from human-readable string format
- deserialize_
duration_ option - Deserialize optional Duration from string
- format_
timestamp - Format timestamp (seconds) to human-readable string
- format_
timestamp_ millis - Format millisecond timestamp to string
- format_
timestamp_ millis_ now - Format current millisecond timestamp to string
- format_
timestamp_ now - Format current timestamp to string
- serialize_
datetime_ option - Serialize optional datetime to string
- timestamp
- Get current timestamp as Duration
- timestamp_
millis - Get current timestamp in milliseconds
- timestamp_
secs - Get current timestamp in seconds
- to_
bytesize - Parse human-readable byte size string to usize
- to_
duration - Convert human-readable duration string to Duration
Type Aliases§
- Addr
- Network address storage using efficient ByteString
- NodeId
- Cluster node identifier type (64-bit unsigned integer)
- Timestamp
- Timestamp representation in seconds since Unix epoch
- Timestamp
Millis - Timestamp representation in milliseconds since Unix epoch