use std::fmt;
use crate::{error::SyRes, AsyncSyslogDestination, TapType};
#[allow(async_fn_in_trait)]
pub trait AsyncSyslogTap<D: AsyncSyslogDestination + fmt::Display>: fmt::Debug + Send
{
fn new(req_tap: D) -> SyRes<Self> where Self: Sized;
async fn connectlog(&mut self) -> SyRes<()>;
async fn send(&mut self, msg: &[u8]) -> std::io::Result<usize>;
async fn disconnectlog(&mut self) -> std::io::Result<()>;
fn is_connected(&self) -> bool;
fn get_type(&self) -> TapType;
fn get_max_msg_size() -> usize;
fn update_tap_data(&mut self, tap_data: D);
}
#[derive(Debug)]
pub struct AsyncTap<T, D: AsyncSyslogDestination>
{
pub(crate) sock: Option<T>,
pub(crate) tap_data: D,
pub(crate) cur_tap_type: TapType,
}
#[cfg(feature = "build_async_interface")]
impl<T, D: AsyncSyslogDestination> AsyncTap<T, D>
{
pub
fn new(req_tap: D) -> SyRes<AsyncTap<T, D>>
{
return Ok(
Self
{
sock: None,
tap_data: req_tap,
cur_tap_type: TapType::None,
}
);
}
pub
fn get_tap_data(&self) -> &D
{
return &self.tap_data;
}
pub
fn update_tap_data(&mut self, tap_data: D)
{
self.tap_data = tap_data;
}
pub
fn get_tap_type(&self) -> TapType
{
return self.cur_tap_type;
}
pub
fn get_sock(&self) -> Option<&T>
{
return self.sock.as_ref();
}
pub
fn get_sock_mut(&mut self) -> Option<&mut T>
{
return self.sock.as_mut();
}
pub
fn take_sock(&mut self) -> Option<T>
{
return self.sock.take();
}
pub
fn set_sock(&mut self, sock: T)
{
self.sock.replace(sock);
return;
}
pub
fn set_cur_tap_type(&mut self, cur_tap_type: TapType)
{
return self.cur_tap_type = cur_tap_type;
}
}