rusmes-core 0.1.2

Mailet processing engine for RusMES — composable mail processing pipeline with matchers, mailets, DKIM/SPF/DMARC, spam filtering, and AI integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//! Priority queue system for mail delivery
//!
//! This module provides a multi-level priority queue system for mail delivery,
//! allowing mails to be processed based on their priority level.
//!
//! # Priority Levels
//!
//! - **High**: Urgent mail (priority 3)
//! - **Normal**: Default for most mail (priority 2)
//! - **Low**: Non-urgent mail (priority 1)
//! - **Bulk**: Newsletters, marketing (priority 0)
//!
//! # Features
//!
//! - Priority-based scheduling (highest priority first)
//! - Per-priority statistics tracking
//! - Priority inheritance for retries
//! - Configurable priority assignment rules:
//!   - Per sender email
//!   - Per recipient email
//!   - Per domain
//! - Automatic priority boosting after N failed attempts
//!
//! # Example: Basic Priority Queue
//!
//! ```
//! use rusmes_core::queue::priority::{Priority, PriorityQueue};
//! use rusmes_proto::MailId;
//!
//! let mut queue = PriorityQueue::<String>::with_default_config();
//!
//! // Enqueue items with different priorities
//! queue.enqueue(MailId::new(), "bulk mail".to_string(), Priority::Bulk);
//! queue.enqueue(MailId::new(), "urgent mail".to_string(), Priority::High);
//! queue.enqueue(MailId::new(), "normal mail".to_string(), Priority::Normal);
//!
//! // Dequeue returns highest priority first (High, Normal, Bulk)
//! let (mail_id, item, priority) = queue.dequeue().unwrap();
//! assert_eq!(item, "urgent mail");
//! assert_eq!(priority, Priority::High);
//! ```
//!
//! # Example: Priority Configuration
//!
//! ```
//! use rusmes_core::queue::priority::{Priority, PriorityConfig};
//! use rusmes_proto::{Mail, MimeMessage, MessageBody, HeaderMap};
//! use bytes::Bytes;
//!
//! let mut config = PriorityConfig::new();
//!
//! // VIP sender always gets high priority
//! config.add_sender_priority("vip@example.com", Priority::High);
//!
//! // Important domain gets high priority
//! config.add_domain_priority("important.com", Priority::High);
//!
//! // Bulk domain gets low priority
//! config.add_domain_priority("marketing.com", Priority::Bulk);
//!
//! // Enable priority boost after 3 failed attempts
//! config.boost_after_attempts = Some(3);
//! config.boost_amount = 1;
//!
//! // Calculate priority for a mail
//! let message = MimeMessage::new(HeaderMap::new(), MessageBody::Small(Bytes::from("test")));
//! let mail = Mail::new(
//!     Some("vip@example.com".parse().unwrap()),
//!     vec!["user@example.com".parse().unwrap()],
//!     message,
//!     None,
//!     None,
//! );
//!
//! let priority = config.calculate_priority(&mail, 0);
//! assert_eq!(priority, Priority::High);
//! ```
//!
//! # Example: Integration with MailQueue
//!
//! ```no_run
//! use rusmes_core::{MailQueue, PriorityConfig, Priority};
//! use rusmes_proto::{Mail, MimeMessage, MessageBody, HeaderMap};
//! use bytes::Bytes;
//!
//! #[tokio::main]
//! async fn main() {
//!     // Create priority configuration
//!     let mut priority_config = PriorityConfig::new();
//!     priority_config.add_domain_priority("urgent.com", Priority::High);
//!
//!     // Create queue with priority support
//!     let queue = MailQueue::new_with_priority_config(priority_config);
//!
//!     // Enqueue mail (priority calculated automatically)
//!     let message = MimeMessage::new(HeaderMap::new(), MessageBody::Small(Bytes::from("test")));
//!     let mail = Mail::new(
//!         Some("sender@example.com".parse().unwrap()),
//!         vec!["user@urgent.com".parse().unwrap()],
//!         message,
//!         None,
//!         None,
//!     );
//!     queue.enqueue(mail).await.unwrap();
//!
//!     // Get ready mails (sorted by priority)
//!     let ready_mails = queue.get_ready_for_retry(10);
//!
//!     // Get statistics by priority
//!     let stats = queue.stats_by_priority();
//!     for (priority, stat) in stats {
//!         println!("{}: {} total, {} ready", priority, stat.total, stat.ready);
//!     }
//! }
//! ```

