Functional Weave
Code in Rust

monitor.component-state@1.0.0

impl/rust.rs

1,410 bytes · the Rust implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one

/// One component's state in Atlassian Statuspage's vocabulary. Maintenance
/// wins, so planned work never pages anyone as an outage; the statuses are
/// still checked, so a bad feed fails loudly even during maintenance.
///
/// # Panics
/// Panics on an empty list or an unknown status.
pub fn component_state(statuses: &[String], under_maintenance: bool) -> String {
    if statuses.is_empty() {
        panic!("statuses must not be empty");
    }
    let mut down = 0usize;
    let mut degraded = 0usize;
    for s in statuses {
        match s.as_str() {
            "down" => down += 1,
            "degraded" => degraded += 1,
            "up" => {}
            other => panic!("unknown check status: {}", other),
        }
    }
    let state = if under_maintenance {
        "maintenance"
    } else if down == statuses.len() {
        "major-outage"
    } else if down > 0 {
        "partial-outage"
    } else if degraded > 0 {
        "degraded"
    } else {
        "operational"
    };
    state.to_string()
}

pub fn fune_vector(args: &[Value]) -> Value {
    let statuses: Vec<String> = args[0].as_arr().iter().map(|s| s.as_str().to_string()).collect();
    let under_maintenance = match &args[1] {
        Value::Bool(b) => *b,
        _ => panic!("underMaintenance must be true or false"),
    };
    Value::str(&component_state(&statuses, under_maintenance))
}