-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathdiagnostics.rs
More file actions
72 lines (64 loc) · 1.98 KB
/
diagnostics.rs
File metadata and controls
72 lines (64 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use pglt_diagnostics::adapters::{IoError, StdError};
use pglt_diagnostics::{Category, Diagnostic, DiagnosticExt, DiagnosticTags, Error};
use std::io;
#[derive(Debug, Diagnostic)]
#[diagnostic(category = "internalError/panic", tags(INTERNAL))]
pub(crate) struct PanicDiagnostic {
#[description]
#[message]
pub(crate) message: String,
}
/// Extension trait for turning [Display]-able error types into [TraversalError]
pub(crate) trait ResultExt {
type Result;
fn with_file_path_and_code(
self,
file_path: String,
code: &'static Category,
) -> Result<Self::Result, Error>;
fn with_file_path_and_code_and_tags(
self,
file_path: String,
code: &'static Category,
tags: DiagnosticTags,
) -> Result<Self::Result, Error>;
}
impl<T, E> ResultExt for Result<T, E>
where
E: std::error::Error + Send + Sync + 'static,
{
type Result = T;
fn with_file_path_and_code_and_tags(
self,
file_path: String,
code: &'static Category,
diagnostic_tags: DiagnosticTags,
) -> Result<Self::Result, Error> {
self.map_err(move |err| {
StdError::from(err)
.with_category(code)
.with_file_path(file_path)
.with_tags(diagnostic_tags)
})
}
fn with_file_path_and_code(
self,
file_path: String,
code: &'static Category,
) -> Result<Self::Result, Error> {
self.map_err(move |err| {
StdError::from(err)
.with_category(code)
.with_file_path(file_path)
})
}
}
/// Extension trait for turning [io::Error] into [Error]
pub(crate) trait ResultIoExt: ResultExt {
fn with_file_path(self, file_path: String) -> Result<Self::Result, Error>;
}
impl<T> ResultIoExt for io::Result<T> {
fn with_file_path(self, file_path: String) -> Result<Self::Result, Error> {
self.map_err(|error| IoError::from(error).with_file_path(file_path))
}
}