diff --git a/.github/workflows/docs.yaml b/.github/workflows/docs.yaml index b0164ac9..1cf7e868 100644 --- a/.github/workflows/docs.yaml +++ b/.github/workflows/docs.yaml @@ -1,7 +1,7 @@ name: Docs on: push: - branches: [main] + branches: [ main ] workflow_dispatch: permissions: contents: read @@ -27,7 +27,7 @@ jobs: - name: Clean docs folder run: cargo clean --doc - name: Build docs - run: cargo doc --no-deps + run: cargo doc --all-features --no-deps - name: Add redirect run: echo '' > target/doc/index.html - name: Remove lock file diff --git a/falco_event/Cargo.toml b/falco_event/Cargo.toml index c6a824fe..0b383d8e 100644 --- a/falco_event/Cargo.toml +++ b/falco_event/Cargo.toml @@ -10,6 +10,9 @@ readme = "README.md" keywords = ["falco", "security"] categories = ["api-bindings"] +[features] +serde = ["dep:serde", "bitflags/serde", "falco_event_derive/serde"] + [dependencies] byteorder = "1.5.0" falco_event_derive = { path = "../falco_event_derive", version = "0.4.0" } @@ -17,10 +20,10 @@ memchr = "2.7.1" num-derive = "0.4.2" num-traits = "0.2.17" thiserror = "1.0.58" -bitflags = { version = "2.4.2", features = ["serde"] } +bitflags = { version = "2.4.2" } anyhow = "1.0.81" chrono = "0.4.38" -serde = { version = "1.0.210", features = ["derive"] } +serde = { version = "1.0.210", features = ["derive"], optional = true } [target.'cfg(target_os = "linux")'.dependencies] nix = { version = "0.29.0", features = ["signal"] } diff --git a/falco_event/src/events/event.rs b/falco_event/src/events/event.rs index cce6ccca..29b01571 100644 --- a/falco_event/src/events/event.rs +++ b/falco_event/src/events/event.rs @@ -41,6 +41,7 @@ where } } +#[cfg(feature = "serde")] mod serde_event { use super::*; use crate::events::types::AnyEvent; diff --git a/falco_event/src/events/metadata.rs b/falco_event/src/events/metadata.rs index c74e8930..639c569e 100644 --- a/falco_event/src/events/metadata.rs +++ b/falco_event/src/events/metadata.rs @@ -1,9 +1,9 @@ use crate::event_derive::Format; -use serde::{Deserialize, Serialize}; use std::fmt::{Debug, Formatter}; use std::time::{Duration, SystemTime, UNIX_EPOCH}; -#[derive(Clone, Deserialize, Serialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(Clone)] pub struct EventMetadata { pub ts: u64, pub tid: i64, diff --git a/falco_event/src/events/mod.rs b/falco_event/src/events/mod.rs index 62483c20..c4cb0282 100644 --- a/falco_event/src/events/mod.rs +++ b/falco_event/src/events/mod.rs @@ -11,7 +11,7 @@ mod event; mod metadata; pub(crate) mod payload; mod raw_event; -#[cfg(test)] +#[cfg(all(test, feature = "serde"))] mod serde_tests; mod to_bytes; @@ -51,12 +51,15 @@ mod to_bytes; /// |-----------------------------------------------------------|-----------------|--------------| /// | Loading from a [RawEvent](`crate::events::RawEvent`) | supported | ^1 | /// | [writing to a byte buffer](`crate::events::EventToBytes`) | supported | supported | -/// | [arbitrary serialization](`serde::Serialize`) | supported | supported | -/// | [arbitrary deserialization](`serde::Deserialize`) | | supported | +/// | [arbitrary serialization](`serde::Serialize`) ^2 | supported | supported | +/// | [arbitrary deserialization](`serde::Deserialize`) ^2 | | supported | /// /// **Footnotes**: /// /// 1. Loading an owned event from a raw event is technically possible but has no benefits over /// loading a borrowed event and incurs extra allocations and copies, so to avoid the confusion /// it's explicitly not supported. +/// +/// 2. Arbitrary serialization and deserialization with [`serde`] is only supported when +/// the `serde` feature of the crate is enabled. pub mod types; diff --git a/falco_event/src/lib.rs b/falco_event/src/lib.rs index d30d2840..ba8229e9 100644 --- a/falco_event/src/lib.rs +++ b/falco_event/src/lib.rs @@ -48,5 +48,6 @@ mod event_derive { pub use crate::types::BorrowDeref; pub use crate::types::Borrowed; + #[cfg(feature = "serde")] pub use crate::types::serde; } diff --git a/falco_event/src/types/bytebuf.rs b/falco_event/src/types/bytebuf.rs index e31ea670..b08e84ab 100644 --- a/falco_event/src/types/bytebuf.rs +++ b/falco_event/src/types/bytebuf.rs @@ -84,6 +84,7 @@ impl BorrowDeref for Vec { } } +#[cfg(feature = "serde")] pub mod serde { pub mod bytebuf { use crate::types::utf_chunked::{OwnedUtfChunked, UtfChunked}; diff --git a/falco_event/src/types/fd_list.rs b/falco_event/src/types/fd_list.rs index e189b1fb..e5661613 100644 --- a/falco_event/src/types/fd_list.rs +++ b/falco_event/src/types/fd_list.rs @@ -5,10 +5,10 @@ use crate::fields::event_flags::PT_FLAGS16_file_flags; use crate::fields::{FromBytes, FromBytesResult, ToBytes}; use crate::types::format::Format; use byteorder::{NativeEndian, ReadBytesExt, WriteBytesExt}; -use serde::{Deserialize, Serialize}; /// A list of file descriptors with flags -#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct FdList(pub Vec<(u64, PT_FLAGS16_file_flags)>); impl Format for FdList @@ -92,6 +92,11 @@ mod tests { assert_eq!(fdlist, fdlist2) } +} + +#[cfg(all(test, feature = "serde"))] +mod serde_tests { + use super::*; #[test] fn test_serde_fd_list() { diff --git a/falco_event/src/types/mod.rs b/falco_event/src/types/mod.rs index e8911cc8..b3253daa 100644 --- a/falco_event/src/types/mod.rs +++ b/falco_event/src/types/mod.rs @@ -6,8 +6,10 @@ mod path; mod primitive; mod string; mod time; +#[cfg(feature = "serde")] mod utf_chunked; +#[cfg(feature = "serde")] pub mod serde { pub use super::bytebuf::serde::*; pub use super::string::serde::*; diff --git a/falco_event/src/types/net/endpoint.rs b/falco_event/src/types/net/endpoint.rs index efbbe7c5..3949c8b2 100644 --- a/falco_event/src/types/net/endpoint.rs +++ b/falco_event/src/types/net/endpoint.rs @@ -65,8 +65,8 @@ impl Format for EndpointV6 { } } -#[cfg(test)] -mod tests { +#[cfg(all(test, feature = "serde"))] +mod serde_tests { use super::Port; use std::net::{Ipv4Addr, Ipv6Addr}; diff --git a/falco_event/src/types/net/ipnet.rs b/falco_event/src/types/net/ipnet.rs index 133beb26..5d135571 100644 --- a/falco_event/src/types/net/ipnet.rs +++ b/falco_event/src/types/net/ipnet.rs @@ -8,7 +8,8 @@ use std::net::IpAddr; /// /// This is a wrapper around [IpAddr] that makes it a distinct type, suitable for storing /// IP (v4 or v6) subnets. -#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct IpNet(pub IpAddr); impl FromBytes<'_> for IpNet { @@ -40,8 +41,8 @@ where } } -#[cfg(test)] -mod tests { +#[cfg(all(test, feature = "serde"))] +mod serde_tests { use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; #[test] diff --git a/falco_event/src/types/net/ipv4net.rs b/falco_event/src/types/net/ipv4net.rs index 894540e1..d7b35a88 100644 --- a/falco_event/src/types/net/ipv4net.rs +++ b/falco_event/src/types/net/ipv4net.rs @@ -1,15 +1,15 @@ +use crate::event_derive::{FromBytes, FromBytesResult, ToBytes}; +use crate::types::format::Format; use std::fmt::Formatter; use std::io::Write; use std::net::Ipv4Addr; -use crate::event_derive::{FromBytes, FromBytesResult, ToBytes}; -use crate::types::format::Format; - /// An IPv4 network /// /// This is a wrapper around [Ipv4Addr] that makes it a distinct type, suitable for storing /// IPv4 subnets. -#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct Ipv4Net(pub Ipv4Addr); impl FromBytes<'_> for Ipv4Net { @@ -38,8 +38,8 @@ impl Format for Ipv4Net { } } -#[cfg(test)] -mod tests { +#[cfg(all(test, feature = "serde"))] +mod serde_tests { use std::net::Ipv4Addr; #[test] diff --git a/falco_event/src/types/net/ipv6net.rs b/falco_event/src/types/net/ipv6net.rs index 6e2248e9..29bc998e 100644 --- a/falco_event/src/types/net/ipv6net.rs +++ b/falco_event/src/types/net/ipv6net.rs @@ -1,15 +1,15 @@ +use crate::event_derive::{FromBytes, FromBytesResult, ToBytes}; +use crate::types::format::Format; use std::fmt::Formatter; use std::io::Write; use std::net::Ipv6Addr; -use crate::event_derive::{FromBytes, FromBytesResult, ToBytes}; -use crate::types::format::Format; - /// An IPv6 network /// /// This is a wrapper around [Ipv6Addr] that makes it a distinct type, suitable for storing /// IPv6 subnets. -#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct Ipv6Net(pub Ipv6Addr); impl FromBytes<'_> for Ipv6Net { @@ -38,8 +38,8 @@ impl Format for Ipv6Net { } } -#[cfg(test)] -mod tests { +#[cfg(all(test, feature = "serde"))] +mod serde_tests { use std::net::Ipv6Addr; #[test] diff --git a/falco_event/src/types/net/sockaddr.rs b/falco_event/src/types/net/sockaddr.rs index 4ee224d1..fca757d5 100644 --- a/falco_event/src/types/net/sockaddr.rs +++ b/falco_event/src/types/net/sockaddr.rs @@ -3,7 +3,6 @@ use crate::ffi::{PPM_AF_INET, PPM_AF_INET6, PPM_AF_LOCAL, PPM_AF_UNSPEC}; use crate::types::format::Format; use crate::types::{Borrow, Borrowed, EndpointV4, EndpointV6}; use byteorder::{ReadBytesExt, WriteBytesExt}; -use serde::{Deserialize, Serialize}; use std::ffi::OsStr; use std::fmt::Formatter; use std::io::Write; @@ -11,8 +10,9 @@ use std::os::unix::ffi::OsStrExt; use std::path::{Path, PathBuf}; /// A socket address -#[derive(Debug, Serialize)] -#[serde(rename_all = "lowercase")] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))] +#[derive(Debug)] pub enum SockAddr<'a> { /// Unix sockets Unix(&'a Path), @@ -24,7 +24,10 @@ pub enum SockAddr<'a> { V6(EndpointV6), /// any other address family is represented as the number (`PPM_AF_*` constant) and the raw data - Other(u8, #[serde(with = "crate::types::serde::bytebuf")] &'a [u8]), + Other( + u8, + #[cfg_attr(feature = "serde", serde(with = "crate::types::serde::bytebuf"))] &'a [u8], + ), } impl ToBytes for SockAddr<'_> { @@ -106,8 +109,9 @@ where } /// A socket address (owned) -#[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "lowercase")] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))] +#[derive(Debug)] pub enum OwnedSockAddr { /// Unix sockets Unix(PathBuf), @@ -119,7 +123,10 @@ pub enum OwnedSockAddr { V6(EndpointV6), /// any other address family is represented as the number (`PPM_AF_*` constant) and the raw data - Other(u8, #[serde(with = "crate::types::serde::bytebuf")] Vec), + Other( + u8, + #[cfg_attr(feature = "serde", serde(with = "crate::types::serde::bytebuf"))] Vec, + ), } impl<'a> Borrowed for SockAddr<'a> { @@ -139,7 +146,7 @@ impl Borrow for OwnedSockAddr { } } -#[cfg(test)] +#[cfg(all(test, feature = "serde"))] mod tests { use crate::types::{OwnedSockAddr, Port, SockAddr}; use std::net::{Ipv4Addr, Ipv6Addr}; diff --git a/falco_event/src/types/net/socktuple.rs b/falco_event/src/types/net/socktuple.rs index ca6a5f67..53370aac 100644 --- a/falco_event/src/types/net/socktuple.rs +++ b/falco_event/src/types/net/socktuple.rs @@ -8,11 +8,11 @@ use crate::types::format::Format; use crate::types::net::endpoint::{EndpointV4, EndpointV6}; use crate::types::{Borrow, Borrowed}; use byteorder::{ReadBytesExt, WriteBytesExt}; -use serde::{Deserialize, Serialize}; /// Socket tuple: describing both endpoints of a connection -#[derive(Debug, Eq, PartialEq, Serialize)] -#[serde(rename_all = "lowercase")] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))] +#[derive(Debug, Eq, PartialEq)] pub enum SockTuple<'a> { /// Unix socket connection Unix { @@ -41,7 +41,10 @@ pub enum SockTuple<'a> { }, /// Unknown/other socket family: `PPM_AF_*` id and a raw byte buffer - Other(u8, #[serde(with = "crate::types::serde::bytebuf")] &'a [u8]), + Other( + u8, + #[cfg_attr(feature = "serde", serde(with = "crate::types::serde::bytebuf"))] &'a [u8], + ), } impl Display for SockTuple<'_> { @@ -173,8 +176,9 @@ where } /// Socket tuple: describing both endpoints of a connection (owned) -#[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "lowercase")] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))] +#[derive(Debug)] pub enum OwnedSockTuple { /// Unix socket connection Unix { @@ -203,7 +207,10 @@ pub enum OwnedSockTuple { }, /// Unknown/other socket family: `PPM_AF_*` id and a raw byte buffer - Other(u8, #[serde(with = "crate::types::serde::bytebuf")] Vec), + Other( + u8, + #[cfg_attr(feature = "serde", serde(with = "crate::types::serde::bytebuf"))] Vec, + ), } impl<'a> Borrowed for SockTuple<'a> { @@ -324,6 +331,14 @@ mod tests { assert_eq!(binary, binary2.as_slice(),); } +} + +#[cfg(all(test, feature = "serde"))] +mod serde_tests { + use crate::types::{OwnedSockTuple, Port, SockTuple}; + use std::net::{Ipv4Addr, Ipv6Addr}; + use std::path::Path; + use std::str::FromStr; #[test] fn test_serde_socktuple_unix() { diff --git a/falco_event/src/types/path/relative_path.rs b/falco_event/src/types/path/relative_path.rs index 6bf5ab73..be4fb947 100644 --- a/falco_event/src/types/path/relative_path.rs +++ b/falco_event/src/types/path/relative_path.rs @@ -1,7 +1,6 @@ use crate::event_derive::{FromBytes, FromBytesResult, ToBytes}; use crate::types::format::Format; use crate::types::{Borrow, Borrowed}; -use serde::{Deserialize, Serialize}; use std::fmt::Formatter; use std::io::Write; use std::os::unix::ffi::OsStrExt; @@ -12,7 +11,8 @@ use std::path::{Path, PathBuf}; /// Events containing a parameter of this type will have an extra method available, derived /// from the field name. For example, if the field is called `name`, the event type will have /// a method called `name_dirfd` that returns the corresponding `dirfd` (as an `Option`) -#[derive(Debug, Serialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +#[derive(Debug)] pub struct RelativePath<'a>(pub &'a Path); impl<'a> ToBytes for RelativePath<'a> { @@ -51,7 +51,8 @@ where /// Events containing a parameter of this type will have an extra method available, derived /// from the field name. For example, if the field is called `name`, the event type will have /// a method called `name_dirfd` that returns the corresponding `dirfd` (as an `Option`) -#[derive(Debug, Deserialize, Serialize)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[derive(Debug)] pub struct OwnedRelativePath(pub PathBuf); impl<'a> Borrowed for RelativePath<'a> { @@ -68,12 +69,16 @@ impl Borrow for OwnedRelativePath { #[cfg(test)] mod tests { - use std::path::{Path, PathBuf}; + use std::path::PathBuf; use std::str::FromStr; use crate::event_derive::{FromBytes, ToBytes}; use crate::types::path::relative_path::RelativePath; + + #[cfg(feature = "serde")] use crate::types::OwnedRelativePath; + #[cfg(feature = "serde")] + use std::path::Path; #[test] fn test_relative_path() { @@ -91,6 +96,7 @@ mod tests { assert_eq!(path.0.to_str().unwrap(), "/foo"); } + #[cfg(feature = "serde")] #[test] fn test_serde_relative_path() { let path = RelativePath(Path::new("/foo")); diff --git a/falco_event/src/types/primitive/newtypes.rs b/falco_event/src/types/primitive/newtypes.rs index 24d550d5..3824b21a 100644 --- a/falco_event/src/types/primitive/newtypes.rs +++ b/falco_event/src/types/primitive/newtypes.rs @@ -21,9 +21,8 @@ macro_rules! newtype { ($(#[$attr:meta])* $name:ident($repr:ty)) => { $(#[$attr])* #[derive(Default, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)] - #[derive(serde::Deserialize)] - #[derive(serde::Serialize)] - #[serde(transparent)] + #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] + #[cfg_attr(feature = "serde", serde(transparent))] pub struct $name(pub $repr); impl FromBytes<'_> for $name { @@ -255,8 +254,8 @@ impl Format for Bool { } } -#[cfg(test)] -mod tests { +#[cfg(all(test, feature = "serde"))] +mod serde_tests { use crate::types::SyscallResult; #[test] diff --git a/falco_event/src/types/string/cstr.rs b/falco_event/src/types/string/cstr.rs index 0984bf96..31fa4395 100644 --- a/falco_event/src/types/string/cstr.rs +++ b/falco_event/src/types/string/cstr.rs @@ -50,6 +50,7 @@ impl Borrow for CString { } } +#[cfg(feature = "serde")] pub mod serde { #[allow(dead_code)] pub mod cstr { diff --git a/falco_event/src/types/string/cstr_array.rs b/falco_event/src/types/string/cstr_array.rs index d83f0934..d2fad8f5 100644 --- a/falco_event/src/types/string/cstr_array.rs +++ b/falco_event/src/types/string/cstr_array.rs @@ -67,6 +67,7 @@ impl Borrow for Vec { } } +#[cfg(feature = "serde")] pub mod serde { #[allow(dead_code)] pub mod cstr_array { diff --git a/falco_event/src/types/string/cstr_pair_array.rs b/falco_event/src/types/string/cstr_pair_array.rs index 43799b16..1dca0790 100644 --- a/falco_event/src/types/string/cstr_pair_array.rs +++ b/falco_event/src/types/string/cstr_pair_array.rs @@ -80,6 +80,7 @@ impl Borrow for Vec<(CString, CString)> { } } +#[cfg(feature = "serde")] pub mod serde { #[allow(dead_code)] pub mod cstr_pair_array { diff --git a/falco_event/src/types/string/mod.rs b/falco_event/src/types/string/mod.rs index bf7784b9..954be288 100644 --- a/falco_event/src/types/string/mod.rs +++ b/falco_event/src/types/string/mod.rs @@ -2,6 +2,7 @@ mod cstr; mod cstr_array; mod cstr_pair_array; +#[cfg(feature = "serde")] pub mod serde { pub use super::cstr::serde::*; pub use super::cstr_array::serde::*; diff --git a/falco_event_derive/Cargo.toml b/falco_event_derive/Cargo.toml index 5d1e267b..e735518e 100644 --- a/falco_event_derive/Cargo.toml +++ b/falco_event_derive/Cargo.toml @@ -12,6 +12,9 @@ keywords = ["falco", "security"] [lib] proc-macro = true +[features] +serde = [] + [dependencies] quote = "1" proc-macro2 = "1.0" diff --git a/falco_event_derive/src/dynamic_params.rs b/falco_event_derive/src/dynamic_params.rs index 6bac5bc5..de713b81 100644 --- a/falco_event_derive/src/dynamic_params.rs +++ b/falco_event_derive/src/dynamic_params.rs @@ -1,5 +1,4 @@ use crate::event_info::{lifetime_type, LifetimeType}; -use crate::serde_custom::serde_with_tag; use proc_macro::TokenStream; use proc_macro2::Ident; use quote::quote; @@ -8,6 +7,14 @@ use syn::punctuated::Punctuated; use syn::token::{Brace, Bracket}; use syn::{braced, bracketed, parse_macro_input, LitInt, Token}; +#[cfg(feature = "serde")] +use crate::serde_custom::serde_with_tag; + +#[cfg(not(feature = "serde"))] +fn serde_with_tag(_ty: &Ident) -> Option { + None +} + struct DynamicParamVariant { _brackets: syn::token::Bracket, discriminant: Ident, @@ -191,6 +198,8 @@ impl DynamicParam { } else { quote!() }; + + #[cfg(feature = "serde")] let derives = if wants_lifetime { quote!(#[derive(serde::Serialize)]) } else { @@ -200,6 +209,14 @@ impl DynamicParam { #[derive(serde::Serialize)] ) }; + + #[cfg(not(feature = "serde"))] + let derives = if wants_lifetime { + None + } else { + Some(quote!(#[derive(Clone)])) + }; + let to_owned = if wants_lifetime { Some(quote!( impl #lifetime crate::event_derive::Borrowed for #name #lifetime { @@ -273,12 +290,20 @@ impl DynamicParam { ) }); + #[cfg(feature = "serde")] + let serde_derives = quote!( + #[derive(serde::Deserialize)] + #[derive(serde::Serialize)] + ); + + #[cfg(not(feature = "serde"))] + let serde_derives = quote!(); + if wants_lifetime { quote!( #[allow(non_camel_case_types)] #[derive(Debug)] - #[derive(serde::Deserialize)] - #[derive(serde::Serialize)] + #serde_derives pub enum #name { #(#variant_definitions,)* } diff --git a/falco_event_derive/src/event_flags.rs b/falco_event_derive/src/event_flags.rs index 1d77374b..254c6d63 100644 --- a/falco_event_derive/src/event_flags.rs +++ b/falco_event_derive/src/event_flags.rs @@ -133,13 +133,21 @@ fn render_enum( .clone() .map(|(variant, value)| quote!(#name::#variant => crate::ffi::#value as #repr_type)); + #[cfg(feature = "serde")] + let serde_derives = quote!( + #[derive(serde::Deserialize)] + #[derive(serde::Serialize)] + ); + + #[cfg(not(feature = "serde"))] + let serde_derives = quote!(); + quote!( #[repr(#repr_type)] #[allow(non_camel_case_types)] #[non_exhaustive] #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] - #[derive(serde::Deserialize)] - #[derive(serde::Serialize)] + #serde_derives pub enum #name { #(#tags,)* Unknown(usize), @@ -214,12 +222,21 @@ fn render_bitflags( items: impl Iterator, ) -> proc_macro2::TokenStream { let items = items.map(|(name, value)| quote!(const #name = crate::ffi::#value as #repr_type)); + + #[cfg(feature = "serde")] + let serde_derives = quote!( + #[derive(serde::Deserialize)] + #[derive(serde::Serialize)] + ); + + #[cfg(not(feature = "serde"))] + let serde_derives = quote!(); + quote!( bitflags::bitflags! { #[allow(non_camel_case_types)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] - #[derive(serde::Deserialize)] - #[derive(serde::Serialize)] + #serde_derives pub struct #name: #repr_type { #(#items;)* const _ = !0; diff --git a/falco_event_derive/src/event_info.rs b/falco_event_derive/src/event_info.rs index fc1b47c0..219ebcd6 100644 --- a/falco_event_derive/src/event_info.rs +++ b/falco_event_derive/src/event_info.rs @@ -1,10 +1,22 @@ -use crate::serde_custom::{serde_with_option_tag, serde_with_option_tag_owned}; use proc_macro::TokenStream; use proc_macro2::Ident; use quote::quote; use syn::parse::{Parse, ParseStream}; use syn::{braced, bracketed, parse_macro_input, Token}; +#[cfg(feature = "serde")] +use crate::serde_custom::{serde_with_option_tag, serde_with_option_tag_owned}; + +#[cfg(not(feature = "serde"))] +fn serde_with_option_tag(_ty: &Ident) -> Option { + None +} + +#[cfg(not(feature = "serde"))] +fn serde_with_option_tag_owned(_ty: &Ident) -> Option { + None +} + pub(crate) enum LifetimeType { None, Ref, @@ -267,22 +279,37 @@ impl EventInfo { let is_large = self.flags.iter().any(|flag| *flag == "EF_LARGE_PAYLOAD"); let name = &self.name; + #[cfg(feature = "serde")] + let derive_serde = quote!( + #[derive(serde::Deserialize)] + #[derive(serde::Serialize)] + ); + + #[cfg(not(feature = "serde"))] + let derive_serde = quote!(); + + #[cfg(feature = "serde")] + let derive_ser = quote!( + #[derive(serde::Serialize)] + ); + + #[cfg(not(feature = "serde"))] + let derive_ser = quote!(); + let derives = match (variant, wants_lifetime) { (CodegenVariant::Borrowed, true) => quote!( #[derive(falco_event_derive::FromBytes)] #[derive(falco_event_derive::ToBytes)] - #[derive(serde::Serialize)] + #derive_ser ), (CodegenVariant::Borrowed, false) => quote!( #[derive(falco_event_derive::FromBytes)] #[derive(falco_event_derive::ToBytes)] - #[derive(serde::Serialize)] - #[derive(serde::Deserialize)] + #derive_serde ), (CodegenVariant::Owned, _) => quote!( #[derive(falco_event_derive::ToBytes)] - #[derive(serde::Deserialize)] - #[derive(serde::Serialize)] + #derive_serde ), }; @@ -443,6 +470,7 @@ fn event_info_variant(events: &Events, variant: CodegenVariant) -> proc_macro2:: CodegenVariant::Owned => None, }; + #[cfg(feature = "serde")] let derives = match variant { CodegenVariant::Borrowed => quote!( #[derive(serde::Serialize)] @@ -453,6 +481,9 @@ fn event_info_variant(events: &Events, variant: CodegenVariant) -> proc_macro2:: ), }; + #[cfg(not(feature = "serde"))] + let derives = quote!(); + quote!( #(#typedefs)* diff --git a/falco_event_derive/src/lib.rs b/falco_event_derive/src/lib.rs index 507b921f..e0d75311 100644 --- a/falco_event_derive/src/lib.rs +++ b/falco_event_derive/src/lib.rs @@ -4,6 +4,7 @@ mod binary_payload; mod dynamic_params; mod event_flags; mod event_info; +#[cfg(feature = "serde")] mod serde_custom; #[proc_macro_derive(ToBytes)]