2018-08-18 13:55:43 +03:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2014-07-01 18:39:41 +02:00
|
|
|
|
2020-02-28 15:32:09 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct InvalidErrorCode;
|
|
|
|
|
2015-01-30 21:44:27 +13:00
|
|
|
#[derive(Clone)]
|
2014-07-01 18:39:41 +02:00
|
|
|
pub struct Registry {
|
2020-02-28 15:32:09 -07:00
|
|
|
long_descriptions: FxHashMap<&'static str, Option<&'static str>>,
|
2014-07-01 18:39:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Registry {
|
2020-02-28 15:32:09 -07:00
|
|
|
pub fn new(long_descriptions: &[(&'static str, Option<&'static str>)]) -> Registry {
|
2020-05-04 11:52:15 -07:00
|
|
|
Registry { long_descriptions: long_descriptions.iter().copied().collect() }
|
2014-07-01 18:39:41 +02:00
|
|
|
}
|
|
|
|
|
2020-02-28 15:32:09 -07: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 11:52:15 -07:00
|
|
|
self.long_descriptions.get(code).copied().ok_or(InvalidErrorCode)
|
2014-07-01 18:39:41 +02:00
|
|
|
}
|
|
|
|
}
|