Skip to content

Commit

Permalink
Cleaned up a lot of config specific code
Browse files Browse the repository at this point in the history
In a number of places we were using `cfg(not(...))` or `cfg(all(...))`
like code, which was not required. The features `python-bindings` and
`wasm` are mutually exclusive and that is checked once already, at all
other places those checks made this look ugly.

Signed-off-by: Abhijit Gadgil <gabhijit@iitbombay.org>
  • Loading branch information
gabhijit committed Jun 26, 2024
1 parent ce15480 commit 6013d51
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
14 changes: 6 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ serde_json = { version = "1.0" }
erased-serde = "0.4"
log = { version = "0.4", optional = true }

[target.'cfg(target_family = "wasm")'.dependencies]
wasm-bindgen = { version = "0.2", optional=true}
# Required for feature - python-bindings
pyo3 = { version = "0.20", features = ["extension-module"], optional = true}


[target.'cfg(not(target_family = "wasm"))'.dependencies]
pyo3 = { version = "0.20", features = ["extension-module"], optional=true}
# Required for feature - wasm
wasm-bindgen = { version = "0.2" , optional = true}

[dev-dependencies]
clap = { version = "4.0" , features = ["derive"] }
Expand All @@ -34,8 +33,8 @@ clap = { version = "4.0" , features = ["derive"] }
pcap = { version = "1.3"}
criterion = "0.5"

[target.'cfg(target_family = "wasm")'.dev-dependencies]
wasm-bindgen-test = { version = "0.3"}
[target.'cfg(target_family= "wasm")'.dev-dependencies]
wasm-bindgen-test = { version = "0.3" }

# Required by `cargo flamegraph`
[profile.bench]
Expand Down Expand Up @@ -75,5 +74,4 @@ walkdir = "2"
python-bindings = ['pyo3']
logging = ["log"]
sculpting = []

wasm = ['wasm-bindgen']
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl core::fmt::Display for Error {
}

// Python Bindings
#[cfg(all(feature = "python-bindings", not(target_family = "wasm")))]
#[cfg(feature = "python-bindings")]
impl std::convert::From<Error> for pyo3::PyErr {
// TODO: Add proper error reporting
fn from(_e: Error) -> pyo3::PyErr {
Expand Down
13 changes: 7 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ compile_error!(
"feature \"python-bindings\" and feature \"wasm\" cannot be enabled at the same time"
);

#[cfg(all(target_arch = "wasm32", not(feature = "wasm")))]
#[cfg(all(target_family = "wasm", not(feature = "wasm")))]
compile_error!("feature \"wasm\" is required for \"wasm32\" targets.");

#[cfg(all(not(target_arch = "wasm32"), feature = "wasm"))]
/* #[cfg(all(not(target_family = "wasm"), feature = "wasm"))]
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.");
Expand Down Expand Up @@ -76,21 +77,21 @@ pub use packet::Packet;
#[doc(inline)]
pub use types::{ENCAP_TYPE_ETH, ENCAP_TYPE_LINUX_SLL, ENCAP_TYPE_LINUX_SLL2};

#[cfg(all(feature = "python-bindings", not(target_family = "wasm")))]
#[cfg(feature = "python-bindings")]
use pyo3::prelude::*;

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

#[cfg(all(not(feature = "python-bindings"), target_family = "wasm"))]
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

#[cfg(all(not(feature = "python-bindings"), target_family = "wasm"))]
#[cfg(feature = "wasm")]
#[wasm_bindgen]
pub fn dissect_packet(packet: String) -> String {
let _ = layers::register_defaults();
Expand Down
8 changes: 4 additions & 4 deletions src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::errors::Error;
use crate::types::{EncapType, LayerCreatorFn};
use crate::Layer;

#[cfg(all(feature = "python-bindings", not(target_family = "wasm")))]
#[cfg(feature = "python-bindings")]
use pyo3::prelude::*;

fn get_encap_types_map() -> &'static RwLock<HashMap<EncapType, LayerCreatorFn>> {
Expand Down Expand Up @@ -44,7 +44,7 @@ pub(crate) fn register_defaults() -> Result<(), Error> {
/// Each of the following is a Layer - `Ethernet`, `IPv4`, `TCP` etc.
/// * `unprocessed`: The part of the original byte-stream that is not processed and captured into
/// `layers` above.
#[cfg_attr(all(feature = "python-bindings", not(target_family = "wasm")), pyclass)]
#[cfg_attr(feature = "python-bindings", pyclass)]
#[derive(Debug, Default, Serialize)]
pub struct Packet {
pub meta: PacketMetadata,
Expand Down Expand Up @@ -177,7 +177,7 @@ impl Packet {

// Python Bindings
#[allow(clippy::borrow_deref_ref)]
#[cfg(all(feature = "python-bindings", not(target_family = "wasm")))]
#[cfg(feature = "python-bindings")]
#[pymethods]
impl Packet {
#[staticmethod]
Expand All @@ -192,7 +192,7 @@ impl Packet {
}
}

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

0 comments on commit 6013d51

Please sign in to comment.