Skip to main content

css_parse/
parser_return.rs

1use crate::{Cursor, CursorInterleaveSink, CursorSink, Diagnostic, ToCursors, Vec};
2
3#[derive(Debug)]
4pub struct ParserReturn<'a, T>
5where
6	T: ToCursors,
7{
8	pub output: Option<T>,
9	pub source_text: &'a str,
10	pub errors: Vec<'a, Diagnostic>,
11	pub trivia: Vec<'a, (Vec<'a, Cursor>, Cursor)>,
12	with_trivia: bool,
13}
14
15impl<'a, T: ToCursors> ParserReturn<'a, T> {
16	pub fn new(
17		output: Option<T>,
18		source_text: &'a str,
19		errors: Vec<'a, Diagnostic>,
20		trivia: Vec<'a, (Vec<'a, Cursor>, Cursor)>,
21	) -> Self {
22		Self { output, source_text, errors, trivia, with_trivia: false }
23	}
24
25	pub fn with_trivia(mut self) -> Self {
26		self.with_trivia = true;
27		self
28	}
29}
30
31impl<T: ToCursors> ToCursors for ParserReturn<'_, T> {
32	fn to_cursors(&self, s: &mut impl CursorSink) {
33		if let Some(output) = &self.output {
34			let eof_offset = css_lexer::SourceOffset(self.source_text.len() as u32);
35			let eof_cursor = crate::Cursor::new(eof_offset, css_lexer::Token::EOF);
36
37			if self.with_trivia {
38				let mut sink = CursorInterleaveSink::new(s, &self.trivia);
39				ToCursors::to_cursors(output, &mut sink);
40				sink.append(eof_cursor);
41			} else {
42				ToCursors::to_cursors(output, s);
43				s.append(eof_cursor);
44			}
45		}
46	}
47}