Skip to content

Commit

Permalink
node-data: change Event to only support JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Aug 8, 2024
1 parent 511aaa8 commit 9279da7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
12 changes: 2 additions & 10 deletions node-data/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ pub struct Event {
pub component: &'static str,
pub topic: &'static str,
pub entity: String,
pub data: EventData,
pub data: Option<serde_json::Value>,
}

pub trait EventSource {
const COMPONENT: &'static str;

fn topic(&self) -> &'static str;
fn entity(&self) -> String;
fn data(&self) -> EventData;
fn data(&self) -> Option<serde_json::Value>;
}

impl<ES: EventSource> From<ES> for Event {
Expand All @@ -36,11 +36,3 @@ impl<ES: EventSource> From<ES> for Event {
}
}
}

#[derive(Clone, Debug)]
pub enum EventData {
None,
Json(serde_json::Value),
Text(String),
Binary(Vec<u8>),
}
15 changes: 8 additions & 7 deletions node-data/src/events/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,27 @@ impl EventSource for BlockEvent<'_> {
Self::StateChange { .. } => "statechange",
}
}
fn data(&self) -> EventData {
match self {
fn data(&self) -> Option<serde_json::Value> {
let data = match self {
Self::Accepted(b) => {
let header = b.header();
let header = serde_json::to_value(header)
.expect("json to be serialized");
let txs: Vec<_> =
b.txs().iter().map(|t| hex::encode(t.id())).collect();
EventData::Json(serde_json::json!({
serde_json::json!({
"header": header,
"transactions": txs,
}))
})
}
Self::StateChange { state, height, .. } => {
EventData::Json(serde_json::json!({
serde_json::json!({
"state": state,
"atHeight": height,
}))
})
}
}
};
Some(data)
}
fn entity(&self) -> String {
let hash = match self {
Expand Down
4 changes: 2 additions & 2 deletions node-data/src/events/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ impl EventSource for TransactionEvent<'_> {
Self::Included(_) => "included",
}
}
fn data(&self) -> EventData {
EventData::None
fn data(&self) -> Option<serde_json::Value> {
None
}
fn entity(&self) -> String {
let hash = match self {
Expand Down

0 comments on commit 9279da7

Please sign in to comment.