refactor: ♻️ error, send and receive signals

This commit is contained in:
Ahmet Kaan Gümüş 2025-04-14 16:13:42 +03:00
parent 85fb4fb2af
commit f781afe995
3 changed files with 89 additions and 58 deletions

View file

@ -1,7 +1,30 @@
use std::fmt::Display;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum Error {
SignalType(SignalType),
}
impl std::error::Error for Error {
fn cause(&self) -> Option<&dyn std::error::Error> {
self.source()
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::SignalType(signal_type) => {
write!(f, "Not Expected Signal Type: {}", signal_type)
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SignalType {
Auth,
Offer,
@ -10,25 +33,38 @@ pub enum SignalType {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Signal {
pub username: String,
pub signal_type: SignalType,
pub data: String,
signal_type: SignalType,
data: String,
time: DateTime<Utc>,
}
impl Display for SignalType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SignalType::Auth => write!(f, "Auth"),
SignalType::Offer => write!(f, "Offer"),
SignalType::Answer => write!(f, "Answer"),
}
}
}
impl Signal {
pub fn new(username: &String, signal_type: &SignalType, data: &String) -> Signal {
let username = username.to_owned();
pub fn new(signal_type: &SignalType, data: &String) -> Signal {
let signal_type = *signal_type;
let data = data.to_owned();
Signal {
username,
signal_type,
data,
time: DateTime::default(),
}
}
pub fn get_signal_type(&self) -> SignalType {
self.signal_type
}
pub fn get_data(&self) -> String {
self.data.to_owned()
}
pub fn get_time(&self) -> DateTime<Utc> {
self.time
}