alpine-protocol-sdk 0.2.4

High-level SDK on top of the ALPINE protocol layer.
Documentation
use std::collections::HashMap;
use std::net::IpAddr;
use std::sync::{Mutex, OnceLock};

static QUARANTINE: OnceLock<Mutex<HashMap<IpAddr, String>>> = OnceLock::new();

fn quarantine_map() -> &'static Mutex<HashMap<IpAddr, String>> {
    QUARANTINE.get_or_init(|| Mutex::new(HashMap::new()))
}

pub fn mark_quarantine(ip: IpAddr, reason: String) -> bool {
    let mut guard = quarantine_map()
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    if guard.contains_key(&ip) {
        return false;
    }
    guard.insert(ip, reason);
    true
}

pub fn quarantine_reason(ip: IpAddr) -> Option<String> {
    let guard = quarantine_map()
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    guard.get(&ip).cloned()
}

pub fn clear_quarantine(ip: IpAddr) -> bool {
    let mut guard = quarantine_map()
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    guard.remove(&ip).is_some()
}