Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
XAMPPRocky committed Feb 4, 2022
1 parent ddd08f8 commit 361b355
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/filters/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ impl Filter for Capture {
#[cfg_attr(feature = "instrument", tracing::instrument(skip(self, ctx)))]
fn read(&self, mut ctx: ReadContext) -> Option<ReadResponse> {
let capture = self.capture.capture(&mut ctx.contents, &self.metrics);
ctx.metadata.insert(self.is_present_key.clone(), Value::Bool(capture.is_some()));
ctx.metadata
.insert(self.is_present_key.clone(), Value::Bool(capture.is_some()));

if let Some(value) = capture {
ctx.metadata.insert(self.metadata_key.clone(), value);
Expand Down Expand Up @@ -255,7 +256,7 @@ mod tests {
fn regex_capture() {
let metrics = Metrics::new(&Registry::default()).unwrap();
let end = Regex {
regex: regex::bytes::Regex::new(".{3}$").unwrap(),
pattern: regex::bytes::Regex::new(".{3}$").unwrap(),
};
let mut contents = b"helloabc".to_vec();
let result = end.capture(&mut contents, &metrics).unwrap();
Expand Down
29 changes: 20 additions & 9 deletions src/filters/capture/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ impl Serialize for Config {
impl<'de> serde::Deserialize<'de> for Config {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(field_identifier, rename_all = "lowercase")]
enum Field {
#[serde(rename="metadataKey")]
#[serde(rename = "metadataKey")]
MetadataKey,
Prefix,
Suffix,
Expand All @@ -103,37 +103,41 @@ impl<'de> serde::Deserialize<'de> for Config {
{
let mut metadata_key = None;
let mut strategy = None;
let strategy_exists_err = || Err(serde::de::Error::custom("Multiple strategies found, only one capture strategy is permitted"));
let strategy_exists_err = || {
Err(serde::de::Error::custom(
"Multiple strategies found, only one capture strategy is permitted",
))
};

while let Some(key) = map.next_key()? {
match key {
Field::MetadataKey => {
if metadata_key.is_some() {
return Err(serde::de::Error::duplicate_field("metadataKey"))
return Err(serde::de::Error::duplicate_field("metadataKey"));
}

metadata_key = Some(map.next_value()?);
}

Field::Prefix => {
if strategy.is_some() {
return (strategy_exists_err)()
return (strategy_exists_err)();
}

strategy = Some(Strategy::Prefix(map.next_value()?));
}

Field::Suffix => {
if strategy.is_some() {
return (strategy_exists_err)()
return (strategy_exists_err)();
}

strategy = Some(Strategy::Suffix(map.next_value()?));
}

Field::Regex => {
if strategy.is_some() {
return (strategy_exists_err)()
return (strategy_exists_err)();
}

strategy = Some(Strategy::Regex(map.next_value()?));
Expand All @@ -142,9 +146,16 @@ impl<'de> serde::Deserialize<'de> for Config {
}

let metadata_key = metadata_key.unwrap_or_else(|| CAPTURED_BYTES.into());
let strategy = strategy.ok_or_else(|| serde::de::Error::custom("Capture strategy of `regex`, `suffix`, or `prefix` is required"))?;
let strategy = strategy.ok_or_else(|| {
serde::de::Error::custom(
"Capture strategy of `regex`, `suffix`, or `prefix` is required",
)
})?;

Ok(Config { metadata_key, strategy })
Ok(Config {
metadata_key,
strategy,
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/filters/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
/// The default key under which the [`super::capture`] filter puts the
/// byte slices it extracts from each packet.
/// - **Type** `Vec<u8>`
pub const CAPTURED_BYTES: &str = "quilkin.dev/captured";
pub const CAPTURED_BYTES: &str = "quilkin.dev/capture";
11 changes: 6 additions & 5 deletions tests/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,27 @@ async fn matches() {
let echo = t.run_echo_server().await;

let capture_yaml = "
size: 3
remove: true
suffix:
size: 3
remove: true
";

let matches_yaml = "
on_read:
metadataKey: quilkin.dev/capture
fallthrough:
id: quilkin.extensions.filters.concatenate_bytes.v1alpha1.ConcatenateBytes
filter: quilkin.extensions.filters.concatenate_bytes.v1alpha1.ConcatenateBytes
config:
on_read: APPEND
bytes: ZGVm
branches:
- value: abc
id: quilkin.extensions.filters.concatenate_bytes.v1alpha1.ConcatenateBytes
filter: quilkin.extensions.filters.concatenate_bytes.v1alpha1.ConcatenateBytes
config:
on_read: APPEND
bytes: eHl6 # xyz
- value: xyz
id: quilkin.extensions.filters.concatenate_bytes.v1alpha1.ConcatenateBytes
filter: quilkin.extensions.filters.concatenate_bytes.v1alpha1.ConcatenateBytes
config:
on_read: APPEND
bytes: YWJj # abc
Expand Down

0 comments on commit 361b355

Please sign in to comment.