Crate rmqtt_utils

Source
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.
NodeAddr
Cluster node address representation (ID@Address)

Enums§

ParseSizeError
StatsMergeMode
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
TimestampMillis
Timestamp representation in milliseconds since Unix epoch