Skip to content

Commit

Permalink
chore: Move swarm notification types into a module
Browse files Browse the repository at this point in the history
  • Loading branch information
bgins committed Nov 6, 2023
1 parent 717d9d4 commit 1e39d39
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 42 deletions.
46 changes: 4 additions & 42 deletions homestar-runtime/src/event_handler/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ use chrono::prelude::Utc;
use homestar_core::ipld::DagJson;
use libipld::{serde::from_ipld, Ipld};
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, fmt, str::FromStr};
use std::{collections::BTreeMap, str::FromStr};
use tracing::warn;

pub(crate) mod swarm;
pub(crate) use swarm::SwarmNotification;

const TYPE_KEY: &str = "type";
const DATA_KEY: &str = "data";
const TIMESTAMP_KEY: &str = "timestamp";
Expand Down Expand Up @@ -123,47 +126,6 @@ impl TryFrom<Ipld> for EventNotificationType {
}
}

// Types of swarm notification sent to clients
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(crate) enum SwarmNotification {
ConnnectionEstablished,
ConnnectionClosed,
ListeningOn,
OutgoingConnectionError,
IncomingConnectionError,
}

impl fmt::Display for SwarmNotification {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
SwarmNotification::ConnnectionEstablished => write!(f, "connectionEstablished"),
SwarmNotification::ConnnectionClosed => write!(f, "connectionClosed"),
SwarmNotification::ListeningOn => write!(f, "listeningOn"),
SwarmNotification::OutgoingConnectionError => {
write!(f, "outgoingConnectionError")
}
SwarmNotification::IncomingConnectionError => {
write!(f, "incomingConnectionError")
}
}
}
}

impl FromStr for SwarmNotification {
type Err = anyhow::Error;

fn from_str(ty: &str) -> Result<Self, Self::Err> {
match ty {
"connectionEstablished" => Ok(Self::ConnnectionEstablished),
"connectionClosed" => Ok(Self::ConnnectionClosed),
"listeningOn" => Ok(Self::ListeningOn),
"outgoingConnectionError" => Ok(Self::OutgoingConnectionError),
"incomingConnectionError" => Ok(Self::IncomingConnectionError),
_ => Err(anyhow!("Missing swarm notification type: {}", ty)),
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
44 changes: 44 additions & 0 deletions homestar-runtime/src/event_handler/notification/swarm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use anyhow::anyhow;
use serde::{Deserialize, Serialize};
use std::{fmt, str::FromStr};

// Swarm notification types sent to clients
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(crate) enum SwarmNotification {
ConnnectionEstablished,
ConnnectionClosed,
ListeningOn,
OutgoingConnectionError,
IncomingConnectionError,
}

impl fmt::Display for SwarmNotification {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
SwarmNotification::ConnnectionEstablished => write!(f, "connectionEstablished"),
SwarmNotification::ConnnectionClosed => write!(f, "connectionClosed"),
SwarmNotification::ListeningOn => write!(f, "listeningOn"),
SwarmNotification::OutgoingConnectionError => {
write!(f, "outgoingConnectionError")
}
SwarmNotification::IncomingConnectionError => {
write!(f, "incomingConnectionError")
}
}
}
}

impl FromStr for SwarmNotification {
type Err = anyhow::Error;

fn from_str(ty: &str) -> Result<Self, Self::Err> {
match ty {
"connectionEstablished" => Ok(Self::ConnnectionEstablished),
"connectionClosed" => Ok(Self::ConnnectionClosed),
"listeningOn" => Ok(Self::ListeningOn),
"outgoingConnectionError" => Ok(Self::OutgoingConnectionError),
"incomingConnectionError" => Ok(Self::IncomingConnectionError),
_ => Err(anyhow!("Missing swarm notification type: {}", ty)),
}
}
}

0 comments on commit 1e39d39

Please sign in to comment.