#![allow(rustdoc::bare_urls)]
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]
#[allow(clippy::needless_lifetimes)]
pub mod api;
pub mod common;
pub mod math;
pub mod utility;
pub struct ResourceManager {
resource_paths: std::collections::HashMap<String, Vec<std::path::PathBuf>>,
}
impl Default for ResourceManager {
fn default() -> Self {
Self::new()
}
}
impl ResourceManager {
pub fn new() -> ResourceManager {
let mut resource_paths = std::collections::HashMap::new();
maliput_sdk::sdk_resources()
.iter()
.for_each(|(backend_name, resource_path)| {
let files = std::fs::read_dir(resource_path).unwrap();
let mut paths = vec![];
files.filter(|file| file.is_ok()).for_each(|file| {
paths.push(file.unwrap().path());
});
resource_paths.insert(backend_name.clone(), paths);
});
ResourceManager { resource_paths }
}
pub fn get_resource_path_by_name(&self, backend_name: &str, resource_name: &str) -> Option<std::path::PathBuf> {
let paths = self.resource_paths.get(backend_name).unwrap();
for path in paths {
if path.to_str().unwrap().contains(resource_name) {
return Some(path.clone());
}
}
None
}
pub fn get_all_resources_by_backend(&self, backend_name: &str) -> Option<&Vec<std::path::PathBuf>> {
self.resource_paths.get(backend_name)
}
pub fn get_all_resources(&self) -> &std::collections::HashMap<String, Vec<std::path::PathBuf>> {
&self.resource_paths
}
}
mod tests {
#[test]
#[cfg(feature = "maliput_malidrive")]
fn test_maliput_malidrive_resources() {
let resource_manager = crate::ResourceManager::new();
let t_shape_road_xodr_path = resource_manager.get_resource_path_by_name("maliput_malidrive", "TShapeRoad.xodr");
assert!(t_shape_road_xodr_path.is_some());
assert!(t_shape_road_xodr_path.unwrap().exists());
let wrong_file_path = resource_manager.get_resource_path_by_name("maliput_malidrive", "wrong_file");
assert!(wrong_file_path.is_none());
let all_resources_in_malidrive = resource_manager.get_all_resources_by_backend("maliput_malidrive");
assert!(all_resources_in_malidrive.is_some());
assert!(all_resources_in_malidrive.unwrap().len() > 10);
let all_resources_in_wrong_backend = resource_manager.get_all_resources_by_backend("wrong_backend");
assert!(all_resources_in_wrong_backend.is_none());
let all_resources = resource_manager.get_all_resources();
assert!(all_resources.contains_key("maliput_malidrive"));
}
}