grow-graylog 0.2.0

Rust SDK for Graylog Open by Grow Codes
Documentation
// Generated by sdk-kit 0.3.0 — do not edit; change sdk-kit and run "sdk-kit sync"
//! Health result (SPEC §2.4).

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use super::error::{Error, ErrorCode, Result};

/// Outcome of `health()`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HealthStatus {
    /// Whether the service is usable.
    pub healthy: bool,
    /// `ok`, `degraded`, `unhealthy`, `unreachable` or the vendor's word.
    pub status: String,
    /// Extra vendor details.
    #[serde(default)]
    pub details: BTreeMap<String, String>,
}

impl HealthStatus {
    /// A healthy status with the given word.
    pub fn healthy(status: impl Into<String>) -> Self {
        HealthStatus {
            healthy: true,
            status: status.into(),
            details: BTreeMap::new(),
        }
    }

    /// An unhealthy status with the given word.
    pub fn unhealthy(status: impl Into<String>) -> Self {
        HealthStatus {
            healthy: false,
            status: status.into(),
            details: BTreeMap::new(),
        }
    }

    /// Adds a detail entry.
    pub fn with_detail(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.details.insert(key.into(), value.into());
        self
    }

    /// Converts a failed health request into a status. `CANCELLED` and `CONFIG` stay errors;
    /// network failures become `unreachable`, HTTP errors `unhealthy` with the status detail.
    pub fn from_error(err: Error) -> Result<HealthStatus> {
        match err.code() {
            ErrorCode::Cancelled | ErrorCode::Config => Err(err),
            _ => Ok(match err.http_status() {
                None => HealthStatus::unhealthy("unreachable").with_detail("error", err.message()),
                Some(status) => HealthStatus::unhealthy("unhealthy")
                    .with_detail("httpStatus", status.to_string()),
            }),
        }
    }
}