use rusmes_proto::{Mail, MailId};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, RwLock};

/// Mail priority levels
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord, Default,
)]
pub enum Priority {
    /// Highest priority - urgent mail
    High = 3,
    /// Normal priority - default for most mail
    #[default]
    Normal = 2,
    /// Low priority - non-urgent mail
    Low = 1,
    /// Bulk mail - newsletters, marketing
    Bulk = 0,
}

impl Priority {
    /// Get all priority levels in order (highest first)
    pub fn all() -> &'static [Priority] {
        &[
            Priority::High,
            Priority::Normal,
            Priority::Low,
            Priority::Bulk,
        ]
    }

    /// Get priority as numeric value
    pub fn as_u8(self) -> u8 {
        self as u8
    }

    /// Create priority from numeric value
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            3 => Some(Priority::High),
            2 => Some(Priority::Normal),
            1 => Some(Priority::Low),
            0 => Some(Priority::Bulk),
            _ => None,
        }
    }
}

impl std::fmt::Display for Priority {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Priority::High => write!(f, "high"),
            Priority::Normal => write!(f, "normal"),
            Priority::Low => write!(f, "low"),
            Priority::Bulk => write!(f, "bulk"),
        }
    }
}

impl std::str::FromStr for Priority {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "high" => Ok(Priority::High),
            "normal" => Ok(Priority::Normal),
            "low" => Ok(Priority::Low),
            "bulk" => Ok(Priority::Bulk),
            _ => Err(format!("Invalid priority: {}", s)),
        }
    }
}

/// Configuration for priority assignment rules
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PriorityConfig {
    /// Default priority if no rules match
    pub default_priority: Priority,

    /// Sender-based priority rules (email -> priority)
    pub sender_priorities: HashMap<String, Priority>,

    /// Recipient-based priority rules (email -> priority)
    pub recipient_priorities: HashMap<String, Priority>,

    /// Domain-based priority rules (domain -> priority)
    pub domain_priorities: HashMap<String, Priority>,

    /// Enable priority inheritance for retries
    pub inherit_priority_on_retry: bool,

    /// Boost priority after N failed attempts
    pub boost_after_attempts: Option<u32>,

    /// Priority boost amount (e.g., Low -> Normal)
    pub boost_amount: u8,
}

impl Default for PriorityConfig {
    fn default() -> Self {
        Self {
            default_priority: Priority::Normal,
            sender_priorities: HashMap::new(),
            recipient_priorities: HashMap::new(),
            domain_priorities: HashMap::new(),
            inherit_priority_on_retry: true,
            boost_after_attempts: Some(3),
            boost_amount: 1,
        }
    }
}

impl PriorityConfig {
    /// Create a new priority configuration with defaults
    pub fn new() -> Self {
        Self::default()
    }

    /// Add sender priority rule
    pub fn add_sender_priority(&mut self, sender: impl Into<String>, priority: Priority) {
        self.sender_priorities.insert(sender.into(), priority);
    }

    /// Add recipient priority rule
    pub fn add_recipient_priority(&mut self, recipient: impl Into<String>, priority: Priority) {
        self.recipient_priorities.insert(recipient.into(), priority);
    }

    /// Add domain priority rule
    pub fn add_domain_priority(&mut self, domain: impl Into<String>, priority: Priority) {
        self.domain_priorities.insert(domain.into(), priority);
    }

