Add the opensmtpd_derive crate

This commit is contained in:
Rodolphe Bréard 2020-12-20 17:24:08 +01:00
parent d1d51bbaa8
commit 4598fb33e4
29 changed files with 81 additions and 39 deletions

View file

@ -1,24 +1,5 @@
[package]
name = "opensmtpd"
version = "0.3.0"
authors = ["Rodolphe Bréard <rodolphe@what.tf>"]
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"
]

View file

@ -0,0 +1,20 @@
[package]
name = "opensmtpd_derive"
version = "0.3.0"
authors = ["Rodolphe Bréard <rodolphe@what.tf>"]
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"] }

View file

@ -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()
}

25
opensmtpd/Cargo.toml Normal file
View file

@ -0,0 +1,25 @@
[package]
name = "opensmtpd"
version = "0.3.0"
authors = ["Rodolphe Bréard <rodolphe@what.tf>"]
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"

View file

@ -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!(

View file

@ -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()
);
}
};
}