helm_rs/schema/generate/
annotation.rs1use std::collections::BTreeMap;
4
5use serde_json::Value;
6
7use super::decode::AliasBudget;
8use super::error::{GenerateError, Result};
9use super::yaml::{self, Kind, YamlError};
10use super::{comments, parse_values};
11
12#[derive(Debug, Clone, PartialEq)]
14pub struct Annotation {
15 pub path: Vec<String>,
17 pub keywords: BTreeMap<String, Value>,
19 pub description: String,
21 pub root: bool,
23}
24
25struct Walk<'a> {
27 doc: &'a yaml::Doc,
28 keep_full_comment: bool,
29 path: Vec<String>,
30 active: Vec<usize>,
31 budget: AliasBudget,
32 out: Vec<Annotation>,
33}
34
35impl Walk<'_> {
36 fn collect(&mut self, id: usize, first_comment: Option<&str>) -> Result<()> {
39 let doc = self.doc;
40 let node = &doc.nodes[id];
41
42 if node.kind != Kind::Mapping {
43 return Ok(());
44 }
45
46 self.active.push(id);
47
48 for (index, pair) in node.content.chunks(2).enumerate() {
49 self.budget.tick()?;
50
51 let key = &doc.nodes[pair[0]];
52 let head = match first_comment {
53 Some(text) if index == 0 => text,
54 _ => key.head_comment.as_str(),
55 };
56 let comment = if self.keep_full_comment {
57 head
58 } else {
59 comments::remove_leading(head)
60 };
61
62 self.path.push(key.value.clone());
63
64 if let Some((keywords, description)) =
65 comments::keywords(comment, false).map_err(|m| {
66 GenerateError::annotation(
67 format!("error parsing comment of key {}", key.value),
68 m,
69 )
70 })?
71 {
72 self.out.push(Annotation {
73 path: self.path.clone(),
74 keywords,
75 description,
76 root: false,
77 });
78 }
79
80 let value = &doc.nodes[pair[1]];
81 let target = if value.kind == Kind::Alias {
82 value.alias.unwrap_or(pair[1])
83 } else {
84 pair[1]
85 };
86
87 if self.active.contains(&target) {
88 return Err(YamlError::plain(&format!(
89 "anchor '{}' value contains itself",
90 value.value
91 ))
92 .into());
93 }
94
95 if target != pair[1] {
96 self.budget.enter_alias();
97 }
98
99 self.collect(target, None)?;
100
101 if target != pair[1] {
102 self.budget.leave_alias();
103 }
104
105 self.path.pop();
106 }
107
108 self.active.pop();
109
110 Ok(())
111 }
112}
113
114pub fn annotations(values_yaml: &str, keep_full_comment: bool) -> Result<Vec<Annotation>> {
120 let doc = parse_values(&values_yaml.replace("\r\n", "\n"))?;
121 let root = doc.root.unwrap_or_default();
122 let mut out = Vec::new();
123 let mut heads = vec![doc.nodes[root].head_comment.as_str()];
124 let mut first_comment = None;
125
126 if let Some(&first) = doc.nodes[root].content.first()
127 && let Some(&key) = doc.nodes[first].content.first()
128 {
129 heads.push(&doc.nodes[key].head_comment);
130 }
131
132 for (index, head) in heads.into_iter().enumerate() {
133 if let Some((keywords, description)) = comments::keywords(head, true).map_err(|m| {
134 GenerateError::annotation("error parsing root schema comment".to_owned(), m)
135 })? {
136 if index == 1 {
137 first_comment = Some(description.clone());
138 }
139
140 out.push(Annotation {
141 path: Vec::new(),
142 keywords,
143 description,
144 root: true,
145 });
146 }
147 }
148
149 let mut walk = Walk {
150 doc: &doc,
151 keep_full_comment,
152 path: Vec::new(),
153 active: Vec::new(),
154 budget: AliasBudget::default(),
155 out,
156 };
157
158 if let Some(&child) = doc.nodes[root].content.first() {
159 walk.collect(child, first_comment.as_deref())?;
160 }
161
162 Ok(walk.out)
163}
164
165#[cfg(test)]
166mod tests;