use crate::compiler::prelude::*;
use std::net::IpAddr;
fn ip_to_ipv6(value: &Value) -> Resolved {
let ip: IpAddr = value
.try_bytes_utf8_lossy()?
.parse()
.map_err(|err| format!("unable to parse IP address: {err}"))?;
match ip {
IpAddr::V4(addr) => Ok(addr.to_ipv6_mapped().to_string().into()),
IpAddr::V6(addr) => Ok(addr.to_string().into()),
}
}
#[derive(Clone, Copy, Debug)]
pub struct IpToIpv6;
impl Function for IpToIpv6 {
fn identifier(&self) -> &'static str {
"ip_to_ipv6"
}
fn usage(&self) -> &'static str {
"Converts the `ip` to an IPv6 address."
}
fn category(&self) -> &'static str {
Category::Ip.as_ref()
}
fn internal_failure_reasons(&self) -> &'static [&'static str] {
&["`ip` is not a valid IP address."]
}
fn return_kind(&self) -> u16 {
kind::BYTES
}
fn return_rules(&self) -> &'static [&'static str] {
&[
"The `ip` is returned unchanged if it's already an IPv6 address.",
"The `ip` is converted to an IPv6 address if it's an IPv4 address.",
]
}
fn parameters(&self) -> &'static [Parameter] {
const PARAMETERS: &[Parameter] = &[Parameter::required(
"value",
kind::BYTES,
"The IP address to convert to IPv6.",
)];
PARAMETERS
}
fn examples(&self) -> &'static [Example] {
&[example! {
title: "IPv4 to IPv6",
source: r#"ip_to_ipv6!("192.168.10.32")"#,
result: Ok("::ffff:192.168.10.32"),
}]
}
fn compile(
&self,
_state: &state::TypeState,
_ctx: &mut FunctionCompileContext,
arguments: ArgumentList,
) -> Compiled {
let value = arguments.required("value");
Ok(IpToIpv6Fn { value }.as_expr())
}
}
#[derive(Debug, Clone)]
struct IpToIpv6Fn {
value: Box<dyn Expression>,
}
impl FunctionExpression for IpToIpv6Fn {
fn resolve(&self, ctx: &mut Context) -> Resolved {
let value = self.value.resolve(ctx)?;
ip_to_ipv6(&value)
}
fn type_def(&self, _: &state::TypeState) -> TypeDef {
TypeDef::bytes().fallible()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::value;
test_function![
ip_to_ipv6 => IpToIpv6;
invalid {
args: func_args![value: "i am not an ipaddress"],
want: Err(
"unable to parse IP address: invalid IP address syntax"),
tdef: TypeDef::bytes().fallible(),
}
valid {
args: func_args![value: "192.168.0.1"],
want: Ok(value!("::ffff:192.168.0.1")),
tdef: TypeDef::bytes().fallible(),
}
ipv6_passthrough {
args: func_args![value: "2404:6800:4003:c02::64"],
want: Ok(value!("2404:6800:4003:c02::64")),
tdef: TypeDef::bytes().fallible(),
}
];
}