Skip to content

Commit

Permalink
Using tokio style cfg_* macros
Browse files Browse the repository at this point in the history
Defined two macros `cfg_python!` and `cfg_wasm!` to wrap items within
feature specific blocks. Looks a lot cleaner

Signed-off-by: Abhijit Gadgil <gabhijit@iitbombay.org>
  • Loading branch information
gabhijit committed Jun 26, 2024
1 parent 6013d51 commit edfe18b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 45 deletions.
19 changes: 19 additions & 0 deletions src/cfg_macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![allow(unused_macros)]

macro_rules! cfg_python {
($($item:item)*) => {
$(
#[cfg(feature = "python-bindings")]
$item
)*
}
}

macro_rules! cfg_wasm {
($($item:item)*) => {
$(
#[cfg(feature = "wasm")]
$item
)*
}
}
11 changes: 6 additions & 5 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ impl core::fmt::Display for Error {
}

// Python Bindings
#[cfg(feature = "python-bindings")]
impl std::convert::From<Error> for pyo3::PyErr {
// TODO: Add proper error reporting
fn from(_e: Error) -> pyo3::PyErr {
pyo3::exceptions::PyValueError::new_err("Error")
cfg_python! {
impl std::convert::From<Error> for pyo3::PyErr {
// TODO: Add proper error reporting
fn from(_e: Error) -> pyo3::PyErr {
pyo3::exceptions::PyValueError::new_err("Error")
}
}
}
41 changes: 22 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ compile_error!("feature \"wasm\" is only supported for \"wasm32\" targets.");
#[cfg(all(target_family = "wasm", feature = "python-bindings"))]
compile_error!("feature \"python-bindings\" is not supported for \"wasm32\" targets.");

#[macro_use]
pub mod cfg_macros;

pub mod layers;

pub mod errors;
Expand All @@ -77,32 +80,32 @@ pub use packet::Packet;
#[doc(inline)]
pub use types::{ENCAP_TYPE_ETH, ENCAP_TYPE_LINUX_SLL, ENCAP_TYPE_LINUX_SLL2};

#[cfg(feature = "python-bindings")]
use pyo3::prelude::*;
cfg_python! {
use pyo3::prelude::*;

/// Python bindings for packet dissection and sculpting in Rust (scalpel)
#[cfg(feature = "python-bindings")]
#[pymodule]
fn scalpel(py: Python, m: &PyModule) -> PyResult<()> {
packet::register(py, m)?;
Ok(())
/// Python bindings for packet dissection and sculpting in Rust (scalpel)
#[pymodule]
fn scalpel(py: Python, m: &PyModule) -> PyResult<()> {
packet::register(py, m)?;
Ok(())
}
}

#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;
cfg_wasm! {
use wasm_bindgen::prelude::*;

#[cfg(feature = "wasm")]
#[wasm_bindgen]
pub fn dissect_packet(packet: String) -> String {
let _ = layers::register_defaults();
#[wasm_bindgen]
pub fn dissect_packet(packet: String) -> String {
let _ = layers::register_defaults();

let packet = hex::decode(packet);
let packet = hex::decode(packet);

let packet = packet.unwrap();
let packet = packet.unwrap();

let p = Packet::from_bytes(&packet, ENCAP_TYPE_ETH);
let p = Packet::from_bytes(&packet, ENCAP_TYPE_ETH);

let p = p.unwrap();
let p = p.unwrap();

serde_json::to_string_pretty(&p).unwrap()
serde_json::to_string_pretty(&p).unwrap()
}
}
42 changes: 21 additions & 21 deletions src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ use crate::errors::Error;
use crate::types::{EncapType, LayerCreatorFn};
use crate::Layer;

#[cfg(feature = "python-bindings")]
use pyo3::prelude::*;

fn get_encap_types_map() -> &'static RwLock<HashMap<EncapType, LayerCreatorFn>> {
static ENCAP_TYPES_MAP: OnceLock<RwLock<HashMap<EncapType, LayerCreatorFn>>> = OnceLock::new();
ENCAP_TYPES_MAP.get_or_init(|| RwLock::new(HashMap::new()))
Expand Down Expand Up @@ -176,33 +173,36 @@ impl Packet {
}

// Python Bindings
#[allow(clippy::borrow_deref_ref)]
#[cfg(feature = "python-bindings")]
#[pymethods]
impl Packet {
#[staticmethod]
fn from_bytes_py(bytes: &[u8], encap: EncapType) -> PyResult<Self> {
let _ = crate::layers::register_defaults();
cfg_python! {
use pyo3::prelude::*;

Self::from_bytes(bytes, encap).map_err(|e| e.into())
}
#[allow(clippy::borrow_deref_ref)]
#[pymethods]
impl Packet {
#[staticmethod]
fn from_bytes_py(bytes: &[u8], encap: EncapType) -> PyResult<Self> {
let _ = crate::layers::register_defaults();

Self::from_bytes(bytes, encap).map_err(|e| e.into())
}

fn as_json(&self) -> PyResult<String> {
Ok(serde_json::to_string_pretty(self).unwrap())
fn as_json(&self) -> PyResult<String> {
Ok(serde_json::to_string_pretty(self).unwrap())
}
}
}

#[cfg(feature = "python-bindings")]
pub(crate) fn register(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<Packet>()?;
Ok(())
pub(crate) fn register(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<Packet>()?;
Ok(())
}
}

#[cfg(test)]
mod tests {

#[cfg(feature = "wasm")]
use wasm_bindgen_test::wasm_bindgen_test;
cfg_wasm! {
use wasm_bindgen_test::wasm_bindgen_test;
}

use super::*;
use hex;
Expand Down

0 comments on commit edfe18b

Please sign in to comment.