resynth 0.4.0

A packet synthesis language
Documentation
use std::rc::Rc;

use pkt::Packet;

use crate::libapi::{Class, ClassDef, FuncDef, Module};
use crate::sym::Symbol;
use crate::val::{Val, ValDef};
use ezpkt::Erspan2Flow;

const ENCAP: FuncDef = func!(
    /// Encapsulate packets in ERSPAN2
    resynth fn encap(
        it: PktGen
        =>
        port_index: U32 = 0,
        =>
        Void
    ) -> PktGen
    |mut args| {
        let obj = args.take_this();
        let mut r = obj.borrow_mut();
        let this: &mut Erspan2Flow = r.as_mut_any().downcast_mut().unwrap();
        let it: Rc<Box<[Packet]>> = args.next().into();
        let port_index: u32 = args.next().into();

        let mut ret: Vec<Packet> = Vec::with_capacity(it.len());

        for pkt in it.iter() {
            ret.push(this.encap(&pkt.as_slice().get(pkt), port_index));
        }

        Ok(ret.into())
    }
);

const ERSPAN2: ClassDef = class!(
    /// # ERSPAN2 Session
    resynth class Erspan2 {
        encap => Symbol::Func(&ENCAP),
    }
);

impl Class for Erspan2Flow {
    fn def(&self) -> &'static ClassDef {
        &ERSPAN2
    }
}

const SESSION: FuncDef = func!(
    /// Create an erspan2 session
    resynth fn session(
        cl: Ip4,
        sv: Ip4,
        =>
        raw: Bool = false,
        =>
        Void
    ) -> Obj
    |mut args| {
        let cl = args.next();
        let sv = args.next();
        let raw: bool = args.next().into();
        Ok(Val::from(Erspan2Flow::new(cl.into(), sv.into(), raw)))
    }
);

pub const MODULE: Module = module! {
    /// # ERSPAN version 2
    resynth mod erspan2 {
        Erspan2 => Symbol::Class(&ERSPAN2),
        session => Symbol::Func(&SESSION),
    }
};