helm-rs 0.1.1

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! The parser state shared by the scanner, the event state machine and the tree builder
//! (`yaml_parser_t` in `yamlh.go`).

use std::collections::HashMap;

use super::events::{Event, State};
use super::token::{Comment, SimpleKey, Token};
use super::{Doc, Mark, YamlError};

/// The whole parser. The scanner, the event state machine and the tree builder are `impl`
/// blocks in sibling modules, as in `yaml.v3` they are functions over one `yaml_parser_t`.
pub(super) struct Parser {
    pub(super) buf: Vec<u8>,
    pub(super) pos: usize,
    pub(super) mark: Mark,
    pub(super) newlines: usize,
    pub(super) comments: Vec<Comment>,
    pub(super) comments_head: usize,
    pub(super) head_comment: Vec<u8>,
    pub(super) line_comment: Vec<u8>,
    pub(super) foot_comment: Vec<u8>,
    pub(super) stem_comment: Option<Vec<u8>>,
    pub(super) stream_start_produced: bool,
    pub(super) stream_end_produced: bool,
    pub(super) flow_level: usize,
    pub(super) tokens: Vec<Token>,
    pub(super) tokens_head: usize,
    pub(super) tokens_parsed: usize,
    pub(super) token_available: bool,
    pub(super) indent: isize,
    pub(super) indents: Vec<isize>,
    pub(super) simple_key_allowed: bool,
    pub(super) simple_keys: Vec<SimpleKey>,
    pub(super) simple_keys_by_tok: HashMap<usize, usize>,
    pub(super) state: State,
    pub(super) states: Vec<State>,
    pub(super) marks: Vec<Mark>,
    pub(super) tag_directives: Vec<(Vec<u8>, Vec<u8>)>,
    pub(super) doc: Doc,
    pub(super) anchors: HashMap<String, usize>,
    pub(super) event: Option<Event>,
}

impl Parser {
    /// Creates a parser over `input`, rejecting what `yaml.v3`'s reader rejects: control
    /// characters. A byte order mark is skipped, empty input reads as a newline.
    pub(super) fn new(input: &str) -> Result<Self, YamlError> {
        let text = input.strip_prefix('\u{feff}').unwrap_or(input);
        let text = if text.is_empty() { "\n" } else { text };

        for c in text.chars() {
            let value = u32::from(c);
            let allowed = matches!(value, 0x09 | 0x0A | 0x0D | 0x20..=0x7E | 0x85 | 0xA0..=0xD7FF | 0xE000..=0xFFFD | 0x1_0000..=0x10_FFFF);

            if !allowed {
                return Err(YamlError::plain("control characters are not allowed"));
            }
        }

        let mut buf = text.as_bytes().to_vec();

        buf.extend_from_slice(&[0; 8]);

        Ok(Self {
            buf,
            pos: 0,
            mark: Mark::default(),
            newlines: 0,
            comments: Vec::new(),
            comments_head: 0,
            head_comment: Vec::new(),
            line_comment: Vec::new(),
            foot_comment: Vec::new(),
            stem_comment: None,
            stream_start_produced: false,
            stream_end_produced: false,
            flow_level: 0,
            tokens: Vec::new(),
            tokens_head: 0,
            tokens_parsed: 0,
            token_available: false,
            indent: 0,
            indents: Vec::new(),
            simple_key_allowed: false,
            simple_keys: Vec::new(),
            simple_keys_by_tok: HashMap::new(),
            state: State::StreamStart,
            states: Vec::new(),
            marks: Vec::new(),
            tag_directives: Vec::new(),
            doc: Doc::default(),
            anchors: HashMap::new(),
            event: None,
        })
    }
}