Functional Weave
Code in Python

net.cidr@1.0.0

impl/rust/netmask_to_prefix.rs

668 bytes · the Rust implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
use super::net_ipv4_parse_ipv4::parse_ipv4;

/// The prefix length of a dotted-quad mask; a mask with a gap in its one bits is refused.
///
/// # Panics
/// Panics on text that is not a dotted quad, or a non-contiguous mask.
pub fn netmask_to_prefix(netmask: &str) -> i64 {
    let value = parse_ipv4(netmask);
    let host_part = 4294967295 - value + 1;
    for prefix in 0..=32i64 {
        if (1i64 << (32 - prefix)) == host_part {
            return prefix;
        }
    }
    panic!("\"{}\" is not a contiguous netmask", netmask);
}

pub fn fune_vector(args: &[Value]) -> Value {
    Value::Int(netmask_to_prefix(args[0].as_str()))
}