use std::sync::Arc;
use std::time::Duration;
use crate::{ClientOptions, Envelope};
mod options;
pub use self::options::TransportOptions;
pub trait Transport: Send + Sync + 'static {
fn send_envelope(&self, envelope: Envelope);
fn flush(&self, timeout: Duration) -> bool {
let _timeout = timeout;
true
}
fn shutdown(&self, timeout: Duration) -> bool {
self.flush(timeout)
}
}
pub trait TransportFactory: Send + Sync {
fn create_transport_with_options(&self, options: TransportOptions) -> Arc<dyn Transport> {
#[expect(deprecated, reason = "need to call deprecated method for back-compat")]
self.create_transport(&options.into_client_options())
}
#[deprecated = "use and implement `create_transport_with_options` instead"]
fn create_transport(&self, options: &ClientOptions) -> Arc<dyn Transport> {
TransportOptions::try_from_client_options(options).map_or_else(
|| {
let no_op: Arc<dyn Transport> = Arc::new(NoOpTransport);
no_op
},
|options| self.create_transport_with_options(options),
)
}
}
struct NoOpTransport;
impl<F> TransportFactory for F
where
F: Fn(&ClientOptions) -> Arc<dyn Transport> + Clone + Send + Sync + 'static,
{
fn create_transport(&self, options: &ClientOptions) -> Arc<dyn Transport> {
(*self)(options)
}
}
impl<T: Transport> Transport for Arc<T> {
fn send_envelope(&self, envelope: Envelope) {
(**self).send_envelope(envelope)
}
fn shutdown(&self, timeout: Duration) -> bool {
(**self).shutdown(timeout)
}
}
impl<T: Transport> TransportFactory for Arc<T> {
fn create_transport_with_options(&self, _: TransportOptions) -> Arc<dyn Transport> {
self.clone()
}
}
impl Transport for NoOpTransport {
fn send_envelope(&self, envelope: Envelope) {
let _ = envelope;
}
}