use crate::error::YamlError;
use crate::io::traits::ISource;
use crate::parser::ParseResult;
use crate::parser::directives::DirectiveContext;
use crate::parser::token_stream::TokenStream;
pub(crate) fn validate_top_level_comment_followed_by_indented_content(
source: &mut dyn ISource,
directives: &DirectiveContext,
indent_level: usize,
) -> ParseResult<()> {
let mut stream = TokenStream::new(source, directives, false)?;
while matches!(
stream.current(),
Some(crate::parser::lexer::Token::Comment(_))
) {
stream.next()?;
}
if let Some(crate::parser::lexer::Token::Newline) = stream.current() {
stream.next()?;
match stream.current().cloned() {
Some(crate::parser::lexer::Token::Indent(level)) if level > indent_level => {
stream.next()?;
stream.skip_newlines_and_comments()?;
match stream.current() {
Some(crate::parser::lexer::Token::Plain(_))
| Some(crate::parser::lexer::Token::SingleQuoted(_))
| Some(crate::parser::lexer::Token::DoubleQuoted(..))
| Some(crate::parser::lexer::Token::FlowMappingStart)
| Some(crate::parser::lexer::Token::FlowSequenceStart) => {
let msg = crate::parser::utils::helpers::parse_error_token(
&stream,
"Unexpected indented content after top-level comment.",
);
return Err(YamlError::from(msg));
}
_ => {}
}
}
_ => {}
}
}
stream.skip_trivia()?;
Ok(())
}