rspamd_client/lib.rs
1//!# Rspamd Client for Rust
2//!
3//! This crate provides an HTTP client for interacting with the Rspamd service in Rust.
4//! It supports both synchronous and asynchronous operations using the `attohttpc` and
5//! `reqwest` libraries, respectively.
6//!
7//! ## Features
8//!
9//! - **Sync**: Synchronous client using `attohttpc`.
10//! - **Async**: Asynchronous client using `reqwest`.
11//! - Persistent clients with connection pooling (async backend).
12//! - Easily configurable with support for proxy, encryption, TLS and ZSTD compression.
13//! - Supports scanning emails for spam scores and other metrics.
14
15// Ensure async and sync features are mutually exclusive
16#[cfg(all(feature = "async", feature = "sync"))]
17compile_error!("Features 'async' and 'sync' are mutually exclusive. Please enable only one.");
18
19#[cfg(not(any(feature = "async", feature = "sync")))]
20compile_error!("Either 'async' or 'sync' feature must be enabled.");
21
22pub mod config;
23pub mod error;
24pub mod protocol;
25
26pub mod backend;
27
28pub use config::{Config, EnvelopeData, ProxyConfig, TlsSettings};
29pub use protocol::{RspamdLearnReply, RspamdScanReply};
30
31/// ### Synchronous Client
32///
33/// This example demonstrates how to scan emails using the persistent
34/// synchronous client.
35///
36/// ```rust,no_run
37/// use rspamd_client::{config, RspamdSyncClient};
38///
39/// let config = config::Config::builder()
40/// .base_url("http://localhost:11333".to_string())
41/// .build();
42/// let client = RspamdSyncClient::new(config).expect("cannot create client");
43/// let email = "From: user@example.com\nTo: recipient@example.com\nSubject: Test\n\nThis is a test email.";
44///
45/// match client.scan(email, Default::default()) {
46/// Ok(response) => println!("Scan result: {:?}", response),
47/// Err(e) => eprintln!("Error scanning email: {}", e),
48/// }
49/// ```
50#[cfg(feature = "sync")]
51pub use backend::sync_client::RspamdSyncClient;
52
53#[cfg(feature = "sync")]
54#[allow(deprecated)]
55pub use backend::sync_client::{scan_sync, sync_client, SyncClient};
56
57/// ### Asynchronous Client
58///
59/// This example demonstrates how to scan emails using the persistent
60/// asynchronous client. The underlying connection is pooled and reused
61/// across calls.
62///
63/// ```rust,no_run
64/// use rspamd_client::{config, RspamdAsyncClient};
65/// # use tokio;
66///
67/// # #[tokio::main]
68/// # async fn main() {
69/// let config = config::Config::builder()
70/// .base_url("http://localhost:11333".to_string())
71/// .build();
72/// let client = RspamdAsyncClient::new(config).expect("cannot create client");
73/// let email = "From: user@example.com\nTo: recipient@example.com\nSubject: Test\n\nThis is a test email.";
74///
75/// match client.scan(email, Default::default()).await {
76/// Ok(response) => println!("Scan result: {:?}", response),
77/// Err(e) => eprintln!("Error scanning email: {}", e),
78/// }
79/// # }
80/// ```
81#[cfg(feature = "async")]
82pub use backend::async_client::RspamdAsyncClient;
83
84#[cfg(feature = "async")]
85#[allow(deprecated)]
86pub use backend::async_client::{async_client, scan_async, AsyncClient};