mod bytes_received;
mod bytes_sent;
pub mod cached_event;
pub mod component_events_dropped;
mod events_received;
mod events_sent;
mod optional_tag;
mod prelude;
pub mod service;
use std::ops::{Add, AddAssign};
pub use metrics::SharedString;
pub use bytes_received::BytesReceived;
pub use bytes_sent::BytesSent;
#[allow(clippy::module_name_repetitions)]
pub use cached_event::{RegisterTaggedInternalEvent, RegisteredEventCache};
pub use component_events_dropped::{ComponentEventsDropped, INTENTIONAL, UNINTENTIONAL};
pub use events_received::EventsReceived;
pub use events_sent::{EventsSent, TaggedEventsSent, DEFAULT_OUTPUT};
pub use optional_tag::OptionalTag;
pub use prelude::{error_stage, error_type};
pub use service::{CallError, PollReadyError};
use crate::json_size::JsonSize;
pub trait InternalEvent: Sized {
fn emit(self);
fn name(&self) -> Option<&'static str> {
None
}
}
#[allow(clippy::module_name_repetitions)]
pub trait RegisterInternalEvent: Sized {
type Handle: InternalEventHandle;
fn register(self) -> Self::Handle;
fn name(&self) -> Option<&'static str> {
None
}
}
#[allow(clippy::module_name_repetitions)]
pub trait InternalEventHandle: Sized {
type Data: Sized;
fn emit(&self, data: Self::Data);
}
pub struct DefaultName<E> {
pub name: &'static str,
pub event: E,
}
impl<E: InternalEvent> InternalEvent for DefaultName<E> {
fn emit(self) {
self.event.emit();
}
fn name(&self) -> Option<&'static str> {
Some(self.event.name().unwrap_or(self.name))
}
}
impl<E: RegisterInternalEvent> RegisterInternalEvent for DefaultName<E> {
type Handle = E::Handle;
fn register(self) -> Self::Handle {
self.event.register()
}
fn name(&self) -> Option<&'static str> {
Some(self.event.name().unwrap_or(self.name))
}
}
#[cfg(any(test, feature = "test"))]
pub fn emit(event: impl InternalEvent) {
if let Some(name) = event.name() {
super::event_test_util::record_internal_event(name);
}
event.emit();
}
#[cfg(not(any(test, feature = "test")))]
pub fn emit(event: impl InternalEvent) {
event.emit();
}
#[cfg(any(test, feature = "test"))]
pub fn register<E: RegisterInternalEvent>(event: E) -> E::Handle {
if let Some(name) = event.name() {
super::event_test_util::record_internal_event(name);
}
event.register()
}
#[cfg(not(any(test, feature = "test")))]
pub fn register<E: RegisterInternalEvent>(event: E) -> E::Handle {
event.register()
}
pub type Registered<T> = <T as RegisterInternalEvent>::Handle;
#[derive(Clone, Copy)]
pub struct ByteSize(pub usize);
#[derive(Clone, Copy)]
pub struct Count(pub usize);
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct CountByteSize(pub usize, pub JsonSize);
impl AddAssign for CountByteSize {
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0;
self.1 += rhs.1;
}
}
impl Add<CountByteSize> for CountByteSize {
type Output = CountByteSize;
fn add(self, rhs: CountByteSize) -> Self::Output {
CountByteSize(self.0 + rhs.0, self.1 + rhs.1)
}
}
pub struct Output(pub Option<SharedString>);
pub struct Protocol(pub SharedString);
impl Protocol {
pub const HTTP: Protocol = Protocol(SharedString::const_str("http"));
pub const HTTPS: Protocol = Protocol(SharedString::const_str("https"));
pub const NONE: Protocol = Protocol(SharedString::const_str("none"));
pub const TCP: Protocol = Protocol(SharedString::const_str("tcp"));
pub const UDP: Protocol = Protocol(SharedString::const_str("udp"));
pub const UNIX: Protocol = Protocol(SharedString::const_str("unix"));
pub const INTERNAL: Protocol = Protocol(SharedString::const_str("internal"));
pub const STATIC: Protocol = Protocol(SharedString::const_str("static"));
}
impl From<&'static str> for Protocol {
fn from(s: &'static str) -> Self {
Self(SharedString::const_str(s))
}
}
impl From<Protocol> for SharedString {
fn from(value: Protocol) -> Self {
value.0
}
}
#[macro_export]
macro_rules! registered_event {
($event:ident => $($tail:tt)*) => {
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $event;
$crate::registered_event!(=> $event $($tail)*);
};
($event:ident { $( $field:ident: $type:ty, )* } => $($tail:tt)*) => {
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $event {
$( pub $field: $type, )*
}
$crate::registered_event!(=> $event $($tail)*);
};
(
=> $event:ident {
$( $field:ident: $type:ty = $value:expr, )*
}
fn emit(&$slf:ident, $data_name:ident: $data:ident)
$emit_body:block
$(fn register($fixed_name:ident: $fixed_tags:ty, $tags_name:ident: $tags:ty)
$register_body:block)?
) => {
paste::paste!{
#[derive(Clone)]
pub struct [<$event Handle>] {
$( $field: $type, )*
}
impl $crate::internal_event::RegisterInternalEvent for $event {
type Handle = [<$event Handle>];
fn name(&self) -> Option<&'static str> {
Some(stringify!($event))
}
fn register($slf) -> Self::Handle {
Self::Handle {
$( $field: $value, )*
}
}
}
impl $crate::internal_event::InternalEventHandle for [<$event Handle>] {
type Data = $data;
fn emit(&$slf, $data_name: $data)
$emit_body
}
$(impl $crate::internal_event::cached_event::RegisterTaggedInternalEvent for $event {
type Tags = $tags;
type Fixed = $fixed_tags;
fn register(
$fixed_name: $fixed_tags,
$tags_name: $tags,
) -> <Self as $crate::internal_event::RegisterInternalEvent>::Handle {
$register_body
}
})?
}
};
}