simplify Windows path logic; fix formatting

This commit is contained in:
Marcus Ball 2016-12-28 23:11:25 -05:00
parent edea408f62
commit 840da927d1
1 changed files with 18 additions and 22 deletions

View File

@ -191,25 +191,17 @@ fn split_path(path: &Path) -> (PathBuf, String, Option<String>) {
let rel_path = path.strip_prefix(&*TEMPLATE_DIR).unwrap().to_path_buf(); let rel_path = path.strip_prefix(&*TEMPLATE_DIR).unwrap().to_path_buf();
let path_no_ext = remove_extension(&rel_path); let path_no_ext = remove_extension(&rel_path);
let data_type = path_no_ext.extension(); let data_type = path_no_ext.extension();
let name = remove_extension(&path_no_ext); let name = remove_extension(&path_no_ext).to_string_lossy().into_owned();
(rel_path, // Ensure template name consistency on Windows systems
to_template_name(name.to_string_lossy().into_owned()), let name = match cfg!(windows) {
data_type.map(|d| d.to_string_lossy().into_owned())) true => name.replace("\\", "/"),
false => name,
};
(rel_path, name, data_type.map(|d| d.to_string_lossy().into_owned()))
} }
/// Converts a Windows path, like "foo\bar\file" to "foo/bar/file".
/// Used to standardize template path names for different operating systems.
#[cfg(windows)]
fn to_template_name(name: String) -> String {
name.replace("\\", "/")
}
/// Returns `name` unchanged, on non-Windows systems.
#[cfg(not(windows))]
fn to_template_name(name: String) -> String {
name
}
/// Returns a HashMap of `TemplateInfo`'s for all of the templates in /// Returns a HashMap of `TemplateInfo`'s for all of the templates in
/// `TEMPLATE_DIR`. Templates are all files that match one of the extensions for /// `TEMPLATE_DIR`. Templates are all files that match one of the extensions for
@ -227,11 +219,15 @@ fn discover_templates() -> HashMap<String, TemplateInfo> {
glob_path.set_extension(ext); glob_path.set_extension(ext);
for path in glob(glob_path.to_str().unwrap()).unwrap().filter_map(Result::ok) { for path in glob(glob_path.to_str().unwrap()).unwrap().filter_map(Result::ok) {
let (rel_path, name, data_type) = split_path(&path); let (rel_path, name, data_type) = split_path(&path);
templates.insert(name, TemplateInfo { templates.insert(name,
TemplateInfo {
full_path: path.to_path_buf(), full_path: path.to_path_buf(),
path: rel_path, path: rel_path,
extension: path.extension().unwrap().to_string_lossy().into_owned(), extension: path.extension()
data_type: data_type .unwrap()
.to_string_lossy()
.into_owned(),
data_type: data_type,
}); });
} }
} }