]> git.lizzy.rs Git - rust.git/blob - src/librustc_errors/registry.rs
Auto merge of #76035 - tiagolam:master, r=pietroalbini
[rust.git] / src / librustc_errors / registry.rs
1 use rustc_data_structures::fx::FxHashMap;
2
3 #[derive(Debug)]
4 pub struct InvalidErrorCode;
5
6 #[derive(Clone)]
7 pub struct Registry {
8     long_descriptions: FxHashMap<&'static str, Option<&'static str>>,
9 }
10
11 impl Registry {
12     pub fn new(long_descriptions: &[(&'static str, Option<&'static str>)]) -> Registry {
13         Registry { long_descriptions: long_descriptions.iter().copied().collect() }
14     }
15
16     /// This will panic if an invalid error code is passed in
17     pub fn find_description(&self, code: &str) -> Option<&'static str> {
18         self.long_descriptions[code]
19     }
20     /// Returns `InvalidErrorCode` if the code requested does not exist in the
21     /// registry. Otherwise, returns an `Option` where `None` means the error
22     /// code is valid but has no extended information.
23     pub fn try_find_description(
24         &self,
25         code: &str,
26     ) -> Result<Option<&'static str>, InvalidErrorCode> {
27         self.long_descriptions.get(code).copied().ok_or(InvalidErrorCode)
28     }
29 }