Skip to content

Commit

Permalink
Insert empty structured data too (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsc authored Jul 15, 2022
1 parent 7abce78 commit 60fcca0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,25 @@ impl StructuredData {
}
}

/// Fetch or insert a new sd_id entry into the StructuredData
pub fn entry<SI>(&mut self, sd_id: SI) -> &mut BTreeMap<String, String>
where
SI: Into<SDIDType>,
{
self.elements
.entry(sd_id.into())
.or_insert_with(BTreeMap::new)
}

/// Insert a new (sd_id, sd_param_id) -> sd_value mapping into the StructuredData
pub fn insert_tuple<SI, SPI, SPV>(&mut self, sd_id: SI, sd_param_id: SPI, sd_param_value: SPV)
where
SI: Into<SDIDType>,
SPI: Into<SDParamIDType>,
SPV: Into<SDParamValueType>,
{
let sub_map = self
.elements
.entry(sd_id.into())
.or_insert_with(BTreeMap::new);
sub_map.insert(sd_param_id.into(), sd_param_value.into());
self.entry(sd_id)
.insert(sd_param_id.into(), sd_param_value.into());
}

/// Lookup by SDID, SDParamID pair
Expand Down
26 changes: 25 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ fn parse_sd(structured_data_raw: &str) -> ParseResult<(StructuredData, &str)> {
let mut rest = structured_data_raw;
while !rest.is_empty() {
let (sd_id, params) = take_item!(parse_sde(rest), rest);
let sub_map = sd.entry(sd_id.clone());
for (sd_param_id, sd_param_value) in params {
sd.insert_tuple(sd_id.clone(), sd_param_id, sd_param_value);
sub_map.insert(sd_param_id, sd_param_value);
}
if rest.starts_with(' ') {
break;
Expand Down Expand Up @@ -480,6 +481,29 @@ mod tests {
assert_eq!(v, "29");
}

#[test]
fn test_sd_empty() {
let msg = parse_message(
"<78>1 2016-01-15T00:04:01Z host1 CROND 10391 - [meta@1234] some_message",
)
.expect("Should parse message with empty structured data");
assert_eq!(msg.facility, SyslogFacility::LOG_CRON);
assert_eq!(msg.severity, SyslogSeverity::SEV_INFO);
assert_eq!(msg.hostname, Some(String::from("host1")));
assert_eq!(msg.appname, Some(String::from("CROND")));
assert_eq!(msg.procid, Some(message::ProcId::PID(10391)));
assert_eq!(msg.msg, String::from("some_message"));
assert_eq!(msg.timestamp, Some(1452816241));
assert_eq!(msg.sd.len(), 1);
assert_eq!(
msg.sd
.find_sdid("meta@1234")
.expect("should contain meta")
.len(),
0
);
}

#[test]
fn test_sd_features() {
let msg = parse_message("<78>1 2016-01-15T00:04:01Z host1 CROND 10391 - [meta sequenceId=\"29\" sequenceBlah=\"foo\"][my key=\"value\"][meta bar=\"baz=\"] some_message").expect("Should parse complex message");
Expand Down

0 comments on commit 60fcca0

Please sign in to comment.