css_parse/
parser_return.rs

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