forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9d8f4f8Ivan Molodetskikh1 month ago2997 commits

error.rs102 lines, 2.8 KiBHistoryRawDownload
1use std::error::Error;
2use std::fmt;
3use std::path::PathBuf;
4
5use miette::Diagnostic;
6
7#[derive(Debug)]
8pub struct ConfigParseResult<T, E> {
9 pub config: Result<T, E>,
10
11 // We always try to return includes for the file watcher.
12 //
13 // If the main config is valid, but an included file fails to parse, config will be an Err(),
14 // but includes will still be filled, so that fixing just the included file is enough to
15 // trigger a reload.
16 //
17 // FIXME: This is currently used to track all the files that needed to be watched. The name
18 // `includes` is not really correct and should be renamed in the future.
19 pub includes: Vec<PathBuf>,
20}
21
22/// Error type that chains main errors with include errors.
23///
24/// Allows miette's Report formatting to have main + include errors all in one.
25#[derive(Debug)]
26pub struct ConfigIncludeError {
27 pub main: knuffel::Error,
28 pub includes: Vec<knuffel::Error>,
29}
30
31impl<T, E> ConfigParseResult<T, E> {
32 pub fn from_err(err: E) -> Self {
33 Self {
34 config: Err(err),
35 includes: Vec::new(),
36 }
37 }
38
39 pub fn map_config_res<U, V>(
40 self,
41 f: impl FnOnce(Result<T, E>) -> Result<U, V>,
42 ) -> ConfigParseResult<U, V> {
43 ConfigParseResult {
44 config: f(self.config),
45 includes: self.includes,
46 }
47 }
48}
49
50impl fmt::Display for ConfigIncludeError {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 fmt::Display::fmt(&self.main, f)
53 }
54}
55
56impl Error for ConfigIncludeError {
57 fn source(&self) -> Option<&(dyn Error + 'static)> {
58 self.main.source()
59 }
60}
61
62impl Diagnostic for ConfigIncludeError {
63 fn code<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
64 self.main.code()
65 }
66
67 fn severity(&self) -> Option<miette::Severity> {
68 self.main.severity()
69 }
70
71 fn help<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
72 self.main.help()
73 }
74
75 fn url<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
76 self.main.url()
77 }
78
79 fn source_code(&self) -> Option<&dyn miette::SourceCode> {
80 self.main.source_code()
81 }
82
83 fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
84 self.main.labels()
85 }
86
87 fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
88 self.main.diagnostic_source()
89 }
90
91 fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
92 let main_related = self.main.related();
93 let includes_iter = self.includes.iter().map(|err| err as &'a dyn Diagnostic);
94
95 let iter: Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a> = match main_related {
96 Some(main) => Box::new(main.chain(includes_iter)),
97 None => Box::new(includes_iter),
98 };
99
100 Some(iter)
101 }
102}