datex_core/parser/
parser_result.rs1use crate::ast::expressions::DatexExpression;
2use crate::parser::errors::SpannedParserError;
3
4#[derive(Debug, Clone)]
5pub struct ValidDatexParseResult {
6 pub ast: DatexExpression,
7}
8
9#[derive(Debug, Clone)]
10pub struct InvalidDatexParseResult {
11 pub ast: DatexExpression,
12 pub errors: Vec<SpannedParserError>,
13}
14
15#[derive(Debug, Clone)]
16pub enum ParserResult {
17 Valid(ValidDatexParseResult),
18 Invalid(InvalidDatexParseResult),
19}
20
21impl ParserResult {
22 pub fn is_valid(&self) -> bool {
23 core::matches!(self, ParserResult::Valid { .. })
24 }
25 pub fn unwrap(self) -> ValidDatexParseResult {
26 match self {
27 ParserResult::Valid(result) => result,
28 ParserResult::Invalid(InvalidDatexParseResult {
29 errors, ..
30 }) => {
31 core::panic!("Parsing failed with errors: {:?}", errors)
32 }
33 }
34 }
35 pub fn errors(&self) -> Option<&Vec<SpannedParserError>> {
36 match self {
37 ParserResult::Valid { .. } => None,
38 ParserResult::Invalid(InvalidDatexParseResult {
39 errors, ..
40 }) => Some(errors),
41 }
42 }
43
44 pub fn ast(&self) -> &DatexExpression {
45 match self {
46 ParserResult::Valid(result) => &result.ast,
47 ParserResult::Invalid(InvalidDatexParseResult { ast, .. }) => ast,
48 }
49 }
50
51 pub fn into_ast_and_errors(
52 self,
53 ) -> (DatexExpression, Vec<SpannedParserError>) {
54 match self {
55 ParserResult::Valid(result) => (result.ast, vec![]),
56 ParserResult::Invalid(InvalidDatexParseResult { ast, errors }) => {
57 (ast, errors)
58 }
59 }
60 }
61
62 pub fn to_result(self) -> Result<DatexExpression, Vec<SpannedParserError>> {
63 match self {
64 ParserResult::Valid(result) => Ok(result.ast),
65 ParserResult::Invalid(InvalidDatexParseResult {
66 errors, ..
67 }) => Err(errors),
68 }
69 }
70}