helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! A YAML reader with the semantics of `gopkg.in/yaml.v3` v3.0.1, the library helm-schema decodes
//! `values.yaml` with.
//!
//! helm-schema reads descriptions from the comments `yaml.v3` attaches to nodes, infers types
//! from the tags it resolves, and reports the line numbers it prints, so a different YAML library
//! would produce different schemas. This module is a port of `yaml.v3`'s scanner
//! (`scannerc.go`), parser (`parserc.go`), node builder (`decode.go`) and tag resolver
//! (`resolve.go`), which are themselves ports of libyaml. Only the pieces helm-schema reaches are
//! ported: decoding a stream into the first document's [`Node`] tree, comments included.
//!
//! Upstream licences: `yaml.v3` is MIT and Apache-2.0 licensed (Copyright 2011-2019 Canonical
//! Ltd, Copyright 2006-2010 Kirill Simonov for the libyaml parts).

mod block;
mod comments;
mod events;
mod fetch;
mod flow;
mod keys;
mod node;
mod nodes;
mod parser;
mod plain;
mod queue;
mod quoted;
mod resolve;
mod scalars;
mod scanner;
mod timestamp;
mod token;
mod tree;

pub(super) use node::{Doc, Kind, Node, Style};
pub(super) use resolve::{Resolved, go_parse_float, resolve};
pub(super) use timestamp::Timestamp;

/// A position in the input, as `yaml.v3` marks it: character index, 0-based line and column.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(super) struct Mark {
    pub(super) index: usize,
    pub(super) line: usize,
    pub(super) column: usize,
}

/// A YAML error, formatted like `yaml.v3`: `yaml: line N: problem` (no line when it is 0).
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{}", self.render())]
pub struct YamlError {
    /// The line `yaml.v3` prints, 0 when it prints none.
    pub line: usize,
    /// The problem, without the `yaml: line N:` prefix.
    pub message: String,
    /// Whether the message is printed without the `yaml:` prefix (errors of custom unmarshalers).
    pub raw: bool,
}

impl YamlError {
    fn render(&self) -> String {
        if self.raw {
            return self.message.clone();
        }

        if self.line == 0 {
            return format!("yaml: {}", self.message);
        }

        format!("yaml: line {}: {}", self.line, self.message)
    }

    /// A scanner error: `yaml.v3` adds one to the line of the mark it reports.
    pub(super) fn scanner(context: Mark, problem: Mark, message: &str) -> Self {
        let line = match (context.line, problem.line) {
            (0, 0) => 0,
            (0, line) | (line, _) => line + 1,
        };

        Self {
            line,
            message: message.to_owned(),
            raw: false,
        }
    }

    /// A parser error: the line is the 0-based line of the context (or problem) mark.
    pub(super) fn parser(context: Mark, problem: Mark, message: &str) -> Self {
        let line = if context.line == 0 {
            problem.line
        } else {
            context.line
        };

        Self {
            line,
            message: message.to_owned(),
            raw: false,
        }
    }

    /// An error without a position.
    pub(super) fn plain(message: &str) -> Self {
        Self {
            line: 0,
            message: message.to_owned(),
            raw: false,
        }
    }

    /// An error printed verbatim, as a custom unmarshaler's error is.
    pub(super) fn plain_raw(message: &str) -> Self {
        Self {
            line: 0,
            message: message.to_owned(),
            raw: true,
        }
    }
}

/// Parses the first document of `input` like `yaml.Unmarshal` into a `yaml.Node` does. Empty
/// input is a document without a root, as in `yaml.v3`.
///
/// # Errors
/// The first scanner or parser error, in `yaml.v3`'s words.
pub(super) fn parse(input: &str) -> Result<Doc, YamlError> {
    parser::Parser::new(input)?.load()
}

#[cfg(test)]
mod tests;