canic-host 0.100.71

Host-side App build, Fleet install, deployment, and release-set library for Canic workspaces
Documentation
use super::{ReplicaQueryError, cbor::decode_status_root_key, nonempty_text, transport};
use std::path::Path;

#[must_use]
pub fn local_replica_status_reachable_from_root(
    environment: Option<&str>,
    icp_root: &Path,
) -> bool {
    transport::get_http_status(&transport::local_replica_endpoint_from_root(
        environment,
        icp_root,
    ))
    .is_ok()
}

pub fn local_replica_root_key_from_root(
    environment: Option<&str>,
    icp_root: &Path,
) -> Result<Option<String>, ReplicaQueryError> {
    let endpoint = transport::local_replica_endpoint_from_root(environment, icp_root);
    let body = transport::get_http_status(&endpoint)?;
    Ok(parse_local_replica_root_key(&body))
}

pub(super) fn parse_local_replica_root_key(body: &[u8]) -> Option<String> {
    serde_json::from_slice::<serde_json::Value>(body)
        .ok()
        .and_then(|value| root_key_from_json(&value))
        .or_else(|| decode_status_root_key(body).ok().flatten())
}

fn root_key_from_json(value: &serde_json::Value) -> Option<String> {
    match value {
        serde_json::Value::String(text) => nonempty_text(text),
        serde_json::Value::Array(values) => values.iter().find_map(root_key_from_json),
        serde_json::Value::Object(map) => map
            .get("root_key")
            .and_then(root_key_from_json)
            .or_else(|| map.values().find_map(root_key_from_json)),
        _ => None,
    }
}

#[cfg(test)]
mod tests;