use std::collections::HashMap;
use once_cell::sync::Lazy;
use crate::{
commands::CommandName,
styles::{ComputeStyle, FixStyle},
};
pub(crate) static DOCS_MAP: Lazy<DocsMap> =
Lazy::new(|| DocsMap::new_from_str(include_str!["../../docs_extract/index_map.txt"]));
pub struct DocsMap {
fixes: HashMap<FixStyle, String>,
computes: HashMap<ComputeStyle, String>,
commands: HashMap<CommandName, String>,
}
impl DocsMap {
pub fn new_from_str(map_contents: &str) -> Self {
let mut fixes = HashMap::new();
let mut computes = HashMap::new();
let mut others = HashMap::new();
map_contents.lines().for_each(|line| {
let (name, doc_file) = line.split_once(',').expect("Expected key value pair");
if let Some((command_type, style)) = name.split_once(' ') {
match command_type {
"fix" => {
fixes.insert(style.into(), doc_file.to_owned());
}
"compute" => {
computes.insert(style.into(), doc_file.to_owned());
}
_ => {
others.insert(style.into(), doc_file.to_owned());
}
}
} else {
others.insert(name.into(), doc_file.to_owned());
}
});
DocsMap {
fixes,
computes,
commands: others,
}
}
pub fn fixes(&self) -> &HashMap<FixStyle, String> {
&self.fixes
}
pub fn computes(&self) -> &HashMap<ComputeStyle, String> {
&self.computes
}
pub fn commands(&self) -> &HashMap<CommandName, String> {
&self.commands
}
}
#[cfg(test)]
mod tests {
use crate::{
commands::CommandName,
docs::docs_map::DOCS_MAP,
styles::{ComputeStyle, FixStyle},
};
use super::DocsMap;
#[test]
fn index_map() {
let file = include_str!("../../docs_extract/index_map.txt");
let map = DocsMap::new_from_str(file);
assert_eq!(map.fixes()[&FixStyle::Nve], "fix_nve".to_owned());
assert_eq!(map.fixes()[&FixStyle::Nvt], "fix_nh".to_owned());
assert_eq!(
map.computes()[&ComputeStyle::StressAtom],
"compute_stress_atom".to_owned()
);
assert_eq!(map.commands()[&CommandName::Newton], "newton".to_owned());
}
#[test]
fn index_map_static() {
let map = &DOCS_MAP;
assert_eq!(map.fixes()[&FixStyle::Nve], "fix_nve".to_owned());
assert_eq!(map.fixes()[&FixStyle::Nvt], "fix_nh".to_owned());
assert_eq!(
map.computes()[&ComputeStyle::StressAtom],
"compute_stress_atom".to_owned()
);
assert_eq!(map.commands()[&CommandName::Newton], "newton".to_owned());
}
}