pub mod context;
pub mod expression;
pub mod router;
pub mod schema;
use crate::ast::Value;
use cidr::IpCidr;
use std::convert::TryFrom;
use std::ffi;
use std::net::IpAddr;
use std::os::raw::c_char;
use std::slice::from_raw_parts;
pub const ERR_BUF_MAX_LEN: usize = 4096;
#[derive(Debug)]
#[repr(C)]
pub enum CValue {
Str(*const u8, usize),
IpCidr(*const u8),
IpAddr(*const u8),
Int(i64),
}
impl TryFrom<&CValue> for Value {
type Error = String;
fn try_from(v: &CValue) -> Result<Self, Self::Error> {
Ok(match v {
CValue::Str(s, len) => Self::String(unsafe {
std::str::from_utf8(from_raw_parts(*s, *len))
.map_err(|e| e.to_string())?
.to_string()
}),
CValue::IpCidr(s) => Self::IpCidr(
unsafe {
ffi::CStr::from_ptr(*s as *const c_char)
.to_str()
.map_err(|e| e.to_string())?
.to_string()
}
.parse::<IpCidr>()
.map_err(|e| e.to_string())?,
),
CValue::IpAddr(s) => Self::IpAddr(
unsafe {
ffi::CStr::from_ptr(*s as *const c_char)
.to_str()
.map_err(|e| e.to_string())?
.to_string()
}
.parse::<IpAddr>()
.map_err(|e| e.to_string())?,
),
CValue::Int(i) => Self::Int(*i),
})
}
}