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"));
#[derive(Debug, Default)]
pub(super) struct HelmDocs {
pub(super) description: String,
pub(super) default: String,
pub(super) value_type: String,
}
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
}
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()
}