hamelin_lib 0.23.2

Core library for Hamelin query language
Documentation
//! IP network functions.

use std::any::TypeId;

use crate::func::def::{FunctionDef, ParameterBindingProvider, Parameters};
use crate::types::array::Array;
use crate::types::matcher::ExactMatcher;
use crate::types::struct_type::Struct;
use crate::types::{Type, BINARY, INT, STRING};

#[derive(Default, Clone)]
pub struct IpPrefix;

impl FunctionDef for IpPrefix {
    fn name(&self) -> &'static str {
        "ip_prefix"
    }

    fn parameters(&self) -> Parameters {
        Parameters::new()
            .with("ip", ExactMatcher::of(STRING))
            .with("prefix_length", ExactMatcher::of(INT))
    }

    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
        Ok(STRING)
    }

    fn type_id(&self) -> TypeId {
        TypeId::of::<Self>()
    }
}

#[derive(Default, Clone)]
pub struct CidrCover;

impl FunctionDef for CidrCover {
    fn name(&self) -> &'static str {
        "cidr_cover"
    }

    fn parameters(&self) -> Parameters {
        Parameters::new()
            .with("cidr", ExactMatcher::of(STRING))
            .with("prefix_length", ExactMatcher::of(INT))
    }

    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
        Ok(Array::new(STRING).into())
    }

    fn type_id(&self) -> TypeId {
        TypeId::of::<Self>()
    }
}

#[derive(Default, Clone)]
pub struct CidrRange;

impl FunctionDef for CidrRange {
    fn name(&self) -> &'static str {
        "cidr_range"
    }

    fn parameters(&self) -> Parameters {
        Parameters::new().with("cidr", ExactMatcher::of(STRING))
    }

    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
        Ok(Struct::default()
            .with_str("first", STRING)
            .with_str("last", STRING)
            .into())
    }

    fn type_id(&self) -> TypeId {
        TypeId::of::<Self>()
    }
}

#[derive(Default, Clone)]
pub struct IpToBinary;

impl FunctionDef for IpToBinary {
    fn name(&self) -> &'static str {
        "ip_to_binary"
    }

    fn parameters(&self) -> Parameters {
        Parameters::new().with("ip", ExactMatcher::of(STRING))
    }

    fn return_type(&self, _bindings: &dyn ParameterBindingProvider) -> anyhow::Result<Type> {
        Ok(BINARY)
    }

    fn type_id(&self) -> TypeId {
        TypeId::of::<Self>()
    }
}