microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2b583dda267f5bb3df16fbf6251d9bbb86b42b49

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

compiler/qsc/src/error.rs

94lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use miette::{Diagnostic, SourceCode};
5use qsc_frontend::compile::{Source, SourceMap};
6use std::{
7 error::Error,
8 fmt::{self, Debug, Display, Formatter},
9};
10
11#[derive(Clone, Debug)]
12pub(super) struct WithSource<S, E> {
13 source: Option<S>,
14 error: E,
15 stack_trace: Option<String>,
16}
17
18impl<S, E> WithSource<S, E> {
19 pub(super) fn new(source: S, error: E, stack_trace: Option<String>) -> Self {
20 WithSource {
21 source: Some(source),
22 error,
23 stack_trace,
24 }
25 }
26
27 pub(super) fn error(&self) -> &E {
28 &self.error
29 }
30
31 pub(super) fn stack_trace(&self) -> &Option<String> {
32 &self.stack_trace
33 }
34}
35
36impl<E: Diagnostic> WithSource<Source, E> {
37 pub fn from_map(sources: &SourceMap, error: E, stack_trace: Option<String>) -> Self {
38 let source = sources.find_by_diagnostic(&error).cloned();
39 Self {
40 source,
41 error,
42 stack_trace,
43 }
44 }
45}
46
47impl<S: Debug, E: Error> Error for WithSource<S, E> {
48 fn source(&self) -> Option<&(dyn Error + 'static)> {
49 self.error.source()
50 }
51}
52
53impl<S: SourceCode + Debug, E: Diagnostic> Diagnostic for WithSource<S, E> {
54 fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
55 self.error.code()
56 }
57
58 fn severity(&self) -> Option<miette::Severity> {
59 self.error.severity()
60 }
61
62 fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
63 self.error.help()
64 }
65
66 fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
67 self.error.url()
68 }
69
70 fn source_code(&self) -> Option<&dyn miette::SourceCode> {
71 match &self.source {
72 None => self.error.source_code(),
73 Some(source) => Some(source),
74 }
75 }
76
77 fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
78 self.error.labels()
79 }
80
81 fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
82 self.error.related()
83 }
84
85 fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
86 self.error.diagnostic_source()
87 }
88}
89
90impl<S, E: Display> Display for WithSource<S, E> {
91 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
92 self.error.fmt(f)
93 }
94}
95