    /// Calculate priority for a mail based on configured rules
    pub fn calculate_priority(&self, mail: &Mail, current_attempts: u32) -> Priority {
        // Check if priority is already set as an attribute
        if let Some(attr) = mail.get_attribute("priority") {
            if let Some(priority_str) = attr.as_str() {
                if let Ok(priority) = priority_str.parse::<Priority>() {
                    return self.apply_boost(priority, current_attempts);
                }
            }
        }

        // Check sender-based rules
        if let Some(sender) = mail.sender() {
            let sender_email = sender.as_string();
            if let Some(&priority) = self.sender_priorities.get(&sender_email) {
                return self.apply_boost(priority, current_attempts);
            }
        }

        // Check recipient-based rules (use highest priority if multiple recipients)
        let mut max_priority = None;
        for recipient in mail.recipients() {
            let recipient_email = recipient.as_string();
            if let Some(&priority) = self.recipient_priorities.get(&recipient_email) {
                max_priority = Some(max_priority.map_or(priority, |p: Priority| p.max(priority)));
            }

            // Check domain-based rules
            let domain = recipient.domain().as_str();
            if let Some(&priority) = self.domain_priorities.get(domain) {
                max_priority = Some(max_priority.map_or(priority, |p: Priority| p.max(priority)));
            }
        }

        if let Some(priority) = max_priority {
            return self.apply_boost(priority, current_attempts);
        }

        // Use default priority
        self.apply_boost(self.default_priority, current_attempts)
    }

    /// Apply priority boost based on retry attempts
    fn apply_boost(&self, priority: Priority, current_attempts: u32) -> Priority {
        if let Some(boost_after) = self.boost_after_attempts {
            if current_attempts >= boost_after {
                let current_value = priority.as_u8();
                let boosted_value = current_value.saturating_add(self.boost_amount);
                return Priority::from_u8(boosted_value.min(3)).unwrap_or(Priority::High);
            }
        }
        priority
    }
}

/// Statistics for a single priority level
#[derive(Debug, Clone, Default)]
pub struct PriorityStats {
    /// Total mails in this priority queue
    pub total: usize,
    /// Mails ready for delivery
    pub ready: usize,
    /// Mails waiting for retry
    pub delayed: usize,
    /// Total mails enqueued (lifetime)
    pub enqueued_total: u64,
    /// Total mails delivered (lifetime)
    pub delivered_total: u64,
}

/// Multi-level priority queue
pub struct PriorityQueue<T> {
    /// Separate queue for each priority level
    queues: HashMap<Priority, VecDeque<(MailId, T)>>,
    /// Priority configuration
    config: Arc<RwLock<PriorityConfig>>,
    /// Per-priority statistics
    stats: HashMap<Priority, Arc<RwLock<PriorityStats>>>,
}

impl<T> PriorityQueue<T> {
    /// Create a new priority queue
    pub fn new(config: PriorityConfig) -> Self {
        let mut queues = HashMap::new();
        let mut stats = HashMap::new();

        for &priority in Priority::all() {
            queues.insert(priority, VecDeque::new());
            stats.insert(priority, Arc::new(RwLock::new(PriorityStats::default())));
        }

        Self {
            queues,
            config: Arc::new(RwLock::new(config)),
            stats,
        }
    }

    /// Create with default configuration
    pub fn with_default_config() -> Self {
        Self::new(PriorityConfig::default())
    }

    /// Enqueue an item with a specific priority
    pub fn enqueue(&mut self, mail_id: MailId, item: T, priority: Priority) {
        if let Some(queue) = self.queues.get_mut(&priority) {
            queue.push_back((mail_id, item));
        }

        // Update statistics
        if let Some(stats) = self.stats.get(&priority) {
            if let Ok(mut stats) = stats.write() {
                stats.total += 1;
                stats.enqueued_total += 1;
            }
        }
    }

    /// Dequeue the next item based on priority (highest priority first)
    pub fn dequeue(&mut self) -> Option<(MailId, T, Priority)> {
        // Try queues in priority order (high to low)
        for &priority in Priority::all() {
            if let Some(queue) = self.queues.get_mut(&priority) {
                if let Some((mail_id, item)) = queue.pop_front() {
                    // Update statistics
                    if let Some(stats) = self.stats.get(&priority) {
                        if let Ok(mut stats) = stats.write() {
                            stats.total = stats.total.saturating_sub(1);
                        }
                    }
                    return Some((mail_id, item, priority));
                }
            }
        }
        None
    }

