rust_webrtc/protocol/src/lib.rs

72 lines
1.6 KiB
Rust
Raw Normal View History

use std::fmt::Display;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
2025-04-14 04:58:44 +03:00
#[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)]
2025-04-14 04:58:44 +03:00
pub enum SignalType {
Auth,
Offer,
Answer,
}
2025-04-14 04:58:44 +03:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Signal {
signal_type: SignalType,
data: String,
2025-04-14 04:58:44 +03:00
time: DateTime<Utc>,
}
2025-04-14 04:58:44 +03:00
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"),
}
}
}
2025-04-14 04:58:44 +03:00
impl Signal {
pub fn new(signal_type: &SignalType, data: &String) -> Signal {
2025-04-14 04:58:44 +03:00
let signal_type = *signal_type;
let data = data.to_owned();
Signal {
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()
}
2025-04-14 04:58:44 +03:00
pub fn get_time(&self) -> DateTime<Utc> {
self.time
}
}