helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! Errors of the schema generator. Messages follow helm-schema's, so a failure reads the same
//! as in the Go tool; [`GenerateError::Annotation`] also exposes the line it names.

use super::yaml::YamlError;

/// Why a schema could not be generated.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum GenerateError {
    /// The `values.yaml` (or a `Chart.yaml`-level YAML document) is not valid YAML.
    #[error("{0}")]
    Yaml(#[from] YamlError),
    /// An annotation block or comment could not be read: an unclosed `# @schema` block, or YAML
    /// that does not decode into a schema. `line` is the line inside the annotation block that
    /// helm-schema's message names (relative to the block, not to the file).
    #[error("{context}: {message}")]
    Annotation {
        /// What was being read, such as `error parsing comment of key image`.
        context: String,
        /// The line named by `message`, when it names one.
        line: Option<usize>,
        /// The decoder's message.
        message: String,
    },
    /// A decoded annotation is inconsistent, a `$ref` cannot be resolved, a type cannot be
    /// inferred or a final schema is invalid.
    #[error("{context}: {message}")]
    Schema {
        /// What was being processed, such as `error validating schema of key port`.
        context: String,
        /// The problem.
        message: String,
    },
    /// A dependency of the chart cannot be bound or merged.
    #[error("{0}")]
    Dependency(String),
}

impl GenerateError {
    pub(super) fn annotation(context: String, message: String) -> Self {
        let line = message.find("line ").and_then(|i| {
            let digits: String = message[i + 5..]
                .chars()
                .take_while(char::is_ascii_digit)
                .collect();

            digits.parse().ok()
        });

        Self::Annotation {
            context,
            line,
            message,
        }
    }

    pub(super) fn schema(context: String, message: String) -> Self {
        Self::Schema { context, message }
    }
}

/// The result of schema generation.
pub type Result<T> = std::result::Result<T, GenerateError>;