rust_webrtc/protocol/src/lib.rs

107 lines
2.6 KiB
Rust
Raw Normal View History

use std::{fmt::Display, str::FromStr};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserAndSignal {
pub user: User,
pub signal: Signal,
}
impl UserAndSignal {
pub async fn new(user: User, signal: Signal) -> Self {
UserAndSignal { user, signal }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct User {
pub username: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Error {
UnexpectedSignalType(SignalType),
InvalidSignalType(String),
}
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::UnexpectedSignalType(signal_type) => {
write!(f, "Unexpected Signal Type: {}", signal_type)
}
Error::InvalidSignalType(invalid_signal_type) => {
write!(f, "Invalid Signal Type: {}", invalid_signal_type)
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
2025-04-14 04:58:44 +03:00
pub enum SignalType {
Auth,
Offer,
Answer,
ICECandidate,
}
impl FromStr for SignalType {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Auth" => Ok(SignalType::Auth),
"Offer" => Ok(SignalType::Offer),
"Answer" => Ok(SignalType::Answer),
_ => Err(Error::InvalidSignalType(s.to_owned())),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2025-04-14 04:58:44 +03:00
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"),
SignalType::ICECandidate => write!(f, "ICE Candidate"),
}
}
}
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
}
}