Skip to main content

rustc_errors/
registry.rs

1use rustc_data_structures::fx::FxHashMap;
2
3use crate::ErrCode;
4
5#[derive(#[automatically_derived]
impl ::core::fmt::Debug for InvalidErrorCode {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "InvalidErrorCode")
    }
}Debug)]
6pub struct InvalidErrorCode;
7
8#[derive(#[automatically_derived]
impl ::core::clone::Clone for Registry {
    #[inline]
    fn clone(&self) -> Registry {
        Registry {
            long_descriptions: ::core::clone::Clone::clone(&self.long_descriptions),
        }
    }
}Clone)]
9pub struct Registry {
10    long_descriptions: FxHashMap<ErrCode, &'static str>,
11}
12
13impl Registry {
14    pub fn new(long_descriptions: &[(ErrCode, &'static str)]) -> Registry {
15        Registry { long_descriptions: long_descriptions.iter().copied().collect() }
16    }
17
18    /// Returns `InvalidErrorCode` if the code requested does not exist in the
19    /// registry.
20    pub fn try_find_description(&self, code: ErrCode) -> Result<&'static str, InvalidErrorCode> {
21        self.long_descriptions.get(&code).copied().ok_or(InvalidErrorCode)
22    }
23}