0.3.1: Allow creating events with raw IDs, pretty print IDs in hex

This commit is contained in:
tux3
2023-03-10 15:39:45 +01:00
parent f153bb8db9
commit 8582a3309d
2 changed files with 37 additions and 2 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ repository = "https://github.com/tux3/crowdstrike-cloudproto/"
categories = ["network-programming", "api-bindings"]
keywords = ["async", "api", "protocol"]
license = "MIT OR Apache-2.0"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
rust-version = "1.64"
readme = "README.md"
+36 -1
View File
@@ -30,12 +30,20 @@ impl Event {
}
}
pub fn new_raw(raw_event_id: u32, data: Vec<u8>) -> Self {
Self {
raw_event_id,
event_id: None,
data,
}
}
/// Best effort text representation of the event ID using know [`EventId`][EventId] values
pub fn ev_id_string(&self) -> String {
if let Some(id) = self.event_id {
id.to_string()
} else {
self.raw_event_id.to_string()
format!("{:#X}", self.raw_event_id)
}
}
@@ -102,3 +110,30 @@ pub enum EventId {
AgentOnline = 0x338000ac,
UNK_0x340000ee = 0x340000ee, // No search results in sensor
}
#[cfg(test)]
mod tests {
use super::*;
use bytes::Buf;
#[test]
fn test_known_id_string() {
let ev = Event::new(EventId::AgentOnline, vec![]);
assert_eq!(ev.ev_id_string(), "AgentOnline")
}
#[test]
fn test_unknown_id_string() {
let ev = Event::new_raw(0xAABBCCDD, vec![]);
assert_eq!(ev.ev_id_string(), "0xAABBCCDD")
}
#[test]
fn test_event_serde_rountrip() {
let ev = Event::new_raw(0xAABBCCDD, vec![]);
let mut buf = Vec::new();
ev.clone().into_write(&mut buf).unwrap();
let ev2 = Event::from_read(&mut buf.reader()).unwrap();
assert_eq!(ev, ev2);
}
}