aws_lambda_log_proxy/processor/
builder.rs

1use crate::{SimpleProcessor, SinkHandle};
2
3pub struct SimpleProcessorBuilder<T, S> {
4  /// See [`Self::transformer`].
5  pub transformer: T,
6  /// See [`Self::sink`].
7  pub sink: S,
8}
9
10impl SimpleProcessorBuilder<fn(String) -> Option<String>, ()> {
11  /// Create a new [`SimpleProcessorBuilder`] with the default transformer and no sink.
12  pub fn new() -> Self {
13    Self {
14      transformer: Some,
15      sink: (),
16    }
17  }
18}
19
20impl Default for SimpleProcessorBuilder<fn(String) -> Option<String>, ()> {
21  fn default() -> Self {
22    Self::new()
23  }
24}
25
26impl<T, S> SimpleProcessorBuilder<T, S> {
27  /// Set the log line transformer.
28  /// If the transformer returns [`None`], the line will be ignored.
29  /// The default transformer will just return `Some(line)`.
30  pub fn transformer<N: FnMut(String) -> Option<String> + Send + 'static>(
31    self,
32    transformer: N,
33  ) -> SimpleProcessorBuilder<N, S> {
34    SimpleProcessorBuilder {
35      transformer,
36      sink: self.sink,
37    }
38  }
39
40  /// Set the sink to write to.
41  pub fn sink(self, sink: SinkHandle) -> SimpleProcessorBuilder<T, SinkHandle> {
42    SimpleProcessorBuilder {
43      transformer: self.transformer,
44      sink,
45    }
46  }
47}
48
49impl<T: FnMut(String) -> Option<String> + Send + 'static> SimpleProcessorBuilder<T, SinkHandle> {
50  /// Create a new [`SimpleProcessor`] with the given `sink` and [`Self::transformer`].
51  pub fn build(self) -> SimpleProcessor<T> {
52    SimpleProcessor::new(self.transformer, self.sink)
53  }
54}
55
56#[cfg(test)]
57mod tests {
58  use super::*;
59
60  macro_rules! assert_unit {
61    ($unit:expr) => {
62      let _: () = $unit;
63    };
64  }
65
66  #[test]
67  fn default_transformer() {
68    let processor = SimpleProcessorBuilder::default();
69    assert_unit!(processor.sink);
70
71    let text = "test".to_string();
72    let transformed = (processor.transformer)(text.clone());
73    // default transformer should return the input line as is
74    assert_eq!(transformed, Some(text));
75  }
76}