Skip to main content

emissary_core/primitives/
mod.rs

1// Permission is hereby granted, free of charge, to any person obtaining a
2// copy of this software and associated documentation files (the "Software"),
3// to deal in the Software without restriction, including without limitation
4// the rights to use, copy, modify, merge, publish, distribute, sublicense,
5// and/or sell copies of the Software, and to permit persons to whom the
6// Software is furnished to do so, subject to the following conditions:
7//
8// The above copyright notice and this permission notice shall be included in
9// all copies or substantial portions of the Software.
10//
11// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
16// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19use core::{fmt, ops::Deref};
20
21pub use capabilities::Capabilities;
22pub use datagram_flags::DatagramFlags;
23pub use date::Date;
24pub use destination::{Destination, DestinationId};
25pub use lease_set::{Lease, LeaseSet2, LeaseSet2Header};
26pub use mapping::Mapping;
27pub use offline_signature::OfflineSignature;
28pub use router_address::{RouterAddress, TransportKind};
29pub use router_identity::{RouterId, RouterIdentity};
30pub use router_info::RouterInfo;
31pub use string::Str;
32
33#[cfg(test)]
34pub use router_info::RouterInfoBuilder;
35
36mod capabilities;
37mod datagram_flags;
38mod date;
39mod destination;
40mod lease_set;
41mod mapping;
42mod offline_signature;
43mod router_address;
44mod router_identity;
45mod router_info;
46mod string;
47
48/// Logging target for the module.
49const LOG_TARGET: &str = "emissary::primitives";
50
51/// Tunnel ID.
52#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
53pub struct TunnelId(u32);
54
55impl TunnelId {
56    #[cfg(test)]
57    pub fn random() -> TunnelId {
58        use rand::RngCore;
59
60        TunnelId::from(rand::thread_rng().next_u32())
61    }
62}
63
64impl From<u32> for TunnelId {
65    fn from(value: u32) -> Self {
66        TunnelId(value)
67    }
68}
69
70impl From<TunnelId> for u32 {
71    fn from(value: TunnelId) -> Self {
72        value.0
73    }
74}
75
76impl Deref for TunnelId {
77    type Target = u32;
78
79    fn deref(&self) -> &Self::Target {
80        &self.0
81    }
82}
83
84impl fmt::Display for TunnelId {
85    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86        write!(f, "{}", self.0)
87    }
88}
89
90/// Message Id.
91#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
92pub struct MessageId(u32);
93
94impl MessageId {
95    #[cfg(test)]
96    pub fn random() -> MessageId {
97        use rand::RngCore;
98
99        MessageId::from(rand::thread_rng().next_u32())
100    }
101}
102
103impl From<u32> for MessageId {
104    fn from(value: u32) -> Self {
105        MessageId(value)
106    }
107}
108
109impl From<MessageId> for u32 {
110    fn from(value: MessageId) -> Self {
111        value.0
112    }
113}
114
115impl fmt::Display for MessageId {
116    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117        write!(f, "{}", self.0)
118    }
119}
120
121impl Deref for MessageId {
122    type Target = u32;
123
124    fn deref(&self) -> &Self::Target {
125        &self.0
126    }
127}