    /// Peek at the next item without removing it
    pub fn peek(&self) -> Option<(&MailId, &T, Priority)> {
        for &priority in Priority::all() {
            if let Some(queue) = self.queues.get(&priority) {
                if let Some((mail_id, item)) = queue.front() {
                    return Some((mail_id, item, priority));
                }
            }
        }
        None
    }

    /// Get the number of items in a specific priority queue
    pub fn len_for_priority(&self, priority: Priority) -> usize {
        self.queues.get(&priority).map_or(0, |q| q.len())
    }

    /// Get total number of items across all priorities
    pub fn len(&self) -> usize {
        self.queues.values().map(|q| q.len()).sum()
    }

    /// Check if the queue is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Remove a specific item by mail ID
    pub fn remove(&mut self, mail_id: &MailId) -> Option<(T, Priority)> {
        for &priority in Priority::all() {
            if let Some(queue) = self.queues.get_mut(&priority) {
                if let Some(pos) = queue.iter().position(|(id, _)| id == mail_id) {
                    if let Some((_, item)) = queue.remove(pos) {
                        // Update statistics
                        if let Some(stats) = self.stats.get(&priority) {
                            if let Ok(mut stats) = stats.write() {
                                stats.total = stats.total.saturating_sub(1);
                            }
                        }
                        return Some((item, priority));
                    }
                }
            }
        }
        None
    }

    /// Update priority configuration
    pub fn update_config(&self, config: PriorityConfig) {
        if let Ok(mut guard) = self.config.write() {
            *guard = config;
        }
    }

    /// Get current configuration
    pub fn get_config(&self) -> PriorityConfig {
        self.config.read().map(|g| g.clone()).unwrap_or_default()
    }

    /// Get statistics for a specific priority
    pub fn stats_for_priority(&self, priority: Priority) -> PriorityStats {
        self.stats
            .get(&priority)
            .and_then(|s| s.read().ok().map(|g| g.clone()))
            .unwrap_or_default()
    }

    /// Mark item as delivered (for statistics)
    pub fn mark_delivered(&self, priority: Priority) {
        if let Some(stats) = self.stats.get(&priority) {
            if let Ok(mut stats) = stats.write() {
                stats.delivered_total += 1;
            }
        }
    }

    /// Update ready/delayed counts for a priority
    pub fn update_ready_delayed_stats(&self, priority: Priority, ready: usize, delayed: usize) {
        if let Some(stats) = self.stats.get(&priority) {
            if let Ok(mut stats) = stats.write() {
                stats.ready = ready;
                stats.delayed = delayed;
            }
        }
    }

    /// Clear all queues
    pub fn clear(&mut self) {
        for queue in self.queues.values_mut() {
            queue.clear();
        }
        for stats in self.stats.values() {
            if let Ok(mut stats) = stats.write() {
                stats.total = 0;
                stats.ready = 0;
                stats.delayed = 0;
            }
        }
    }

    /// Get all items for a specific priority
    pub fn items_for_priority(&self, priority: Priority) -> Vec<&(MailId, T)> {
        self.queues
            .get(&priority)
            .map(|q| q.iter().collect())
            .unwrap_or_default()
    }
}

