iocaine 2.2.0

The deadliest poison known to AI
Documentation
// SPDX-FileCopyrightText: 2025 Gergely Nagy
// SPDX-FileContributor: Gergely Nagy
//
// SPDX-License-Identifier: MIT

use ipnet::IpNet;
use roto::{Runtime, roto_static_method};
use std::net::IpAddr;
use std::sync::Arc;

use crate::means_of_production::Error;

#[derive(Debug, Clone, Copy)]
struct Network;

pub fn register_network_type(runtime: &mut Runtime) -> Result<(), Error> {
    runtime.register_copy_type_with_name::<Network>("Network", "Representation of a network")?;

    #[roto_static_method(runtime, Network)]
    fn contains(net: Arc<str>, addr: Arc<str>) -> bool {
        let addr = match addr.as_ref().parse::<IpAddr>() {
            Ok(v) => v,
            Err(e) => {
                tracing::error!({ addr = addr.to_string() }, "Error parsing IP address: {e}");
                return false;
            }
        };
        let net = match net.as_ref().parse::<IpNet>() {
            Ok(v) => v,
            Err(e) => {
                tracing::error!({ net = net.to_string() }, "Error parsing IP network: {e}");
                return false;
            }
        };

        net.contains(&addr)
    }

    Ok(())
}