quil_rs/program/error/
syntax.rs

1// Copyright 2022 Rigetti Computing
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::{error::Error, fmt};
16
17use crate::parser::{LexError, ParseError};
18
19use super::LeftoverError;
20
21/// Failed to deserialize a Quil program for some reason.
22///
23/// # Display
24///
25/// The standard [`Display`](fmt::Display) implementation shows only the initial error.
26/// Use the alternative syntax (e.g., `format!("{:#}", err)` instead of `format!("{}", err)`) to
27/// see a backtrace of errors that caused this one.
28///
29/// See also [`Error`](crate::parser::Error).
30#[derive(Clone, Debug, PartialEq)]
31pub enum SyntaxError<T> {
32    LexError(LexError),
33    ParseError(ParseError),
34    Leftover(LeftoverError<T>),
35}
36
37impl<T> SyntaxError<T> {
38    pub fn map_parsed<T2>(self, map: impl Fn(T) -> T2) -> SyntaxError<T2> {
39        match self {
40            Self::LexError(err) => SyntaxError::LexError(err),
41            Self::ParseError(err) => SyntaxError::ParseError(err),
42            Self::Leftover(err) => SyntaxError::Leftover(err.map_parsed(map)),
43        }
44    }
45
46    pub fn recover(self) -> Result<T, Self> {
47        match self {
48            Self::Leftover(err) => Ok(err.recover()),
49            err => Err(err),
50        }
51    }
52}
53
54impl<T: fmt::Debug + 'static> fmt::Display for SyntaxError<T> {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        if f.alternate() {
57            write!(f, "error while parsing: {:#}", self.source().unwrap())
58        } else {
59            write!(f, "error while parsing: {}", self.source().unwrap())
60        }
61    }
62}
63
64impl<T> From<LexError> for SyntaxError<T> {
65    fn from(err: LexError) -> Self {
66        Self::LexError(err)
67    }
68}
69
70impl<T> From<ParseError> for SyntaxError<T> {
71    fn from(err: ParseError) -> Self {
72        Self::ParseError(err)
73    }
74}
75
76impl<T> From<LeftoverError<T>> for SyntaxError<T> {
77    fn from(err: LeftoverError<T>) -> Self {
78        Self::Leftover(err)
79    }
80}
81
82impl<T: fmt::Debug + 'static> Error for SyntaxError<T> {
83    fn source(&self) -> Option<&(dyn Error + 'static)> {
84        match self {
85            Self::LexError(err) => Some(err),
86            Self::ParseError(err) => Some(err),
87            Self::Leftover(err) => Some(err),
88        }
89    }
90}