rust-mc-status 2.0.0

High-performance asynchronous Rust library for querying Minecraft server status (Java & Bedrock)
Documentation
//! Basic usage example for the rust-mc-status library.
//!
//! This example demonstrates the most common use cases:
//! - Pinging a single Java server
//! - Pinging a single Bedrock server
//! - Handling errors
//! - Using SRV record lookup

use rust_mc_status::{McClient, ServerEdition};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a client with custom settings
    let client = McClient::new()
        .with_timeout(Duration::from_secs(5))
        .with_max_parallel(10);

    println!("=== Basic Usage Examples ===\n");

    // Example 1: Ping a Java server without explicit port (uses SRV lookup)
    println!("1. Pinging Java server (automatic SRV lookup):");
    println!("   Address: mc.hypixel.net");
    match client.ping("mc.hypixel.net", ServerEdition::Java).await {
        Ok(status) => {
            println!("   ✅ Server is online!");
            println!("   IP: {}", status.ip);
            println!("   Port: {}", status.port);
            println!("   Latency: {:.2} ms", status.latency);
            if let Some((online, max)) = status.players() {
                println!("   Players: {}/{}", online, max);
            }
        }
        Err(e) => {
            println!("   ❌ Error: {}", e);
        }
    }

    println!("\n{}", "=".repeat(50));
    println!();

    // Example 2: Ping a Java server with explicit port (skips SRV lookup)
    println!("2. Pinging Java server (explicit port, no SRV lookup):");
    println!("   Address: mc.hypixel.net:25565");
    match client.ping("mc.hypixel.net:25565", ServerEdition::Java).await {
        Ok(status) => {
            println!("   ✅ Server is online!");
            println!("   IP: {}", status.ip);
            println!("   Port: {}", status.port);
        }
        Err(e) => {
            println!("   ❌ Error: {}", e);
        }
    }

    println!("\n{}", "=".repeat(50));
    println!();

    // Example 3: Ping a Bedrock server
    println!("3. Pinging Bedrock server:");
    println!("   Address: geo.hivebedrock.network:19132");
    match client.ping("geo.hivebedrock.network:19132", ServerEdition::Bedrock).await {
        Ok(status) => {
            println!("   ✅ Server is online!");
            println!("   IP: {}", status.ip);
            println!("   Port: {}", status.port);
            println!("   Latency: {:.2} ms", status.latency);
            if let Some((online, max)) = status.players() {
                println!("   Players: {}/{}", online, max);
            }
        }
        Err(e) => {
            println!("   ❌ Error: {}", e);
        }
    }

    println!("\n{}", "=".repeat(50));
    println!();

    // Example 4: Error handling
    println!("4. Error handling example:");
    println!("   Trying to ping invalid server...");
    match client.ping("invalid-server-that-does-not-exist.com", ServerEdition::Java).await {
        Ok(status) => {
            println!("   ✅ Server is online: {:?}", status);
        }
        Err(e) => {
            println!("   ❌ Expected error: {}", e);
            println!("   This is normal behavior for invalid servers.");
        }
    }

    Ok(())
}