microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
compiler/qsc/src/error.rs
69lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use miette::Diagnostic; |
| 5 | use std::{ |
| 6 | error::Error, |
| 7 | fmt::{self, Debug, Display, Formatter}, |
| 8 | }; |
| 9 | use thiserror::Error; |
| 10 | |
| 11 | #[derive(Clone, Debug, Error)] |
| 12 | pub struct WithStack<E> |
| 13 | where |
| 14 | E: Diagnostic + Error, |
| 15 | { |
| 16 | error: E, |
| 17 | stack_trace: Option<String>, |
| 18 | } |
| 19 | |
| 20 | impl<E: Diagnostic> WithStack<E> { |
| 21 | pub(super) fn new(error: E, stack_trace: Option<String>) -> Self { |
| 22 | WithStack { error, stack_trace } |
| 23 | } |
| 24 | |
| 25 | pub(super) fn stack_trace(&self) -> &Option<String> { |
| 26 | &self.stack_trace |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl<E: Display + Diagnostic> Display for WithStack<E> { |
| 31 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 32 | std::fmt::Display::fmt(&self.error, f) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // #[diagnostic(transparent)] does not seem to work with generics |
| 37 | impl<E: Diagnostic> Diagnostic for WithStack<E> { |
| 38 | fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> { |
| 39 | self.error.code() |
| 40 | } |
| 41 | |
| 42 | fn severity(&self) -> Option<miette::Severity> { |
| 43 | self.error.severity() |
| 44 | } |
| 45 | |
| 46 | fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> { |
| 47 | self.error.help() |
| 48 | } |
| 49 | |
| 50 | fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> { |
| 51 | self.error.url() |
| 52 | } |
| 53 | |
| 54 | fn source_code(&self) -> Option<&dyn miette::SourceCode> { |
| 55 | self.error.source_code() |
| 56 | } |
| 57 | |
| 58 | fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> { |
| 59 | self.error.labels() |
| 60 | } |
| 61 | |
| 62 | fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> { |
| 63 | self.error.related() |
| 64 | } |
| 65 | |
| 66 | fn diagnostic_source(&self) -> Option<&dyn Diagnostic> { |
| 67 | self.error.diagnostic_source() |
| 68 | } |
| 69 | } |