helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! The helm-docs comment format (`# -- description`), read in `--helm-docs-compatibility-mode`:
//! `ParseComment` of helm-docs v1.14.2 (`pkg/helm/comment.go`) and `helmDocsTypeToSchemaType`.

use std::sync::LazyLock;

use regex::Regex;

use super::comments::WS;

static VALUE_DESCRIPTION: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(&format!(r"^{WS}*#{WS}*(.*){WS}+--{WS}*(.*)$")).expect("static regex")
});
static RAW: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(&format!(r"^{WS}*#{WS}+@raw")).expect("static regex"));
static CONTINUATION: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(&format!(r"^{WS}*#({WS}?)(.*)$")).expect("static regex"));
static DEFAULT: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(&format!(r"^{WS}*# @default -- (.*)$")).expect("static regex"));
static VALUE_TYPE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(&format!(r"^\((.*?)\){WS}*(.*)$")).expect("static regex"));
static NOTATION: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(&format!(r"^{WS}*#{WS}+@notationType{WS}+--{WS}+(.*)$")).expect("static regex")
});
static SECTION: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(&format!(r"^{WS}*# @section -- (.*)$")).expect("static regex"));

/// The pieces of a helm-docs comment helm-schema reads.
#[derive(Debug, Default)]
pub(super) struct HelmDocs {
    pub(super) description: String,
    pub(super) default: String,
    pub(super) value_type: String,
}

/// helm-docs `ParseComment`, reduced to the fields helm-schema uses.
pub(super) fn parse_helm_docs(lines: &[&str]) -> HelmDocs {
    let last = lines
        .iter()
        .rposition(|l| l.starts_with("# --"))
        .unwrap_or(0);

    if last > 0 {
        return parse_helm_docs(&lines[last..]);
    }

    let mut docs = HelmDocs::default();
    let mut start = 0;

    for (i, line) in lines.iter().enumerate() {
        if let Some(m) = VALUE_DESCRIPTION.captures(line) {
            docs.description = m[2].to_owned();
            start = i;

            break;
        }
    }

    if let Some(m) = VALUE_TYPE.captures(&docs.description)
        && !m[1].is_empty()
    {
        let (value_type, description) = (m[1].to_owned(), m[2].to_owned());

        docs.value_type = value_type;
        docs.description = description;
    }

    let mut raw = false;

    for line in lines.iter().skip(start + 1) {
        if !raw && RAW.is_match(line) {
            raw = true;

            continue;
        }

        if let Some(m) = DEFAULT.captures(line) {
            docs.default = m[1].to_owned();

            continue;
        }

        if NOTATION.is_match(line) || SECTION.is_match(line) {
            continue;
        }

        if let Some(m) = CONTINUATION.captures(line) {
            docs.description.push_str(if raw { "\n" } else { " " });
            docs.description.push_str(&m[2]);
        }
    }

    docs
}

/// `helmDocsTypeToSchemaType`: a helm-docs type list as schema types.
pub(super) fn helm_docs_types(value: &str) -> std::result::Result<Vec<String>, String> {
    value
        .trim()
        .split(',')
        .map(|part| {
            let part = part.trim();

            match part {
                "int" => Ok("integer".to_owned()),
                "bool" => Ok("boolean".to_owned()),
                "float" => Ok("number".to_owned()),
                "list" => Ok("array".to_owned()),
                "map" => Ok("object".to_owned()),
                "tpl" => Ok("string".to_owned()),
                "string" | "object" | "integer" | "number" | "array" | "null" | "boolean" | "" => {
                    Ok(part.to_owned())
                }
                _ => Err(format!(
                    "cant translate helm-docs type ({part}) to helm-schema type"
                )),
            }
        })
        .collect()
}