From 8b9eb6eb88d871309348dff1527d69b4b32a98ec Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Sat, 12 Oct 2024 18:23:46 -0500 Subject: refactor: simplify service trait, again --- src/service.rs | 57 +++++++++------------------------------------------------ 1 file changed, 9 insertions(+), 48 deletions(-) (limited to 'src/service.rs') 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; - pub trait IntoService { - type Error: std::error::Error + Sync + Send + Sized; - - fn into_service(self) -> impl Stream> + Send; -} - -pub trait IntoServiceMap { - type Error: std::error::Error + Sync + Send + Sized; - - fn into_service_map(self) -> impl Stream)> + Send; -} - -impl IntoServiceMap for T -where - T: IntoIterator, - 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)> + 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, + ) -> impl std::future::Future; } 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> + Send { + async fn into_service(self, tx: tokio::sync::watch::Sender) { 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, } } } -- cgit v1.2.3-70-g09d2