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
crate::include_proto!("quilkin.filters.debug.v1alpha1");
use std::convert::TryFrom;
use crate::filters::prelude::*;
use serde::{Deserialize, Serialize};
use tracing::info;
use self::quilkin::filters::debug::v1alpha1 as proto;
#[derive(Debug)]
pub struct Debug {
config: Config,
}
impl Debug {
fn new(config: Option<Config>) -> Self {
Self {
config: config.unwrap_or_default(),
}
}
}
#[async_trait::async_trait]
impl Filter for Debug {
#[cfg_attr(feature = "instrument", tracing::instrument(skip(self, ctx)))]
async fn read(&self, ctx: &mut ReadContext) -> Result<(), FilterError> {
info!(id = ?self.config.id, source = ?&ctx.source, contents = ?String::from_utf8_lossy(&ctx.contents), "Read filter event");
Ok(())
}
#[cfg_attr(feature = "instrument", tracing::instrument(skip(self, ctx)))]
async fn write(&self, ctx: &mut WriteContext) -> Result<(), FilterError> {
info!(id = ?self.config.id, endpoint = ?ctx.endpoint.address, source = ?&ctx.source,
dest = ?&ctx.dest, contents = ?String::from_utf8_lossy(&ctx.contents), "Write filter event");
Ok(())
}
}
impl StaticFilter for Debug {
const NAME: &'static str = "quilkin.filters.debug.v1alpha1.Debug";
type Configuration = Config;
type BinaryConfiguration = proto::Debug;
fn try_from_config(config: Option<Self::Configuration>) -> Result<Self, CreationError> {
Ok(Debug::new(config))
}
}
#[derive(Serialize, Default, Deserialize, Debug, schemars::JsonSchema)]
pub struct Config {
pub id: Option<String>,
}
impl From<Config> for proto::Debug {
fn from(config: Config) -> Self {
Self { id: config.id }
}
}
impl TryFrom<proto::Debug> for Config {
type Error = ConvertProtoConfigError;
fn try_from(p: proto::Debug) -> Result<Self, Self::Error> {
Ok(Config { id: p.id })
}
}
#[cfg(test)]
mod tests {
use crate::test_utils::{assert_filter_read_no_change, assert_write_no_change};
use tracing_test::traced_test;
use super::*;
#[traced_test]
#[tokio::test]
async fn read() {
let df = Debug::new(None);
assert_filter_read_no_change(&df).await;
assert!(logs_contain("Read filter event"));
}
#[traced_test]
#[tokio::test]
async fn write() {
let df = Debug::new(None);
assert_write_no_change(&df).await;
assert!(logs_contain("Write filter event"));
assert!(logs_contain("quilkin::filters::debug")); }
#[test]
fn from_config_with_id() {
let config = serde_json::json!({ "id": "name", });
Debug::from_config(Some(serde_json::from_value(config).unwrap()));
}
#[test]
fn from_config_without_id() {
let config = serde_json::json!({});
Debug::from_config(Some(serde_json::from_value(config).unwrap()));
}
#[test]
fn from_config_should_error() {
serde_json::from_value::<Config>(serde_json::json!({ "id": {} })).unwrap_err();
}
}