Functional Weave
Code in Rust

net.cidr@1.0.0

impl/rust/prefix_to_netmask.rs

683 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_format_ipv4::format_ipv4;

/// The dotted-quad mask for a prefix length: 24 is 255.255.255.0.
///
/// # Panics
/// Panics when the prefix is outside 0 to 32.
pub fn prefix_to_netmask(prefix: i64) -> String {
    if !(0..=32).contains(&prefix) {
        panic!("prefix must be an integer from 0 to 32, received {}", prefix);
    }
    format_ipv4(4294967296 - (1i64 << (32 - prefix)))
}

pub fn fune_vector(args: &[Value]) -> Value {
    let prefix = match &args[0] {
        Value::Int(i) => *i,
        other => panic!("prefix must be an integer from 0 to 32, received {}", other),
    };
    Value::str(&prefix_to_netmask(prefix))
}