noq_proto/
address_discovery.rs1use crate::VarInt;
5
6#[derive(PartialEq, Eq, Clone, Copy, Debug, Default)]
10pub(crate) struct Role {
11 pub(crate) send: bool,
13 pub(crate) receive: bool,
15}
16
17impl Role {
18 pub(crate) const fn send_only() -> Self {
19 Self {
20 send: true,
21 receive: false,
22 }
23 }
24
25 pub(crate) const fn receive_only() -> Self {
26 Self {
27 send: false,
28 receive: true,
29 }
30 }
31
32 pub(crate) const fn both() -> Self {
33 Self {
34 send: true,
35 receive: true,
36 }
37 }
38}
39
40impl TryFrom<VarInt> for Role {
41 type Error = crate::transport_parameters::Error;
42
43 fn try_from(value: VarInt) -> Result<Self, Self::Error> {
44 match value.0 {
45 0 => Ok(Self::send_only()),
46 1 => Ok(Self::receive_only()),
47 2 => Ok(Self::both()),
48 _ => Err(crate::transport_parameters::Error::IllegalValue),
49 }
50 }
51}
52
53impl Role {
54 pub(crate) fn is_disabled(&self) -> bool {
56 !self.send && !self.receive
57 }
58
59 pub(crate) fn should_report(&self, other: &Self) -> bool {
61 self.send && other.receive
62 }
63
64 pub(crate) fn as_transport_parameter(&self) -> Option<VarInt> {
66 match (self.send, self.receive) {
67 (false, false) => None,
68 (true, false) => Some(VarInt(0)),
69 (false, true) => Some(VarInt(1)),
70 (true, true) => Some(VarInt(2)),
71 }
72 }
73}