From 4598fb33e4a7ec40f81b6ae3cf6abd794cb0e871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 20 Dec 2020 17:24:08 +0100 Subject: [PATCH] Add the opensmtpd_derive crate --- Cargo.toml | 29 ++++--------------- opensmtpd-derive/Cargo.toml | 20 +++++++++++++ opensmtpd-derive/src/lib.rs | 18 ++++++++++++ opensmtpd/Cargo.toml | 25 ++++++++++++++++ {examples => opensmtpd/examples}/counter.rs | 7 +++-- .../examples}/samples/empty.log | 0 .../examples}/samples/single_session.log | 0 {src => opensmtpd/src}/data_line.rs | 0 .../src}/data_structures/address.rs | 0 .../src}/data_structures/auth_result.rs | 0 .../src}/data_structures/event.rs | 0 .../src}/data_structures/filter_kind.rs | 0 .../src}/data_structures/filter_phase.rs | 0 .../src}/data_structures/filter_response.rs | 0 .../src}/data_structures/mail_result.rs | 0 .../src}/data_structures/method.rs | 0 {src => opensmtpd/src}/data_structures/mod.rs | 0 .../src}/data_structures/smtp_status.rs | 0 .../src}/data_structures/subsystem.rs | 0 .../src}/data_structures/timeval.rs | 0 {src => opensmtpd/src}/error.rs | 0 {src => opensmtpd/src}/filter.rs | 0 {src => opensmtpd/src}/io.rs | 0 {src => opensmtpd/src}/lib.rs | 21 ++++++-------- {src => opensmtpd/src}/parsers/entry.rs | 0 {src => opensmtpd/src}/parsers/handshake.rs | 0 {src => opensmtpd/src}/parsers/mod.rs | 0 {src => opensmtpd/src}/parsers/parameters.rs | 0 {src => opensmtpd/src}/process.rs | 0 29 files changed, 81 insertions(+), 39 deletions(-) create mode 100644 opensmtpd-derive/Cargo.toml create mode 100644 opensmtpd-derive/src/lib.rs create mode 100644 opensmtpd/Cargo.toml rename {examples => opensmtpd/examples}/counter.rs (88%) rename {examples => opensmtpd/examples}/samples/empty.log (100%) rename {examples => opensmtpd/examples}/samples/single_session.log (100%) rename {src => opensmtpd/src}/data_line.rs (100%) rename {src => opensmtpd/src}/data_structures/address.rs (100%) rename {src => opensmtpd/src}/data_structures/auth_result.rs (100%) rename {src => opensmtpd/src}/data_structures/event.rs (100%) rename {src => opensmtpd/src}/data_structures/filter_kind.rs (100%) rename {src => opensmtpd/src}/data_structures/filter_phase.rs (100%) rename {src => opensmtpd/src}/data_structures/filter_response.rs (100%) rename {src => opensmtpd/src}/data_structures/mail_result.rs (100%) rename {src => opensmtpd/src}/data_structures/method.rs (100%) rename {src => opensmtpd/src}/data_structures/mod.rs (100%) rename {src => opensmtpd/src}/data_structures/smtp_status.rs (100%) rename {src => opensmtpd/src}/data_structures/subsystem.rs (100%) rename {src => opensmtpd/src}/data_structures/timeval.rs (100%) rename {src => opensmtpd/src}/error.rs (100%) rename {src => opensmtpd/src}/filter.rs (100%) rename {src => opensmtpd/src}/io.rs (100%) rename {src => opensmtpd/src}/lib.rs (94%) rename {src => opensmtpd/src}/parsers/entry.rs (100%) rename {src => opensmtpd/src}/parsers/handshake.rs (100%) rename {src => opensmtpd/src}/parsers/mod.rs (100%) rename {src => opensmtpd/src}/parsers/parameters.rs (100%) rename {src => opensmtpd/src}/process.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 80aa08c..2bb5d5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,24 +1,5 @@ -[package] -name = "opensmtpd" -version = "0.3.0" -authors = ["Rodolphe Bréard "] -edition = "2018" -description = "Interface for OpenSMTPD filters" -keywords = ["opensmtpd", "filter", "mail"] -documentation = "https://docs.rs/opensmtpd/" -repository = "https://github.com/breard-r/rust-opensmtpd" -readme = "README.md" -license = "MIT OR Apache-2.0" -include = ["src/**/*", "Cargo.toml", "LICENSE-*.txt"] - -[dependencies] -log = "0.4" -nom = "6.0" -pretty-hex = "0.2" - -[dev-dependencies] -simplelog = "0.9" - -[[example]] -name = "counter" -path = "examples/counter.rs" +[workspace] +members = [ + "opensmtpd", + "opensmtpd-derive" +] diff --git a/opensmtpd-derive/Cargo.toml b/opensmtpd-derive/Cargo.toml new file mode 100644 index 0000000..641ccbf --- /dev/null +++ b/opensmtpd-derive/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "opensmtpd_derive" +version = "0.3.0" +authors = ["Rodolphe Bréard "] +edition = "2018" +description = "Interface for OpenSMTPD filters" +keywords = ["opensmtpd", "filter", "mail"] +documentation = "https://docs.rs/opensmtpd-derive/" +repository = "https://github.com/breard-r/rust-opensmtpd" +readme = "README.md" +license = "MIT OR Apache-2.0" +include = ["src/**/*", "Cargo.toml", "../LICENSE-*.txt"] + +[lib] +proc-macro = true + +[dependencies] +proc-macro2 = "1.0" +quote = "1.0" +syn = { version = "1.0", features = ["full", "extra-traits"] } diff --git a/opensmtpd-derive/src/lib.rs b/opensmtpd-derive/src/lib.rs new file mode 100644 index 0000000..24f0c85 --- /dev/null +++ b/opensmtpd-derive/src/lib.rs @@ -0,0 +1,18 @@ +use proc_macro::TokenStream; +use proc_macro2::Span; +use quote::quote; +use syn::{parse_macro_input, Ident, ItemFn}; + +#[proc_macro_attribute] +pub fn register(_attr: TokenStream, input: TokenStream) -> TokenStream { + let item = parse_macro_input!(input as ItemFn); + let fn_name = item.sig.ident.to_string().replacen("on_", "has_", 1); + let fn_name = Ident::new(&fn_name, Span::call_site()); + let output = quote! { + fn #fn_name(&self) -> bool { + true + } + #item + }; + output.into() +} diff --git a/opensmtpd/Cargo.toml b/opensmtpd/Cargo.toml new file mode 100644 index 0000000..e32dd37 --- /dev/null +++ b/opensmtpd/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "opensmtpd" +version = "0.3.0" +authors = ["Rodolphe Bréard "] +edition = "2018" +description = "Interface for OpenSMTPD filters" +keywords = ["opensmtpd", "filter", "mail"] +documentation = "https://docs.rs/opensmtpd/" +repository = "https://github.com/breard-r/rust-opensmtpd" +readme = "README.md" +license = "MIT OR Apache-2.0" +include = ["src/**/*", "Cargo.toml", "../LICENSE-*.txt"] + +[dependencies] +log = "0.4" +nom = "6.0" +opensmtpd_derive = { path = "../opensmtpd-derive" } +pretty-hex = "0.2" + +[dev-dependencies] +simplelog = "0.9" + +[[example]] +name = "counter" +path = "examples/counter.rs" diff --git a/examples/counter.rs b/opensmtpd/examples/counter.rs similarity index 88% rename from examples/counter.rs rename to opensmtpd/examples/counter.rs index c6d8e98..5c1ad95 100644 --- a/examples/counter.rs +++ b/opensmtpd/examples/counter.rs @@ -1,5 +1,6 @@ use log; -use opensmtpd::{register, run_filter, Address, Filter, ReportEntry}; +use opensmtpd::{run_filter, Address, Filter, ReportEntry}; +use opensmtpd_derive::register; use simplelog::{Config, LevelFilter, WriteLogger}; use std::fs::File; @@ -12,7 +13,7 @@ struct MyCounter { } impl Filter for MyCounter { - register!(has_report_link_connect); + #[register] fn on_report_link_connect( &mut self, _entry: &ReportEntry, @@ -30,7 +31,7 @@ impl Filter for MyCounter { ); } - register!(has_report_link_disconnect); + #[register] fn on_report_link_disconnect(&mut self, _entry: &ReportEntry) { self.nb_connected -= 1; log::info!( diff --git a/examples/samples/empty.log b/opensmtpd/examples/samples/empty.log similarity index 100% rename from examples/samples/empty.log rename to opensmtpd/examples/samples/empty.log diff --git a/examples/samples/single_session.log b/opensmtpd/examples/samples/single_session.log similarity index 100% rename from examples/samples/single_session.log rename to opensmtpd/examples/samples/single_session.log diff --git a/src/data_line.rs b/opensmtpd/src/data_line.rs similarity index 100% rename from src/data_line.rs rename to opensmtpd/src/data_line.rs diff --git a/src/data_structures/address.rs b/opensmtpd/src/data_structures/address.rs similarity index 100% rename from src/data_structures/address.rs rename to opensmtpd/src/data_structures/address.rs diff --git a/src/data_structures/auth_result.rs b/opensmtpd/src/data_structures/auth_result.rs similarity index 100% rename from src/data_structures/auth_result.rs rename to opensmtpd/src/data_structures/auth_result.rs diff --git a/src/data_structures/event.rs b/opensmtpd/src/data_structures/event.rs similarity index 100% rename from src/data_structures/event.rs rename to opensmtpd/src/data_structures/event.rs diff --git a/src/data_structures/filter_kind.rs b/opensmtpd/src/data_structures/filter_kind.rs similarity index 100% rename from src/data_structures/filter_kind.rs rename to opensmtpd/src/data_structures/filter_kind.rs diff --git a/src/data_structures/filter_phase.rs b/opensmtpd/src/data_structures/filter_phase.rs similarity index 100% rename from src/data_structures/filter_phase.rs rename to opensmtpd/src/data_structures/filter_phase.rs diff --git a/src/data_structures/filter_response.rs b/opensmtpd/src/data_structures/filter_response.rs similarity index 100% rename from src/data_structures/filter_response.rs rename to opensmtpd/src/data_structures/filter_response.rs diff --git a/src/data_structures/mail_result.rs b/opensmtpd/src/data_structures/mail_result.rs similarity index 100% rename from src/data_structures/mail_result.rs rename to opensmtpd/src/data_structures/mail_result.rs diff --git a/src/data_structures/method.rs b/opensmtpd/src/data_structures/method.rs similarity index 100% rename from src/data_structures/method.rs rename to opensmtpd/src/data_structures/method.rs diff --git a/src/data_structures/mod.rs b/opensmtpd/src/data_structures/mod.rs similarity index 100% rename from src/data_structures/mod.rs rename to opensmtpd/src/data_structures/mod.rs diff --git a/src/data_structures/smtp_status.rs b/opensmtpd/src/data_structures/smtp_status.rs similarity index 100% rename from src/data_structures/smtp_status.rs rename to opensmtpd/src/data_structures/smtp_status.rs diff --git a/src/data_structures/subsystem.rs b/opensmtpd/src/data_structures/subsystem.rs similarity index 100% rename from src/data_structures/subsystem.rs rename to opensmtpd/src/data_structures/subsystem.rs diff --git a/src/data_structures/timeval.rs b/opensmtpd/src/data_structures/timeval.rs similarity index 100% rename from src/data_structures/timeval.rs rename to opensmtpd/src/data_structures/timeval.rs diff --git a/src/error.rs b/opensmtpd/src/error.rs similarity index 100% rename from src/error.rs rename to opensmtpd/src/error.rs diff --git a/src/filter.rs b/opensmtpd/src/filter.rs similarity index 100% rename from src/filter.rs rename to opensmtpd/src/filter.rs diff --git a/src/io.rs b/opensmtpd/src/io.rs similarity index 100% rename from src/io.rs rename to opensmtpd/src/io.rs diff --git a/src/lib.rs b/opensmtpd/src/lib.rs similarity index 94% rename from src/lib.rs rename to opensmtpd/src/lib.rs index 5d03f52..8079ef6 100644 --- a/src/lib.rs +++ b/opensmtpd/src/lib.rs @@ -22,14 +22,15 @@ //! disconnects. //! //! ``` -//! use opensmtpd::{register, run_filter, Filter, ReportEntry}; +//! use opensmtpd::{run_filter, Filter, ReportEntry}; +//! use opensmtpd_derive::register; //! //! struct MyCounter { //! nb: u64, //! } //! //! impl Filter for MyCounter { -//! register!(has_report_link_disconnect); +//! #[register] //! fn on_report_link_disconnect(&mut self, _entry: &ReportEntry) { //! self.nb + 1; //! } @@ -72,15 +73,6 @@ use std::thread; const BUFFER_SIZE: usize = 4096; -#[macro_export] -macro_rules! register { - ($name: ident) => { - fn $name(&self) -> bool { - return true; - } - }; -} - macro_rules! recv { ($rx: ident) => { match $rx.recv() { @@ -127,7 +119,12 @@ macro_rules! handshake_register { ($obj: ident, $func: ident, $subsystem: expr, $type: expr, $name: expr) => { if $obj.$func() { println!("register|{}|{}|{}", $type, $subsystem.to_string(), $name); - log::trace!("{} {} for {} registered", $type, $name, $subsystem.to_string()); + log::trace!( + "{} {} for {} registered", + $type, + $name, + $subsystem.to_string() + ); } }; } diff --git a/src/parsers/entry.rs b/opensmtpd/src/parsers/entry.rs similarity index 100% rename from src/parsers/entry.rs rename to opensmtpd/src/parsers/entry.rs diff --git a/src/parsers/handshake.rs b/opensmtpd/src/parsers/handshake.rs similarity index 100% rename from src/parsers/handshake.rs rename to opensmtpd/src/parsers/handshake.rs diff --git a/src/parsers/mod.rs b/opensmtpd/src/parsers/mod.rs similarity index 100% rename from src/parsers/mod.rs rename to opensmtpd/src/parsers/mod.rs diff --git a/src/parsers/parameters.rs b/opensmtpd/src/parsers/parameters.rs similarity index 100% rename from src/parsers/parameters.rs rename to opensmtpd/src/parsers/parameters.rs diff --git a/src/process.rs b/opensmtpd/src/process.rs similarity index 100% rename from src/process.rs rename to opensmtpd/src/process.rs