lib.rs

Overview

This file serves as a core utility and configuration module within the system, providing essential data types, helper functions, and serialization/deserialization logic primarily related to network communication and message handling. It defines enums for message delivery phases and send modes, utility functions for parsing and serializing network socket addresses, and modules that organize different aspects of the system such as messaging, networking, metrics, and configuration.

The file also exposes and re-exports key components like direct_sender for use in other parts of the system. It is foundational for enabling reliable network communication and message exchange, supporting both broadcast and direct messaging modes.

Modules

Constants

Enums

DeliveryPhase

#[derive(Copy, Clone)]
pub enum DeliveryPhase {
    OutgoingBuffer,
    OutgoingTransfer,
    IncomingTransfer,
    IncomingBuffer,
}

Represents the phase of message delivery:

Usage Example:

let phase = DeliveryPhase::OutgoingTransfer;
match phase {
    DeliveryPhase::OutgoingBuffer => println!("Buffering outgoing message"),
    DeliveryPhase::OutgoingTransfer => println!("Sending message"),
    _ => {}
}

SendMode

pub enum SendMode {
    Broadcast,
    Direct,
}

Defines how messages are sent:

Methods:

Usage Example:

let mode = SendMode::Broadcast;
println!("Send mode: {}", mode); // Output: "Send mode: broadcast"

Structs

PublisherConfig

#[derive(Clone, Debug)]
pub struct PublisherConfig {
    pub addrs: Vec<SocketAddr>,
}

Configuration structure holding a list of publisher socket addresses.

This struct can be used to configure network publishers and manage their addresses.

Functions

host_id_prefix

pub fn host_id_prefix(s: &str) -> &str

Extracts the first 6 characters from a string, intended to be used as a host ID prefix.

Example:

let prefix = host_id_prefix("abcdef12345");
assert_eq!(prefix, "abcdef");

detailed

pub(crate) fn detailed(err: &impl Debug) -> String

Formats a debug error object into a single-line string by removing line breaks.

try_parse_socket_addr

pub fn try_parse_socket_addr(s: impl AsRef<str>, default_port: u16) -> anyhow::Result<SocketAddr>

Parses a string into a SocketAddr. If the string does not contain a port, appends the provided default_port.

Example:

let addr = try_parse_socket_addr("127.0.0.1", 8500).unwrap();
assert_eq!(addr.port(), 8500);

parse_publisher_addr

pub fn parse_publisher_addr(s: impl AsRef<str>) -> anyhow::Result<SocketAddr>

Helper function that parses a string into a SocketAddr using the default publisher port (8500).

deserialize_publisher_addr

pub fn deserialize_publisher_addr<'de, D>(deserializer: D) -> Result<SocketAddr, D::Error>
where
    D: Deserializer<'de>,

Deserializes a string into a SocketAddr, using parse_publisher_addr. Used with Serde for config or message parsing.

deserialize_optional_publisher_addr

pub fn deserialize_optional_publisher_addr<'de, D>(deserializer: D) -> Result<Option<SocketAddr>, D::Error>
where
    D: Deserializer<'de>,

Deserializes an optional string into an optional SocketAddr.

serialize_subscribe

pub fn serialize_subscribe<S>(value: &Vec<Vec<SocketAddr>>, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,

Serializes a nested list of socket addresses (publishers with their addresses) into a flat list for serialization.

deserialize_subscribe

pub fn deserialize_subscribe<'de, D>(deserializer: D) -> Result<Vec<Vec<SocketAddr>>, D::Error>
where
    D: Deserializer<'de>,

Deserializes a flat list of socket address strings into a nested vector structure (publishers with their addresses), parsing each address.

deserialize_publisher_addrs

pub fn deserialize_publisher_addrs<'de, D>(deserializer: D) -> Result<Vec<SocketAddr>, D::Error>
where
    D: Deserializer<'de>,

Deserializes a list of publisher address strings into a vector of SocketAddr.

extract_msg_type

pub fn extract_msg_type(s: impl AsRef<str>) -> String

Extracts the message type from a string representation of a message. It looks for a substring after "::" in the first token of the string.

Example:

let msg_type = extract_msg_type("MsgType::MyMessage (1, 22222)");
assert_eq!(msg_type, "MyMessage");

Testing

The file includes unit tests under the unit_tests module to validate:

Implementation Details

Interactions with Other Parts of the Application


Mermaid Diagram: File Structure and Main Functions Flowchart

flowchart TD
A[lib.rs] --> B[channel]
A --> C[cli]
A --> D[config]
A --> E[direct_sender]
A --> F[message]
A --> G[metrics]
A --> H[network]
A --> I[pub_sub]
A --> J[resolver]
A --> K[tests]
A --> L[transfer]
A --> M[unix_signals]
A --> N[Enums]
N --> N1[DeliveryPhase]
N --> N2[SendMode]
A --> O[Functions]
O --> O1[host_id_prefix]
O --> O2[detailed]
O --> O3[try_parse_socket_addr]
O --> O4[parse_publisher_addr]
O --> O5[deserialize_publisher_addr]
O --> O6[deserialize_optional_publisher_addr]
O --> O7[serialize_subscribe]
O --> O8[deserialize_subscribe]
O --> O9[deserialize_publisher_addrs]
O --> O10[extract_msg_type]
A --> P[Structs]
P --> P1[PublisherConfig]

This diagram illustrates the hierarchical organization of the file, showing the primary modules, enums, functions, and structs contained within lib.rs.