2018-08-18 10:55:43 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2014-07-01 16:39:41 +00:00
|
|
|
|
2020-02-28 22:32:09 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct InvalidErrorCode;
|
|
|
|
|
2015-01-30 08:44:27 +00:00
|
|
|
#[derive(Clone)]
|
2014-07-01 16:39:41 +00:00
|
|
|
pub struct Registry {
|
2020-02-28 22:32:09 +00:00
|
|
|
long_descriptions: FxHashMap<&'static str, Option<&'static str>>,
|
2014-07-01 16:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Registry {
|
2020-02-28 22:32:09 +00:00
|
|
|
pub fn new(long_descriptions: &[(&'static str, Option<&'static str>)]) -> Registry {
|
2020-05-04 18:52:15 +00:00
|
|
|
Registry { long_descriptions: long_descriptions.iter().copied().collect() }
|
2014-07-01 16:39:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 22:32:09 +00:00
|
|
|
/// Returns `InvalidErrorCode` if the code requested does not exist in the
|
|
|
|
/// registry. Otherwise, returns an `Option` where `None` means the error
|
|
|
|
/// code is valid but has no extended information.
|
|
|
|
pub fn try_find_description(
|
|
|
|
&self,
|
|
|
|
code: &str,
|
|
|
|
) -> Result<Option<&'static str>, InvalidErrorCode> {
|
2020-05-04 18:52:15 +00:00
|
|
|
self.long_descriptions.get(code).copied().ok_or(InvalidErrorCode)
|
2014-07-01 16:39:41 +00:00
|
|
|
}
|
|
|
|
}
|