use std::cell::RefCell;
#[allow(unused_imports)]
use std::ops::DerefMut;
use winnow::combinator::cut_err;
use winnow::combinator::delimited;
use winnow::combinator::peek;
use winnow::token::take;
use crate::parser::key::key;
use crate::parser::prelude::*;
use crate::parser::state::ParseState;
use crate::parser::trivia::line_trailing;
pub(crate) const STD_TABLE_OPEN: u8 = b'[';
const STD_TABLE_CLOSE: u8 = b']';
const ARRAY_TABLE_OPEN: &[u8] = b"[[";
const ARRAY_TABLE_CLOSE: &[u8] = b"]]";
pub(crate) fn std_table<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + 's {
move |i: &mut Input<'i>| {
(
delimited(
STD_TABLE_OPEN,
cut_err(key),
cut_err(STD_TABLE_CLOSE)
.context(StrContext::Expected(StrContextValue::CharLiteral('.')))
.context(StrContext::Expected(StrContextValue::StringLiteral("]"))),
)
.with_span(),
cut_err(line_trailing)
.context(StrContext::Expected(StrContextValue::CharLiteral('\n')))
.context(StrContext::Expected(StrContextValue::CharLiteral('#'))),
)
.try_map(|((h, span), t)| state.borrow_mut().deref_mut().on_std_header(h, t, span))
.parse_next(i)
}
}
pub(crate) fn array_table<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + 's {
move |i: &mut Input<'i>| {
(
delimited(
ARRAY_TABLE_OPEN,
cut_err(key),
cut_err(ARRAY_TABLE_CLOSE)
.context(StrContext::Expected(StrContextValue::CharLiteral('.')))
.context(StrContext::Expected(StrContextValue::StringLiteral("]]"))),
)
.with_span(),
cut_err(line_trailing)
.context(StrContext::Expected(StrContextValue::CharLiteral('\n')))
.context(StrContext::Expected(StrContextValue::CharLiteral('#'))),
)
.try_map(|((h, span), t)| state.borrow_mut().deref_mut().on_array_header(h, t, span))
.parse_next(i)
}
}
pub(crate) fn table<'s, 'i>(
state: &'s RefCell<ParseState>,
) -> impl Parser<Input<'i>, (), ContextError> + 's {
move |i: &mut Input<'i>| {
dispatch!(peek::<_, &[u8],_,_>(take(2usize));
b"[[" => array_table(state),
_ => std_table(state),
)
.context(StrContext::Label("table header"))
.parse_next(i)
}
}