use super::Parser;
use super::model::{Comment, CommentKind, HotComment};
use crate::location::Location;
#[derive(Debug, Default)]
pub(super) struct CommentState {
hotcomments: Vec<HotComment>,
comment_locations: Vec<Comment>,
hotcomment_header: bool,
}
impl CommentState {
pub(super) fn new() -> Self {
Self {
hotcomment_header: true,
..Self::default()
}
}
pub(super) fn into_parts(self) -> (Vec<HotComment>, Vec<Comment>) {
(self.hotcomments, self.comment_locations)
}
pub(in crate::parser) fn capture_comment(
&mut self,
capture_comments: bool,
kind: CommentKind,
location: Location,
) {
if capture_comments {
self.comment_locations.push(Comment { kind, location });
}
}
pub(in crate::parser) fn capture_hotcomment(&mut self, text: &[u8], location: Location) {
let Some(content) = text.strip_prefix(b"!") else {
return;
};
let content = content
.strip_suffix(b"\r\n")
.or_else(|| content.strip_suffix(b"\n"))
.or_else(|| content.strip_suffix(b"\r"))
.unwrap_or(content)
.trim_ascii_end();
self.hotcomments.push(HotComment {
header: self.hotcomment_header,
location,
content: content.to_vec(),
});
}
pub(in crate::parser) fn finish_header(&mut self) {
self.hotcomment_header = false;
}
}
impl Parser<'_, '_, '_, '_> {
pub(in crate::parser) fn capture_comment(&mut self, kind: CommentKind, location: Location) {
self.comments
.capture_comment(self.options.capture_comments(), kind, location);
}
pub(in crate::parser) fn capture_hotcomment(&mut self, text: &[u8], location: Location) {
self.comments.capture_hotcomment(text, location);
}
pub(in crate::parser) fn finish_hotcomment_header(&mut self) {
self.comments.finish_header();
}
}