use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct CodesetInfo {
pub name: String,
pub also_known_as: Vec<String>,
pub mib_code: u32,
pub source: Option<String>,
pub references: Option<String>,
}
lazy_static! {
static ref CODESETS: HashMap<String, CodesetInfo> = load_code_sets_from_json();
}
pub fn lookup(name: &str) -> Option<&'static CodesetInfo> {
assert!(name.len() > 0, "codeset name may not be empty");
CODESETS.get(name)
}
pub fn all_names() -> Vec<String> {
CODESETS.keys().cloned().collect()
}
fn load_code_sets_from_json() -> HashMap<String, CodesetInfo> {
info!("load_code_sets_from_json - loading JSON");
let raw_data = include_bytes!("data/codesets.json");
let code_set_map: HashMap<String, CodesetInfo> = serde_json::from_slice(raw_data).unwrap();
info!(
"load_code_sets_from_json - loaded {} codes ets",
code_set_map.len()
);
code_set_map
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_good_codeset_code() {
match lookup("UTF-8") {
None => panic!("was expecting a codeset"),
Some(codeset) => assert_eq!(codeset.name.to_string(), "UTF-8".to_string()),
}
}
#[test]
fn test_bad_codeset_code() {
match lookup(&"UTF-99") {
None => (),
Some(_) => panic!("was expecting a None in response"),
}
}
}