Add the opensmtpd_derive crate
This commit is contained in:
parent
d1d51bbaa8
commit
4598fb33e4
29 changed files with 81 additions and 39 deletions
29
Cargo.toml
29
Cargo.toml
|
@ -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"
|
||||
]
|
||||
|
|
20
opensmtpd-derive/Cargo.toml
Normal file
20
opensmtpd-derive/Cargo.toml
Normal 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"] }
|
18
opensmtpd-derive/src/lib.rs
Normal file
18
opensmtpd-derive/src/lib.rs
Normal 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
25
opensmtpd/Cargo.toml
Normal 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"
|
|
@ -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!(
|
|
@ -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()
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
Reference in a new issue