impl<T> Default for PriorityQueue<T> {
    fn default() -> Self {
        Self::with_default_config()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bytes::Bytes;
    use rusmes_proto::{HeaderMap, MessageBody, MimeMessage};

    #[test]
    fn test_priority_ordering() {
        assert!(Priority::High > Priority::Normal);
        assert!(Priority::Normal > Priority::Low);
        assert!(Priority::Low > Priority::Bulk);
    }

    #[test]
    fn test_priority_from_str() {
        assert_eq!("high".parse::<Priority>().unwrap(), Priority::High);
        assert_eq!("normal".parse::<Priority>().unwrap(), Priority::Normal);
        assert_eq!("low".parse::<Priority>().unwrap(), Priority::Low);
        assert_eq!("bulk".parse::<Priority>().unwrap(), Priority::Bulk);
        assert!("invalid".parse::<Priority>().is_err());
    }

    #[test]
    fn test_priority_queue_enqueue_dequeue() {
        let mut queue = PriorityQueue::<String>::with_default_config();

        let mail_id1 = MailId::new();
        let mail_id2 = MailId::new();
        let mail_id3 = MailId::new();

        queue.enqueue(mail_id1, "low priority".to_string(), Priority::Low);
        queue.enqueue(mail_id2, "high priority".to_string(), Priority::High);
        queue.enqueue(mail_id3, "normal priority".to_string(), Priority::Normal);

        // Should dequeue in priority order: High, Normal, Low
        let (_, item1, priority1) = queue.dequeue().unwrap();
        assert_eq!(item1, "high priority");
        assert_eq!(priority1, Priority::High);

        let (_, item2, priority2) = queue.dequeue().unwrap();
        assert_eq!(item2, "normal priority");
        assert_eq!(priority2, Priority::Normal);

        let (_, item3, priority3) = queue.dequeue().unwrap();
        assert_eq!(item3, "low priority");
        assert_eq!(priority3, Priority::Low);

        assert!(queue.is_empty());
    }

    #[test]
    fn test_priority_queue_remove() {
        let mut queue = PriorityQueue::<String>::with_default_config();

        let mail_id = MailId::new();
        queue.enqueue(mail_id, "test".to_string(), Priority::Normal);

        assert_eq!(queue.len(), 1);
        let (item, priority) = queue.remove(&mail_id).unwrap();
        assert_eq!(item, "test");
        assert_eq!(priority, Priority::Normal);
        assert!(queue.is_empty());
    }

    #[test]
    fn test_priority_config_sender_rule() {
        let mut config = PriorityConfig::new();
        config.add_sender_priority("vip@example.com", Priority::High);

        let message = MimeMessage::new(HeaderMap::new(), MessageBody::Small(Bytes::from("test")));

        let mail = Mail::new(
            Some("vip@example.com".parse().unwrap()),
            vec!["user@example.com".parse().unwrap()],
            message,
            None,
            None,
        );

        let priority = config.calculate_priority(&mail, 0);
        assert_eq!(priority, Priority::High);
    }

    #[test]
    fn test_priority_config_domain_rule() {
        let mut config = PriorityConfig::new();
        config.add_domain_priority("important.com", Priority::High);

        let message = MimeMessage::new(HeaderMap::new(), MessageBody::Small(Bytes::from("test")));

        let mail = Mail::new(
            Some("sender@example.com".parse().unwrap()),
            vec!["user@important.com".parse().unwrap()],
            message,
            None,
            None,
        );

        let priority = config.calculate_priority(&mail, 0);
        assert_eq!(priority, Priority::High);
    }

    #[test]
    fn test_priority_boost_on_retry() {
        let mut config = PriorityConfig::new();
        config.boost_after_attempts = Some(3);
        config.boost_amount = 1;

        let message = MimeMessage::new(HeaderMap::new(), MessageBody::Small(Bytes::from("test")));

        let mail = Mail::new(
            Some("sender@example.com".parse().unwrap()),
            vec!["user@example.com".parse().unwrap()],
            message,
            None,
            None,
        );

        // Before boost threshold
        let priority = config.calculate_priority(&mail, 2);
        assert_eq!(priority, Priority::Normal);

        // After boost threshold
        let priority = config.calculate_priority(&mail, 3);
        assert_eq!(priority, Priority::High);
    }

    #[test]
    fn test_priority_queue_stats() {
        let mut queue = PriorityQueue::<String>::with_default_config();

        let mail_id = MailId::new();
        queue.enqueue(mail_id, "test".to_string(), Priority::High);

        let stats = queue.stats_for_priority(Priority::High);
        assert_eq!(stats.total, 1);
        assert_eq!(stats.enqueued_total, 1);

        queue.mark_delivered(Priority::High);
        let stats = queue.stats_for_priority(Priority::High);
        assert_eq!(stats.delivered_total, 1);
    }
}