wasm_peers_protocol/
lib.rs

1/*!
2Helper crate that declares common types and structures shared between [wasm-peers](https://docs.rs/wasm-peers/latest/wasm_peers/)
3and [wasm-peers-signaling-server](https://docs.rs/wasm-peers-signaling-server/latest/wasm_peers_signaling_server/).
4*/
5
6#![deny(missing_docs)]
7
8use serde::{Deserialize, Serialize};
9use std::fmt::{Display, Formatter};
10use std::ops::Deref;
11use std::str::FromStr;
12
13pub mod many_to_many;
14pub mod one_to_many;
15pub mod one_to_one;
16
17/// Unique identifier of signaling session that each user provides
18/// when communicating with the signaling server.
19#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Hash)]
20pub struct SessionId(String);
21
22impl SessionId {
23    /// Wrap String into a SessionId struct
24    pub fn new(inner: String) -> Self {
25        SessionId(inner)
26    }
27
28    /// Return reference to the underling string
29    pub fn as_str(&self) -> &str {
30        &self.0
31    }
32    /// Acquire the underlying type
33    pub fn into_inner(self) -> String {
34        self.0
35    }
36}
37
38impl FromStr for SessionId {
39    type Err = Box<dyn std::error::Error>;
40
41    fn from_str(s: &str) -> Result<Self, Self::Err> {
42        Ok(SessionId(s.to_string()))
43    }
44}
45
46impl Display for SessionId {
47    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
48        write!(f, "{}", self.0)
49    }
50}
51
52/// Unique identifier of each peer connected to signaling server
53/// useful when communicating in one-to-many and many-to-many topologies.
54#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize, Hash)]
55pub struct UserId(usize);
56
57impl UserId {
58    /// Wrap usize into a UserId struct
59    pub fn new(inner: usize) -> Self {
60        UserId(inner)
61    }
62
63    /// Acquire the underlying type
64    pub fn into_inner(self) -> usize {
65        self.0
66    }
67}
68
69impl From<usize> for UserId {
70    fn from(val: usize) -> Self {
71        UserId(val)
72    }
73}
74
75impl Display for UserId {
76    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
77        write!(f, "{}", self.0)
78    }
79}
80
81impl Deref for UserId {
82    type Target = usize;
83
84    fn deref(&self) -> &Self::Target {
85        &self.0
86    }
87}
88
89/// Unique identifier specifying which peer is host and will be creating an offer,
90/// and which will await it.
91pub type IsHost = bool;