helm-rs 0.1.2

Typed Rust API for Helm 4: swappable backends, native chart, values and release reading
Documentation
//! The token queue as the parser consumes it (`peek_token`, `skip_token` of `parserc.go`): the
//! comments the scanner collected before a token are moved into the parser's pending head, line
//! and foot comments when the token is reached.

use super::parser::Parser;
use super::scanner::Res;
use super::token::{Token, TokenType};

fn join_comment(target: &mut Vec<u8>, extra: &[u8]) {
    if extra.is_empty() {
        return;
    }

    if !target.is_empty() {
        target.push(b'\n');
    }

    target.extend_from_slice(extra);
}

impl Parser {
    /// `peek_token`: the next token, with the comments up to it moved into the parser's
    /// pending head, line and foot comments.
    pub(super) fn peek_token(&mut self) -> Res<Token> {
        if !self.token_available {
            self.fetch_more_tokens()?;
        }

        let token = self.tokens[self.tokens_head].clone();

        self.unfold_comments(&token);

        Ok(token)
    }

    pub(super) fn unfold_comments(&mut self, token: &Token) {
        while self.comments_head < self.comments.len()
            && token.start_mark.index >= self.comments[self.comments_head].token_mark.index
        {
            let comment = std::mem::take(&mut self.comments[self.comments_head]);

            if !comment.head.is_empty() {
                if token.typ == TokenType::BlockEnd {
                    self.comments[self.comments_head] = comment;

                    break;
                }

                join_comment(&mut self.head_comment, &comment.head);
            }

            join_comment(&mut self.foot_comment, &comment.foot);
            join_comment(&mut self.line_comment, &comment.line);
            self.comments_head += 1;
        }
    }

    pub(super) fn skip_token(&mut self) {
        self.token_available = false;
        self.tokens_parsed += 1;
        self.stream_end_produced = self.tokens[self.tokens_head].typ == TokenType::StreamEnd;
        self.tokens_head += 1;
    }
}