diff options
Diffstat (limited to 'src/service.rs')
-rw-r--r-- | src/service.rs | 57 |
1 files changed, 9 insertions, 48 deletions
diff --git a/src/service.rs b/src/service.rs index 9ca9fc6..b67ba51 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1,8 +1,4 @@ -use std::collections::HashMap; - -use futures::{StreamExt, TryStreamExt}; use serde::Deserialize; -use tokio_stream::{Stream, StreamMap}; use crate::Status; @@ -10,35 +6,11 @@ pub mod command; pub mod http; pub mod tcp; -pub type ServiceHandles = HashMap<String, Status>; - pub trait IntoService { - type Error: std::error::Error + Sync + Send + Sized; - - fn into_service(self) -> impl Stream<Item = Result<(), Self::Error>> + Send; -} - -pub trait IntoServiceMap<K> { - type Error: std::error::Error + Sync + Send + Sized; - - fn into_service_map(self) -> impl Stream<Item = (K, Result<(), Self::Error>)> + Send; -} - -impl<T, K, V> IntoServiceMap<K> for T -where - T: IntoIterator<Item = (K, V)>, - V: IntoService, - K: std::hash::Hash + std::cmp::Eq + std::clone::Clone + std::marker::Unpin + std::marker::Send, -{ - type Error = V::Error; - - fn into_service_map(self) -> impl Stream<Item = (K, Result<(), Self::Error>)> + Send { - let mut map = StreamMap::new(); - for (name, srv) in self.into_iter() { - map.insert(name, Box::pin(srv.into_service())); - } - map - } + fn into_service( + self, + tx: tokio::sync::watch::Sender<Status>, + ) -> impl std::future::Future<Output = ()>; } pub fn default_interval() -> std::time::Duration { @@ -54,30 +26,19 @@ pub struct ServiceConfig { #[derive(Debug, Clone, Deserialize)] #[serde(rename_all = "lowercase")] +#[serde(tag = "kind")] pub enum ServiceKind { Http(http::Http), Tcp(tcp::Tcp), Exec(command::Command), } -#[derive(Debug, thiserror::Error)] -pub enum ServiceError { - #[error(transparent)] - Http(#[from] http::Error), - #[error(transparent)] - Tcp(#[from] tcp::Error), - #[error(transparent)] - Command(#[from] command::Error), -} - impl IntoService for ServiceKind { - type Error = ServiceError; - - fn into_service(self) -> impl Stream<Item = Result<(), Self::Error>> + Send { + async fn into_service(self, tx: tokio::sync::watch::Sender<Status>) { match self { - ServiceKind::Http(h) => h.into_service().map_err(ServiceError::from).boxed(), - ServiceKind::Tcp(t) => t.into_service().map_err(ServiceError::from).boxed(), - ServiceKind::Exec(c) => c.into_service().map_err(ServiceError::from).boxed(), + ServiceKind::Http(h) => h.into_service(tx).await, + ServiceKind::Tcp(t) => t.into_service(tx).await, + ServiceKind::Exec(c) => c.into_service(tx).await, } } } |