Skip to main content

helm_rs/schema/generate/
error.rs

1//! Errors of the schema generator. Messages follow helm-schema's, so a failure reads the same
2//! as in the Go tool; [`GenerateError::Annotation`] also exposes the line it names.
3
4use super::yaml::YamlError;
5
6/// Why a schema could not be generated.
7#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
8pub enum GenerateError {
9    /// The `values.yaml` (or a `Chart.yaml`-level YAML document) is not valid YAML.
10    #[error("{0}")]
11    Yaml(#[from] YamlError),
12    /// An annotation block or comment could not be read: an unclosed `# @schema` block, or YAML
13    /// that does not decode into a schema. `line` is the line inside the annotation block that
14    /// helm-schema's message names (relative to the block, not to the file).
15    #[error("{context}: {message}")]
16    Annotation {
17        /// What was being read, such as `error parsing comment of key image`.
18        context: String,
19        /// The line named by `message`, when it names one.
20        line: Option<usize>,
21        /// The decoder's message.
22        message: String,
23    },
24    /// A decoded annotation is inconsistent, a `$ref` cannot be resolved, a type cannot be
25    /// inferred or a final schema is invalid.
26    #[error("{context}: {message}")]
27    Schema {
28        /// What was being processed, such as `error validating schema of key port`.
29        context: String,
30        /// The problem.
31        message: String,
32    },
33    /// A dependency of the chart cannot be bound or merged.
34    #[error("{0}")]
35    Dependency(String),
36}
37
38impl GenerateError {
39    pub(super) fn annotation(context: String, message: String) -> Self {
40        let line = message.find("line ").and_then(|i| {
41            let digits: String = message[i + 5..]
42                .chars()
43                .take_while(char::is_ascii_digit)
44                .collect();
45
46            digits.parse().ok()
47        });
48
49        Self::Annotation {
50            context,
51            line,
52            message,
53        }
54    }
55
56    pub(super) fn schema(context: String, message: String) -> Self {
57        Self::Schema { context, message }
58    }
59}
60
61/// The result of schema generation.
62pub type Result<T> = std::result::Result<T, GenerateError>;