1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
use error::*; use std::path::Path; use std::path::PathBuf; use string; #[derive(Clone, Debug)] pub struct PathResolver { module_names: Vec<String>, module_paths: Vec<PathBuf>, search_paths: Vec<PathBuf>, } impl PathResolver { pub fn new(root_path: PathBuf, exe_path: Option<PathBuf>, search_paths: Option<Vec<PathBuf>>) -> Result<Self> { let root_path_vec = vec![root_path.clone()]; let search_paths = search_paths.unwrap_or(Vec::new()); let result_paths = root_path_vec.into_iter().chain(exe_path).chain(search_paths).collect(); for result_path in &result_paths { try!(Self::verify_search_path(result_path)); } let result = PathResolver{ module_names: Vec::new(), module_paths: Vec::new(), search_paths: result_paths, }; Ok(result) } pub fn add_module_path(&mut self, path: PathBuf, module_id: usize, module_name: String) -> Result<()> { try!(Self::verify_search_path(&path)); if self.module_paths.len() == module_id && self.module_names.len() == module_id { self.module_paths.push(path); self.module_names.push(module_name); Ok(()) } else { Err( ErrorKind::PluginPathIdIncorrect( path, module_id, self.module_paths.len() ).into() ) } } pub fn create_file_path_from_temp_directory_path<TPath>( mut directory_path: PathBuf, file_path: TPath ) -> Result<PathBuf> where TPath: AsRef<Path> { try!(Self::verify_search_path(&directory_path)); directory_path.push(file_path); try!(Self::verify_file_path(&directory_path)); Ok(directory_path) } pub fn find_path<TPath>(&self, path: TPath) -> Result<PathBuf> where TPath: AsRef<Path> { let path = path.as_ref(); if path.is_absolute() { Self::find_absolute_path(path) } else { self.find_relative_path(path) } } pub fn find_module_path(&self, module_id: usize) -> Result<&Path> { match self.module_paths.get(module_id) { Some(some) => Ok(some), None => Err(ErrorKind::ModuleFolderNotFound(module_id).into()), } } pub fn try_find_module_path(&self, module_id: usize) -> Option<&PathBuf> { self.module_paths.get(module_id) } pub fn find_module_file_path<TPath>(&self, path: TPath, module_id: usize) -> Result<PathBuf> where TPath: AsRef<Path> { let path = path.as_ref(); let module_path = self.find_module_path(module_id).chain_err(|| ErrorKind::PluginPathNotFound(path.to_path_buf(), module_id)); let mut module_path = try!(module_path).to_path_buf(); module_path.push(path); if module_path.exists() { Ok(module_path) } else { let file = path.to_path_buf(); Err(ErrorKind::FileNotFoundInPlugin(file, module_path).into()) } } pub fn try_find_module_file_path<TPath>(&self, path: TPath, module_id: usize) -> Result<Option<PathBuf>> where TPath: AsRef<Path> { let path = path.as_ref(); let module_path = self.find_module_path(module_id).chain_err(|| ErrorKind::PluginPathNotFound(path.to_path_buf(), module_id)); let mut module_path = try!(module_path).to_path_buf(); module_path.push(path); if module_path.exists() { Ok(Some(module_path)) } else { Ok(None) } } pub fn has_submodule(&self, module_id: usize, submodule_id: usize) -> Result<bool> { let mut module_path = try!(self.find_module_path(module_id)).to_path_buf(); if let Some(some) = self.module_names.get(submodule_id) { module_path.push(string::submodule_path()); module_path.push(some); Ok(module_path.exists()) } else { Err(ErrorKind::SubmoduleNotExist(module_id, submodule_id).into()) } } pub fn find_submodule_path(&self, module_id: usize, submodule_id: usize) -> Result<PathBuf> { let mut module_path = try!(self.find_module_path(module_id)).to_path_buf(); if let Some(some) = self.module_names.get(submodule_id) { module_path.push(string::submodule_path()); module_path.push(some); Ok(module_path) } else { Err(ErrorKind::SubmoduleNotExist(module_id, submodule_id).into()) } } pub fn find_submodule_file_path<TPath>(&self, path: TPath, module_id: usize, submodule_id: usize) -> Result<PathBuf> where TPath: AsRef<Path> { let mut submodule_path = try!(self.find_submodule_path(module_id, submodule_id)); submodule_path.push(path); Ok(submodule_path) } pub fn try_find_submodule_file_path<TPath>(&self, path: TPath, module_id: usize, submodule_id: usize) -> Result<Option<PathBuf>> where TPath: AsRef<Path> { let mut submodule_path = try!(self.find_submodule_path(module_id, submodule_id)); submodule_path.push(path); if submodule_path.exists() { Ok(Some(submodule_path)) } else { Ok(None) } } fn find_absolute_path<TPath>(path: TPath) -> Result<PathBuf> where TPath: AsRef<Path> { let path = path.as_ref(); if path.exists() { let result = path.to_path_buf(); Ok(result) } else { Err(ErrorKind::AbsolutePathNotFound(path.to_path_buf()).into()) } } fn find_relative_path<TPath>(&self, path: TPath) -> Result<PathBuf> where TPath: AsRef<Path> { let path = path.as_ref(); let self_search_paths = &self.search_paths; for search_path in self_search_paths { let mut current_path = search_path.to_path_buf(); current_path.push(path); if current_path.exists() { return Ok(current_path); } } let search_paths = self_search_paths .into_iter() .map( |search_path| { let mut search_path_buf = search_path.to_path_buf(); search_path_buf.push(path); search_path_buf } ).collect(); Err(ErrorKind::RelativePathNotFound(path.to_path_buf(), search_paths).into()) } fn verify_file_path(file_path: &PathBuf) -> Result<()> { if !file_path.is_absolute() { Err(ErrorKind::PathVerificationNotAbsolute(file_path.clone()).into()) } else if !file_path.exists() { Err(ErrorKind::PathVerificationNotExist(file_path.clone()).into()) } else if !file_path.is_file() { Err(ErrorKind::PathVerificationNotFile(file_path.clone()).into()) } else { Ok(()) } } fn verify_search_path(search_path: &PathBuf) -> Result<()> { if !search_path.is_absolute() { Err(ErrorKind::PathVerificationNotAbsolute(search_path.clone()).into()) } else if !search_path.exists() { Err(ErrorKind::PathVerificationNotExist(search_path.clone()).into()) } else if !search_path.is_dir() { Err(ErrorKind::PathVerificationNotDirectory(search_path.clone()).into()) } else { Ok(()) } } }