diff options
Diffstat (limited to 'src/service')
-rw-r--r-- | src/service/http.rs | 11 | ||||
-rw-r--r-- | src/service/systemd.rs | 20 | ||||
-rw-r--r-- | src/service/tcp.rs | 9 |
3 files changed, 18 insertions, 22 deletions
diff --git a/src/service/http.rs b/src/service/http.rs index 15696a1..fb3ff13 100644 --- a/src/service/http.rs +++ b/src/service/http.rs @@ -2,7 +2,7 @@ use std::fmt::Display; use serde::Deserialize; -use crate::{Check, Error, Status}; +use crate::{Error, Status}; #[derive(Debug, Clone, Deserialize)] pub struct Http { @@ -30,7 +30,7 @@ impl Display for Http { } impl Http { - pub async fn check(&self, client: reqwest::Client) -> Result<Check, Error> { + pub async fn check(&self, client: reqwest::Client) -> Result<Status, Error> { let status_code = client .request(self.method.parse().map_err(|_| Error::Method)?, &self.url) .send() @@ -39,11 +39,8 @@ impl Http { .as_u16(); match status_code == self.status_code { - true => Ok(Check::default()), - false => Ok(Check { - status: Status::Fail, - output: Some(format!("Status code: {status_code}")), - }), + true => Ok(Status::Pass), + false => Ok(Status::Fail(Some(format!("Status code: {status_code}")))), } } } diff --git a/src/service/systemd.rs b/src/service/systemd.rs index 2e3b74c..45f3bf9 100644 --- a/src/service/systemd.rs +++ b/src/service/systemd.rs @@ -2,7 +2,7 @@ use std::{fmt::Display, process::Command}; use serde::Deserialize; -use crate::{Check, Error, Status}; +use crate::{Error, Status}; #[derive(Debug, Clone, Deserialize)] pub struct Systemd { @@ -16,18 +16,20 @@ impl Display for Systemd { } impl Systemd { - pub async fn check(&self) -> Result<Check, Error> { + pub async fn check(&self) -> Result<Status, Error> { let output = Command::new("systemctl") .arg("is-active") .arg(&self.service) .output()?; - let stdout = String::from_utf8_lossy(&output.stdout).to_string(); - Ok((!output.status.success()) - .then(|| Check { - status: Status::Fail, - output: Some(format!("Service state: {}", stdout.trim())), - }) - .unwrap_or_default()) + let status = match output.status.success() { + true => Status::Pass, + false => { + let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string(); + Status::Fail(Some(format!("Service state: {}", stdout))) + } + }; + + Ok(status) } } diff --git a/src/service/tcp.rs b/src/service/tcp.rs index 5f55091..87e696a 100644 --- a/src/service/tcp.rs +++ b/src/service/tcp.rs @@ -2,7 +2,7 @@ use std::fmt::Display; use serde::Deserialize; -use crate::{Check, Error, Status}; +use crate::{Error, Status}; #[derive(Debug, Clone, Deserialize)] pub struct Tcp { @@ -16,13 +16,10 @@ impl Display for Tcp { } impl Tcp { - pub async fn check(&self) -> Result<Check, Error> { + pub async fn check(&self) -> Result<Status, Error> { Ok(std::net::TcpStream::connect(&self.address) .err() - .map(|err| Check { - status: Status::Fail, - output: Some(format!("error: {err}")), - }) + .map(Into::into) .unwrap_or_default()) } } |