1use crate::{connection, endpoint};
5#[cfg(feature = "alloc")]
6use alloc::vec::Vec;
7use core::{fmt, ops::RangeInclusive, time::Duration};
8
9mod generated;
10pub mod metrics;
11pub use generated::*;
12
13#[cfg(any(test, feature = "testing"))]
14#[doc(hidden)]
15pub mod snapshot;
16
17pub trait Event: core::fmt::Debug {
19 const NAME: &'static str;
20}
21
22pub trait IntoEvent<Target> {
23 fn into_event(self) -> Target;
24}
25
26macro_rules! ident_into_event {
27 ($($name:ty),* $(,)?) => {
28 $(
29 impl IntoEvent<$name> for $name {
30 #[inline]
31 fn into_event(self) -> Self {
32 self
33 }
34 }
35 )*
36 };
37}
38
39macro_rules! borrowed_into_event {
40 ($($name:ty),* $(,)?) => {
41 $(
42 impl<'a> IntoEvent<&'a $name> for &'a $name {
43 #[inline]
44 fn into_event(self) -> Self {
45 self
46 }
47 }
48 )*
49 };
50}
51
52ident_into_event!(
53 u8,
54 i8,
55 u16,
56 i16,
57 u32,
58 i32,
59 u64,
60 i64,
61 usize,
62 isize,
63 f32,
64 Duration,
65 bool,
66 connection::Error,
67 endpoint::Location,
68);
69borrowed_into_event!([u8; 4], [u8; 16], [u8], [u32], [&'a [u8]]);
70
71impl<T: IntoEvent<U>, U> IntoEvent<Option<U>> for Option<T> {
72 #[inline]
73 fn into_event(self) -> Option<U> {
74 self.map(IntoEvent::into_event)
75 }
76}
77
78impl<'a> IntoEvent<&'a str> for &'a str {
79 #[inline]
80 fn into_event(self) -> Self {
81 self
82 }
83}
84
85impl<'a> IntoEvent<&'a (dyn core::any::Any + Send + 'static)>
86 for &'a (dyn core::any::Any + Send + 'static)
87{
88 #[inline]
89 fn into_event(self) -> Self {
90 self
91 }
92}
93
94impl<T> IntoEvent<RangeInclusive<T>> for RangeInclusive<T> {
95 #[inline]
96 fn into_event(self) -> RangeInclusive<T> {
97 self
98 }
99}
100
101#[derive(Clone, Copy)]
102pub struct Timestamp(crate::time::Timestamp);
103
104impl fmt::Debug for Timestamp {
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 self.0.fmt(f)
107 }
108}
109
110impl fmt::Display for Timestamp {
111 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112 self.0.fmt(f)
113 }
114}
115
116impl Timestamp {
117 pub fn duration_since_start(&self) -> Duration {
139 unsafe { self.0.as_duration() }
142 }
143
144 #[inline]
147 pub fn saturating_duration_since(self, earlier: Self) -> Duration {
148 self.0.saturating_duration_since(earlier.0)
149 }
150}
151
152impl IntoEvent<Timestamp> for crate::time::Timestamp {
153 #[inline]
154 fn into_event(self) -> Timestamp {
155 Timestamp(self)
156 }
157}
158
159impl IntoEvent<Timestamp> for Timestamp {
160 #[inline]
161 fn into_event(self) -> Timestamp {
162 self
163 }
164}
165
166#[derive(Clone)]
167pub struct TlsSession<'a> {
168 session: &'a dyn crate::crypto::tls::TlsSession,
169}
170
171impl<'a> TlsSession<'a> {
172 #[doc(hidden)]
173 pub fn new(session: &'a dyn crate::crypto::tls::TlsSession) -> TlsSession<'a> {
174 TlsSession { session }
175 }
176
177 pub fn tls_exporter(
178 &self,
179 label: &[u8],
180 context: &[u8],
181 output: &mut [u8],
182 ) -> Result<(), crate::crypto::tls::TlsExportError> {
183 self.session.tls_exporter(label, context, output)
184 }
185
186 #[doc(hidden)]
188 #[cfg(feature = "alloc")]
189 pub fn peer_cert_chain_der(&self) -> Result<Vec<Vec<u8>>, crate::crypto::tls::ChainError> {
190 self.session.peer_cert_chain_der()
191 }
192
193 pub fn cipher_suite(&self) -> crate::event::api::CipherSuite {
194 self.session.cipher_suite().into_event()
195 }
196}
197
198impl<'a> crate::event::IntoEvent<TlsSession<'a>> for TlsSession<'a> {
199 #[inline]
200 fn into_event(self) -> Self {
201 self
202 }
203}
204
205impl core::fmt::Debug for TlsSession<'_> {
206 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
207 f.debug_struct("TlsSession").finish_non_exhaustive()
208 }
209}
210
211#[cfg(feature = "std")]
212impl<'a> IntoEvent<&'a std::io::Error> for &'a std::io::Error {
213 #[inline]
214 fn into_event(self) -> &'a std::io::Error {
215 self
216 }
217}
218
219pub trait Meta: core::fmt::Debug {
221 fn endpoint_type(&self) -> &api::EndpointType;
223
224 fn subject(&self) -> api::Subject;
228
229 fn timestamp(&self) -> &Timestamp;
231}
232
233impl Meta for api::ConnectionMeta {
234 fn endpoint_type(&self) -> &api::EndpointType {
235 &self.endpoint_type
236 }
237
238 fn subject(&self) -> api::Subject {
239 api::Subject::Connection { id: self.id }
240 }
241
242 fn timestamp(&self) -> &Timestamp {
243 &self.timestamp
244 }
245}
246
247impl Meta for api::EndpointMeta {
248 fn endpoint_type(&self) -> &api::EndpointType {
249 &self.endpoint_type
250 }
251
252 fn subject(&self) -> api::Subject {
253 api::Subject::Endpoint {}
254 }
255
256 fn timestamp(&self) -> &Timestamp {
257 &self.timestamp
258 }
259}