use std::fmt;
use std::time::Duration;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct CheckOutcome {
pub stages: Vec<Stage>,
pub total: Duration,
pub status: Status,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Status {
Ready,
Failed,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Stage {
pub kind: StageKind,
pub took: Duration,
pub result: StageResult,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum StageKind {
Dns,
Tcp,
Http,
File,
Postgres,
Redis,
Mysql,
Exec,
Grpc,
Log,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum StageResult {
Ok,
Err {
message: Box<str>,
hint: Option<Box<str>>,
},
}
impl CheckOutcome {
#[must_use]
pub const fn ready(stages: Vec<Stage>, total: Duration) -> Self {
Self {
stages,
total,
status: Status::Ready,
}
}
#[must_use]
pub const fn failed(stages: Vec<Stage>, total: Duration) -> Self {
Self {
stages,
total,
status: Status::Failed,
}
}
#[must_use]
pub const fn is_ready(&self) -> bool {
matches!(self.status, Status::Ready)
}
}
impl StageKind {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Dns => "dns",
Self::Tcp => "tcp",
Self::Http => "http",
Self::File => "file",
Self::Postgres => "postgres",
Self::Redis => "redis",
Self::Mysql => "mysql",
Self::Exec => "exec",
Self::Grpc => "grpc",
Self::Log => "log",
}
}
}
impl fmt::Display for StageKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Dns => "DNS resolution",
Self::Tcp => "TCP connect",
Self::Http => "HTTP request",
Self::File => "filesystem",
Self::Postgres => "Postgres query",
Self::Redis => "Redis PING",
Self::Mysql => "MySQL query",
Self::Exec => "external command",
Self::Grpc => "gRPC health",
Self::Log => "log file match",
})
}
}