use serde::Deserialize; use crate::{status::Sender, Status}; pub mod command; pub mod http; pub mod tcp; pub trait IntoService { fn into_service(self, tx: Sender) -> impl std::future::Future; } impl IntoService for () { async fn into_service(self, tx: Sender) { let mut interval = tokio::time::interval(std::time::Duration::from_secs(3)); let mut status = Status::Ok; loop { interval.tick().await; status = match tx.send_replace(status) { Status::Ok => Status::Error(Some("Test status is in the error state".into())), Status::Error(_) => Status::Ok, }; } } } pub fn default_interval() -> std::time::Duration { std::time::Duration::from_secs(5) } #[derive(Debug, Clone, Deserialize)] pub struct ServiceConfig { pub name: String, #[serde(flatten)] pub kind: ServiceKind, } #[derive(Debug, Clone, Deserialize)] #[serde(rename_all = "lowercase")] #[serde(tag = "kind")] pub enum ServiceKind { Http(http::Http), Tcp(tcp::Tcp), Exec(command::Command), Test(()), } impl IntoService for ServiceKind { async fn into_service(self, tx: Sender) { match self { ServiceKind::Test(()) => ().into_service(tx).await, ServiceKind::Http(h) => h.into_service(tx).await, ServiceKind::Tcp(t) => t.into_service(tx).await, ServiceKind::Exec(c) => c.into_service(tx).await, } } }