Skip to content

Commit 60fcca0

Browse files
authored
Insert empty structured data too (#22)
1 parent 7abce78 commit 60fcca0

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/message.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,25 @@ impl StructuredData {
9090
}
9191
}
9292

93+
/// Fetch or insert a new sd_id entry into the StructuredData
94+
pub fn entry<SI>(&mut self, sd_id: SI) -> &mut BTreeMap<String, String>
95+
where
96+
SI: Into<SDIDType>,
97+
{
98+
self.elements
99+
.entry(sd_id.into())
100+
.or_insert_with(BTreeMap::new)
101+
}
102+
93103
/// Insert a new (sd_id, sd_param_id) -> sd_value mapping into the StructuredData
94104
pub fn insert_tuple<SI, SPI, SPV>(&mut self, sd_id: SI, sd_param_id: SPI, sd_param_value: SPV)
95105
where
96106
SI: Into<SDIDType>,
97107
SPI: Into<SDParamIDType>,
98108
SPV: Into<SDParamValueType>,
99109
{
100-
let sub_map = self
101-
.elements
102-
.entry(sd_id.into())
103-
.or_insert_with(BTreeMap::new);
104-
sub_map.insert(sd_param_id.into(), sd_param_value.into());
110+
self.entry(sd_id)
111+
.insert(sd_param_id.into(), sd_param_value.into());
105112
}
106113

107114
/// Lookup by SDID, SDParamID pair

src/parser.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ fn parse_sd(structured_data_raw: &str) -> ParseResult<(StructuredData, &str)> {
193193
let mut rest = structured_data_raw;
194194
while !rest.is_empty() {
195195
let (sd_id, params) = take_item!(parse_sde(rest), rest);
196+
let sub_map = sd.entry(sd_id.clone());
196197
for (sd_param_id, sd_param_value) in params {
197-
sd.insert_tuple(sd_id.clone(), sd_param_id, sd_param_value);
198+
sub_map.insert(sd_param_id, sd_param_value);
198199
}
199200
if rest.starts_with(' ') {
200201
break;
@@ -480,6 +481,29 @@ mod tests {
480481
assert_eq!(v, "29");
481482
}
482483

484+
#[test]
485+
fn test_sd_empty() {
486+
let msg = parse_message(
487+
"<78>1 2016-01-15T00:04:01Z host1 CROND 10391 - [meta@1234] some_message",
488+
)
489+
.expect("Should parse message with empty structured data");
490+
assert_eq!(msg.facility, SyslogFacility::LOG_CRON);
491+
assert_eq!(msg.severity, SyslogSeverity::SEV_INFO);
492+
assert_eq!(msg.hostname, Some(String::from("host1")));
493+
assert_eq!(msg.appname, Some(String::from("CROND")));
494+
assert_eq!(msg.procid, Some(message::ProcId::PID(10391)));
495+
assert_eq!(msg.msg, String::from("some_message"));
496+
assert_eq!(msg.timestamp, Some(1452816241));
497+
assert_eq!(msg.sd.len(), 1);
498+
assert_eq!(
499+
msg.sd
500+
.find_sdid("meta@1234")
501+
.expect("should contain meta")
502+
.len(),
503+
0
504+
);
505+
}
506+
483507
#[test]
484508
fn test_sd_features() {
485509
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");

0 commit comments

Comments
 (0)