summaryrefslogtreecommitdiffstats
path: root/src/service/tcp.rs
blob: 5f550915d40595764a1cf416be77784e704595f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use std::fmt::Display;

use serde::Deserialize;

use crate::{Check, Error, Status};

#[derive(Debug, Clone, Deserialize)]
pub struct Tcp {
    pub address: String,
}

impl Display for Tcp {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "tcp://{}", self.address)
    }
}

impl Tcp {
    pub async fn check(&self) -> Result<Check, Error> {
        Ok(std::net::TcpStream::connect(&self.address)
            .err()
            .map(|err| Check {
                status: Status::Fail,
                output: Some(format!("error: {err}")),
            })
            .unwrap_or_default())
    }
}