pub enum YamlComment {
Field(&'static str, Vec<&'static str>),
}
pub fn add_comments(yaml: String, comments: Vec<YamlComment>) -> String {
let mut result = yaml.trim().to_string();
let mut is_first = true;
for comment in comments {
let YamlComment::Field(field, lines) = comment;
let comment_block = lines
.iter()
.map(|l| format!("# {}", l.trim()).trim().to_string())
.collect::<Vec<_>>()
.join("\n");
if is_first {
result = result.replace(
&format!("{}:", field),
&format!("{}\n{}:", comment_block, field),
);
is_first = false;
} else {
let pattern = format!("\n{}:", field);
let replacement = format!("\n\n{}\n{}:", comment_block, field);
if result.contains(&pattern) {
result = result.replace(&pattern, &replacement);
}
}
}
result
}