maliput/lib.rs
1// BSD 3-Clause License
2//
3// Copyright (c) 2024, Woven by Toyota.
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8//
9// * Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11//
12// * Redistributions in binary form must reproduce the above copyright notice,
13// this list of conditions and the following disclaimer in the documentation
14// and/or other materials provided with the distribution.
15//
16// * Neither the name of the copyright holder nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30#![allow(rustdoc::bare_urls)]
31#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]
32
33#[allow(clippy::needless_lifetimes)]
34// Remove after rust 1.87 is used. https://github.com/rust-lang/rust-clippy/issues/14441
35pub mod api;
36pub mod common;
37pub mod math;
38pub mod utility;
39
40/// ### ResourceManager
41///
42/// Convenient method for getting the path to the resources of the backends.
43///
44/// ### Backends
45/// - maliput_malidrive: Resources from maliput_malidrive are brought by maliput-sdk package.
46/// All the maliput_malidrive resources are stored in the same directory:
47/// (See https://github.com/maliput/maliput_malidrive/tree/main/resources)
48/// ### Example
49///
50/// How to get all the resources from the maliput_malidrive backend:
51///
52/// ```rust, no_run
53/// let resource_manager = maliput::ResourceManager::new();
54/// let all_resources_in_malidrive = resource_manager.get_all_resources_by_backend("maliput_malidrive");
55/// ```
56///
57/// How to get the path to the TShapeRoad.xodr file from the maliput_malidrive backend:
58/// ```rust, no_run
59/// let resource_manager = maliput::ResourceManager::new();
60/// let t_shape_road_xodr_path = resource_manager.get_resource_path_by_name("maliput_malidrive", "TShapeRoad.xodr");
61/// assert!(t_shape_road_xodr_path.is_some());
62/// assert!(t_shape_road_xodr_path.unwrap().exists());
63/// ```
64pub struct ResourceManager {
65 resource_paths: std::collections::HashMap<String, Vec<std::path::PathBuf>>,
66}
67
68impl Default for ResourceManager {
69 fn default() -> Self {
70 Self::new()
71 }
72}
73
74impl ResourceManager {
75 /// Creates a new ResourceManager.
76 pub fn new() -> ResourceManager {
77 let mut resource_paths = std::collections::HashMap::new();
78 maliput_sdk::sdk_resources()
79 .iter()
80 .for_each(|(backend_name, resource_path)| {
81 // All the files in the path are added to the resource manager
82 let files = std::fs::read_dir(resource_path).unwrap();
83 let mut paths = vec![];
84 files.filter(|file| file.is_ok()).for_each(|file| {
85 paths.push(file.unwrap().path());
86 });
87 resource_paths.insert(backend_name.clone(), paths);
88 });
89 ResourceManager { resource_paths }
90 }
91
92 /// Obtains the path to a resource by its name.
93 /// ### Arguments
94 ///
95 /// * `backend_name` - The name of the backend.
96 /// * `resource_name` - The name of the resource.
97 ///
98 /// ### Returns
99 /// The path to the resource if it exists, otherwise None.
100 pub fn get_resource_path_by_name(&self, backend_name: &str, resource_name: &str) -> Option<std::path::PathBuf> {
101 let paths = self.resource_paths.get(backend_name).unwrap();
102 for path in paths {
103 if path.to_str().unwrap().contains(resource_name) {
104 return Some(path.clone());
105 }
106 }
107 None
108 }
109
110 /// Obtains all the resources from a backend.
111 /// ### Arguments
112 ///
113 /// * `backend_name` - The name of the backend.
114 ///
115 /// ### Returns
116 /// A vector with all the resources from the backend if it exists, otherwise None.
117 pub fn get_all_resources_by_backend(&self, backend_name: &str) -> Option<&Vec<std::path::PathBuf>> {
118 self.resource_paths.get(backend_name)
119 }
120
121 /// Get the underlying collection that stores all the resources for each backend.
122 pub fn get_all_resources(&self) -> &std::collections::HashMap<String, Vec<std::path::PathBuf>> {
123 &self.resource_paths
124 }
125}
126
127// test
128
129mod tests {
130
131 #[test]
132 fn test_maliput_malidrive_resources() {
133 let resource_manager = crate::ResourceManager::new();
134
135 let t_shape_road_xodr_path = resource_manager.get_resource_path_by_name("maliput_malidrive", "TShapeRoad.xodr");
136 assert!(t_shape_road_xodr_path.is_some());
137 assert!(t_shape_road_xodr_path.unwrap().exists());
138
139 let wrong_file_path = resource_manager.get_resource_path_by_name("maliput_malidrive", "wrong_file");
140 assert!(wrong_file_path.is_none());
141
142 let all_resources_in_malidrive = resource_manager.get_all_resources_by_backend("maliput_malidrive");
143 assert!(all_resources_in_malidrive.is_some());
144 assert!(all_resources_in_malidrive.unwrap().len() > 10);
145
146 let all_resources_in_wrong_backend = resource_manager.get_all_resources_by_backend("wrong_backend");
147 assert!(all_resources_in_wrong_backend.is_none());
148
149 let all_resources = resource_manager.get_all_resources();
150 // There is only one backend supported at the moment.
151 assert_eq!(all_resources.len(), 1);
152 }
153}