Functional Weave
Code in Rust

net.ipv4@1.0.0

impl/rust/format_ipv4.rs

899 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

/// Dotted-quad text for an unsigned 32-bit address: the inverse of parse_ipv4.
///
/// # Panics
/// Panics when the value is outside 0 to 4294967295.
pub fn format_ipv4(value: i64) -> String {
    if !(0..=4294967295).contains(&value) {
        panic!("IPv4 value must be an integer from 0 to 4294967295, received {}", value);
    }
    format!(
        "{}.{}.{}.{}",
        (value >> 24) & 255,
        (value >> 16) & 255,
        (value >> 8) & 255,
        value & 255
    )
}

pub fn fune_vector(args: &[Value]) -> Value {
    // A fractional number cannot become an i64 without changing it, so refuse
    // it with the wording TypeScript and Python use.
    let value = match &args[0] {
        Value::Int(i) => *i,
        other => panic!("IPv4 value must be an integer from 0 to 4294967295, received {}", other),
    };
    Value::str(&format_ipv4(value))
}