links_logs_vault/
pipeline.rs1use crate::{LogLevel, LogRecord, Vault, VaultError};
2
3pub trait Filter {
5 fn apply(&mut self, record: LogRecord) -> Option<LogRecord>;
7}
8
9pub struct MinimumLevel {
11 minimum: LogLevel,
12}
13
14impl MinimumLevel {
15 #[must_use]
17 pub const fn new(minimum: LogLevel) -> Self {
18 Self { minimum }
19 }
20}
21
22impl Filter for MinimumLevel {
23 fn apply(&mut self, record: LogRecord) -> Option<LogRecord> {
24 (record.level >= self.minimum).then_some(record)
25 }
26}
27
28pub struct AddField {
30 key: String,
31 value: String,
32}
33
34impl AddField {
35 #[must_use]
37 pub fn new(key: impl Into<String>, value: impl Into<String>) -> Self {
38 Self {
39 key: key.into(),
40 value: value.into(),
41 }
42 }
43}
44
45impl Filter for AddField {
46 fn apply(&mut self, mut record: LogRecord) -> Option<LogRecord> {
47 record.fields.insert(self.key.clone(), self.value.clone());
48 Some(record)
49 }
50}
51
52pub struct Pipeline {
54 vault: Vault,
55 filters: Vec<Box<dyn Filter>>,
56}
57
58impl Pipeline {
59 #[must_use]
61 pub fn new(vault: Vault) -> Self {
62 Self {
63 vault,
64 filters: Vec::new(),
65 }
66 }
67
68 pub fn add_filter(&mut self, filter: impl Filter + 'static) {
70 self.filters.push(Box::new(filter));
71 }
72
73 pub fn process(&mut self, mut record: LogRecord) -> Result<Option<u64>, VaultError> {
75 for filter in &mut self.filters {
76 let Some(filtered) = filter.apply(record) else {
77 return Ok(None);
78 };
79 record = filtered;
80 }
81 self.vault.append(record).map(Some)
82 }
83
84 #[must_use]
86 pub const fn vault(&self) -> &Vault {
87 &self.vault
88 }
89
90 #[must_use]
92 pub const fn vault_mut(&mut self) -> &mut Vault {
93 &mut self.vault
94 }
95
96 #[must_use]
98 pub fn into_vault(self) -> Vault {
99 self.vault
100 }
101}