From 3acb724ba1aafeaf37e24dada7c769bb4066444a Mon Sep 17 00:00:00 2001 From: hozan23 Date: Mon, 27 May 2024 00:48:15 +0200 Subject: net: add serde feature for serializing & deserializing Endpoints --- core/src/event.rs | 8 ++++---- net/Cargo.toml | 4 ++++ net/src/endpoint.rs | 5 +++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/core/src/event.rs b/core/src/event.rs index a32677b..f7e3430 100644 --- a/core/src/event.rs +++ b/core/src/event.rs @@ -7,7 +7,7 @@ use std::{ use async_channel::{Receiver, Sender}; use chrono::{DateTime, Utc}; -use log::{error, trace}; +use log::{error, debug}; use crate::{async_runtime::lock::Mutex, util::random_16, Result}; @@ -104,7 +104,7 @@ where let mut topics = self.listeners.lock().await; if !topics.contains_key(topic) { - trace!( + debug!( "Failed to emit an event to a non-existent topic {:?}", topic ); @@ -115,7 +115,7 @@ where let event_id = E::id().to_string(); if !event_ids.contains_key(&event_id) { - trace!( + debug!( "Failed to emit an event to a non-existent event id: {:?}", event_id ); @@ -127,7 +127,7 @@ where let listeners = event_ids.get_mut(&event_id).unwrap(); for (listener_id, listener) in listeners.iter() { if let Err(err) = listener.send(event.clone()).await { - trace!("Failed to emit event for topic {:?}: {}", topic, err); + debug!("Failed to emit event for topic {:?}: {}", topic, err); failed_listeners.push(*listener_id); } } diff --git a/net/Cargo.toml b/net/Cargo.toml index 3206a0f..01a54a7 100644 --- a/net/Cargo.toml +++ b/net/Cargo.toml @@ -32,6 +32,7 @@ tokio = [ # TODO: use tls feature "tokio-rustls", ] +serde = ["dep:serde"] [dependencies] @@ -58,5 +59,8 @@ futures-rustls = { version = "0.25.1", optional = true } tokio-rustls = { version = "0.26.0", optional = true } tokio = { version = "1.37.0", features = ["io-util"], optional = true } +# serde +serde = { version = "1.0.197", features = ["derive"], optional = true } + [dev-dependencies] smol = "2.0.0" diff --git a/net/src/endpoint.rs b/net/src/endpoint.rs index 5aebdf9..9fb949b 100644 --- a/net/src/endpoint.rs +++ b/net/src/endpoint.rs @@ -9,6 +9,9 @@ use std::os::unix::net::SocketAddr as UnixSocketAddr; use bincode::{Decode, Encode}; use url::Url; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + use crate::{Error, Result}; /// Port defined as a u16. @@ -31,6 +34,7 @@ pub type Port = u16; /// ``` /// #[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum Endpoint { Udp(Addr, Port), Tcp(Addr, Port), @@ -200,6 +204,7 @@ impl Endpoint { /// Addr defines a type for an address, either IP or domain. #[derive(Debug, Clone, PartialEq, Eq, Hash, Encode, Decode)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum Addr { Ip(IpAddr), Domain(String), -- cgit v1.2.3