use super::budget;
use super::escape::{self, heredoc_line};
#[derive(Debug, Clone)]
pub(super) struct Pending {
pub(super) delimiter: String,
pub(super) strip_tabs: bool,
pub(super) joins: bool,
pub(super) start: usize,
}
thread_local! {
static PENDING: std::cell::RefCell<Vec<Pending>> = const { std::cell::RefCell::new(Vec::new()) };
static DECLARED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
pub(super) fn push(heredoc: Pending) {
DECLARED.with(|d| d.set(d.get() + 1));
PENDING.with(|q| q.borrow_mut().push(heredoc));
}
pub(super) fn pending() -> bool {
PENDING.with(|q| !q.borrow().is_empty())
}
pub(super) fn drained() -> bool {
PENDING.with(|q| q.borrow().iter().all(|h| h.start == 0))
}
pub(super) fn reset() {
PENDING.with(|q| q.borrow_mut().clear());
}
pub(super) fn skip_blank(input: &mut &str) -> winnow::ModalResult<()> {
loop {
while let Some(c @ (' ' | '\t' | '\n')) = input.chars().next() {
*input = &input[1..];
if c == '\n' && pending() {
drain(input);
}
}
if !escape::skip_continuation(input) {
break;
}
}
Ok(())
}
pub(super) fn expected_body_start(input: &str) -> Option<&str> {
let mut rest = &input[input.find('\n')? + 1..];
let priors: Vec<Pending> = PENDING.with(|q| q.borrow().clone());
for prior in &priors {
rest = split_body(rest, &prior.delimiter, prior.strip_tabs, prior.joins)?.1;
}
Some(rest)
}
pub(super) fn drain(input: &mut &str) {
let pending: Vec<Pending> = PENDING.with(|q| std::mem::take(&mut *q.borrow_mut()));
for h in pending {
if input.as_ptr() as usize != h.start {
escape::refuse();
}
let split = split_body(input, &h.delimiter, h.strip_tabs, h.joins);
let _ = budget::charge(0, split.map_or(input.len(), |(_, rest)| input.len() - rest.len()));
let Some((_, rest)) = split else {
if !input.trim().is_empty() {
escape::refuse();
}
return;
};
*input = rest;
}
}
pub(super) fn split_body<'a>(s: &'a str, delimiter: &str, strip_tabs: bool, joins: bool) -> Option<(&'a str, &'a str)> {
let bytes = s.as_bytes();
let mut line_start = 0;
while line_start <= bytes.len() {
let (line_end, line) = heredoc_line(s, line_start, joins);
let line = if strip_tabs { line.trim_start_matches('\t') } else { &line };
if line == delimiter {
let advance = line_end + usize::from(line_end < bytes.len());
return Some((&s[..line_start], &s[advance..]));
}
if line_end >= bytes.len() {
return None;
}
line_start = line_end + 1;
}
None
}
pub(super) fn own<O, E>(p: impl FnOnce() -> Result<O, E>) -> Result<O, E> {
let outer = PENDING.with(|q| std::mem::take(&mut *q.borrow_mut()));
let declared = DECLARED.with(std::cell::Cell::get);
let out = p();
if !drained() || (outer.iter().any(|h| h.start != 0) && DECLARED.with(std::cell::Cell::get) != declared) {
escape::refuse();
}
PENDING.with(|q| *q.borrow_mut() = outer);
out
}
#[cfg(test)]
#[path = "heredocs_tests.rs"]
mod tests;