rustzmq2 0.1.0

A native async Rust implementation of ZeroMQ
Documentation
// 10_security_plain — PLAIN authentication with a ZAP handler.
//
// PLAIN sends credentials in cleartext — suitable for IPC or trusted networks only.
//
// This example spawns both a server and client in the same process:
//   - Server: PLAIN server mode + ZAP handler that allows only "alice".
//   - Client: sends PLAIN credentials and exchanges messages.
//
// The ZAP handler runs as a global async callback: set it before any sockets bind.
use rustzmq2::prelude::*;
use rustzmq2::{clear_zap_handler, set_zap_handler, ZapRequest, ZapResponse};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Register a ZAP handler that allows only user "alice".
    set_zap_handler(|req: ZapRequest| async move {
        println!(
            "[zap] auth request from {} — user: {:?}",
            req.address,
            req.username.as_deref().unwrap_or("<none>")
        );
        match (req.username.as_deref(), req.password.as_deref()) {
            (Some("alice"), Some("secret")) => ZapResponse::allow("alice"),
            _ => ZapResponse::deny("Unauthorized"),
        }
    });

    // Server: accept PLAIN credentials, validate via ZAP.
    let server_task = tokio::spawn(async {
        let mut rep = rustzmq2::RepSocket::builder()
            .plain_server()
            .zap_domain("secure-app")
            .build();
        rep.bind("tcp://127.0.0.1:5590").await.unwrap();
        println!("[server] listening on tcp://127.0.0.1:5590");

        for _ in 0..3 {
            let msg: String = rep.recv().await.unwrap().try_into().unwrap();
            println!("[server] received: {msg}");
            rep.send(format!("{msg} — authenticated reply"))
                .await
                .unwrap();
        }
    });

    tokio::time::sleep(std::time::Duration::from_millis(100)).await;

    // Authorised client — credentials match the ZAP handler.
    let mut req = rustzmq2::ReqSocket::builder()
        .plain_client("alice", "secret")
        .build();
    req.connect("tcp://127.0.0.1:5590").await?;

    for i in 0..3 {
        req.send(format!("hello {i}")).await?;
        let reply: String = req.recv().await?.try_into()?;
        println!("[client] reply: {reply}");
    }

    server_task.await?;
    clear_zap_handler();
    Ok(())
}