helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! The signer report Helm prints after it verified a chart or a plugin.

use crate::error::{Error, Result};

/// The signer identities, the key fingerprint and the hash of a verification report.
pub(super) type Signature = (Vec<String>, String, String);

/// Reads the `Signed by:`, `Using Key With Fingerprint:` and `<hash_prefix>` lines
/// (`Chart Hash Verified: ` or `Plugin Hash Verified: `). `None` when the output holds no
/// verification; an error when it holds a fingerprint but no hash.
pub(super) fn parse_signature(
    command: &'static str,
    stdout: &str,
    hash_prefix: &str,
) -> Result<Option<Signature>> {
    let line = |prefix: &str| stdout.lines().find_map(|line| line.strip_prefix(prefix));
    let Some(fingerprint) = line("Using Key With Fingerprint: ") else {
        return Ok(None);
    };
    let hash = line(hash_prefix).ok_or_else(|| Error::Parse {
        command,
        message: format!("no {hash_prefix:?} line in {stdout:?}"),
    })?;
    let signed_by = stdout
        .lines()
        .filter_map(|line| line.strip_prefix("Signed by: "))
        .map(str::to_owned)
        .collect();

    Ok(Some((signed_by, fingerprint.to_owned(), hash.to_owned())))
}

/// Like [`parse_signature`] for commands whose success always reports a verification.
pub(super) fn require_signature(
    command: &'static str,
    stdout: &str,
    hash_prefix: &str,
) -> Result<Signature> {
    parse_signature(command, stdout, hash_prefix)?.ok_or_else(|| Error::Parse {
        command,
        message: format!("no \"Using Key With Fingerprint: \" line in {stdout:?}"),
    })
}