From f80060bbe977d502d34eb3585985252165b6ba93 Mon Sep 17 00:00:00 2001 From: Yousif Akbar <11247449+yhakbar@users.noreply.github.com> Date: Sat, 26 Aug 2023 20:27:16 -0400 Subject: [PATCH 1/6] Add hidden flag to control when hidden files are ignored --- src/registry.rs | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/registry.rs b/src/registry.rs index 02301e6fe..76177312b 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -279,9 +279,10 @@ impl<'reg> Registry<'reg> { /// * `tpl_extension`: the template file extension /// * `dir_path`: the path of directory /// - /// Hidden files and tempfile (starts with `#`) will be ignored. All registered - /// will use their relative name as template name. For example, when `dir_path` is - /// `templates/` and `tpl_extension` is `.hbs`, the file + /// Hidden files and tempfile (starts with `#`) will be ignored by default. + /// Set `hidden` to `true` to avoid ignoring hidden files. + /// All registered will use their relative name as template name. + /// For example, when `dir_path` is `templates/` and `tpl_extension` is `.hbs`, the file /// `templates/some/path/file.hbs` will be registered as `some/path/file`. /// /// This method is not available by default. @@ -295,6 +296,7 @@ impl<'reg> Registry<'reg> { &mut self, tpl_extension: &str, dir_path: P, + hidden: bool, ) -> Result<(), TemplateError> where P: AsRef, @@ -313,7 +315,7 @@ impl<'reg> Registry<'reg> { tpl_path .file_stem() .map(|stem| stem.to_string_lossy()) - .map(|stem| !(stem.starts_with('.') || stem.starts_with('#'))) + .map(|stem| !((!hidden && stem.starts_with('.')) || stem.starts_with('#'))) .unwrap_or(false) }) .filter_map(|tpl_path| { @@ -848,7 +850,7 @@ mod test { let mut file4: File = File::create(&file4_path).unwrap(); writeln!(file4, "

Hallo {{world}}!

").unwrap(); - r.register_templates_directory(".hbs", dir.path()).unwrap(); + r.register_templates_directory(".hbs", dir.path(), false).unwrap(); assert_eq!(r.templates.len(), 3); assert_eq!(r.templates.contains_key("t1"), true); @@ -878,7 +880,7 @@ mod test { let mut file3: File = File::create(&file3_path).unwrap(); writeln!(file3, "

Hello world!

").unwrap(); - r.register_templates_directory(".hbs", dir.path()).unwrap(); + r.register_templates_directory(".hbs", dir.path(), false).unwrap(); assert_eq!(r.templates.len(), 4); assert_eq!(r.templates.contains_key("t4"), true); @@ -913,7 +915,7 @@ mod test { let mut file3: File = File::create(&file3_path).unwrap(); writeln!(file3, "

Ciao {{world}}!

").unwrap(); - r.register_templates_directory(".hbs", dir.path()).unwrap(); + r.register_templates_directory(".hbs", dir.path(), false).unwrap(); assert_eq!(r.templates.len(), 7); assert_eq!(r.templates.contains_key("french/t7"), true); @@ -941,7 +943,7 @@ mod test { if !dir_path.ends_with("/") { dir_path.push('/'); } - r.register_templates_directory(".hbs", dir_path).unwrap(); + r.register_templates_directory(".hbs", dir_path, false).unwrap(); assert_eq!(r.templates.len(), 8); assert_eq!(r.templates.contains_key("t10"), true); @@ -965,7 +967,7 @@ mod test { if !dir_path.ends_with("/") { dir_path.push('/'); } - r.register_templates_directory(".hbs.html", dir_path) + r.register_templates_directory(".hbs.html", dir_path, false) .unwrap(); assert_eq!(r.templates.len(), 1); @@ -974,6 +976,26 @@ mod test { drop(file1); dir.close().unwrap(); } + + { + let dir = tempdir().unwrap(); + let mut r = Registry::new(); + + assert_eq!(r.templates.len(), 0); + + let file1_path = dir.path().join(".t12.hbs"); + let mut file1: File = File::create(&file1_path).unwrap(); + writeln!(file1, "

Hello {{world}}!

").unwrap(); + + r.register_templates_directory(".hbs", dir.path(), true).unwrap(); + + assert_eq!(r.templates.len(), 1); + assert_eq!(r.templates.contains_key(".t12"), true); + + drop(file1); + + dir.close().unwrap(); + } } #[test] From 40fa97b805d347e63e4b1e939202eedd31396c17 Mon Sep 17 00:00:00 2001 From: Yousif Akbar <11247449+yhakbar@users.noreply.github.com> Date: Mon, 28 Aug 2023 10:03:40 -0400 Subject: [PATCH 2/6] Formatting --- src/registry.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/registry.rs b/src/registry.rs index 76177312b..97552de13 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -850,7 +850,8 @@ mod test { let mut file4: File = File::create(&file4_path).unwrap(); writeln!(file4, "

Hallo {{world}}!

").unwrap(); - r.register_templates_directory(".hbs", dir.path(), false).unwrap(); + r.register_templates_directory(".hbs", dir.path(), false) + .unwrap(); assert_eq!(r.templates.len(), 3); assert_eq!(r.templates.contains_key("t1"), true); @@ -880,7 +881,8 @@ mod test { let mut file3: File = File::create(&file3_path).unwrap(); writeln!(file3, "

Hello world!

").unwrap(); - r.register_templates_directory(".hbs", dir.path(), false).unwrap(); + r.register_templates_directory(".hbs", dir.path(), false) + .unwrap(); assert_eq!(r.templates.len(), 4); assert_eq!(r.templates.contains_key("t4"), true); @@ -915,7 +917,8 @@ mod test { let mut file3: File = File::create(&file3_path).unwrap(); writeln!(file3, "

Ciao {{world}}!

").unwrap(); - r.register_templates_directory(".hbs", dir.path(), false).unwrap(); + r.register_templates_directory(".hbs", dir.path(), false) + .unwrap(); assert_eq!(r.templates.len(), 7); assert_eq!(r.templates.contains_key("french/t7"), true); @@ -943,7 +946,8 @@ mod test { if !dir_path.ends_with("/") { dir_path.push('/'); } - r.register_templates_directory(".hbs", dir_path, false).unwrap(); + r.register_templates_directory(".hbs", dir_path, false) + .unwrap(); assert_eq!(r.templates.len(), 8); assert_eq!(r.templates.contains_key("t10"), true); @@ -987,7 +991,8 @@ mod test { let mut file1: File = File::create(&file1_path).unwrap(); writeln!(file1, "

Hello {{world}}!

").unwrap(); - r.register_templates_directory(".hbs", dir.path(), true).unwrap(); + r.register_templates_directory(".hbs", dir.path(), true) + .unwrap(); assert_eq!(r.templates.len(), 1); assert_eq!(r.templates.contains_key(".t12"), true); From 5a332e4934c49469ea46d03ad69830c55c098ac5 Mon Sep 17 00:00:00 2001 From: Yousif Akbar <11247449+yhakbar@users.noreply.github.com> Date: Mon, 28 Aug 2023 10:32:39 -0400 Subject: [PATCH 3/6] Adding `DirectorySourceOptions` struct to handle variable configurations for template directories --- src/registry.rs | 64 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 13 deletions(-) diff --git a/src/registry.rs b/src/registry.rs index 97552de13..250e25cd4 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -100,6 +100,24 @@ fn rhai_engine() -> Engine { Engine::new() } +#[cfg(feature = "dir_source")] +pub struct DirectorySourceOptions { + tpl_extension: String, + hidden: bool, + temporary: bool, +} + +#[cfg(feature = "dir_source")] +impl Default for DirectorySourceOptions { + fn default() -> Self { + DirectorySourceOptions { + tpl_extension: ".hbs".to_owned(), + hidden: false, + temporary: false, + } + } +} + impl<'reg> Registry<'reg> { pub fn new() -> Registry<'reg> { let r = Registry { @@ -294,9 +312,8 @@ impl<'reg> Registry<'reg> { #[cfg_attr(docsrs, doc(cfg(feature = "dir_source")))] pub fn register_templates_directory

( &mut self, - tpl_extension: &str, dir_path: P, - hidden: bool, + options: DirectorySourceOptions, ) -> Result<(), TemplateError> where P: AsRef, @@ -309,13 +326,20 @@ impl<'reg> Registry<'reg> { .into_iter() .filter_map(|e| e.ok().map(|e| e.into_path())) // Checks if extension matches - .filter(|tpl_path| tpl_path.to_string_lossy().ends_with(tpl_extension)) + .filter(|tpl_path| { + tpl_path + .to_string_lossy() + .ends_with(options.tpl_extension.as_str()) + }) // Rejects any hidden or temporary files. .filter(|tpl_path| { tpl_path .file_stem() .map(|stem| stem.to_string_lossy()) - .map(|stem| !((!hidden && stem.starts_with('.')) || stem.starts_with('#'))) + .map(|stem| { + !((!options.hidden && stem.starts_with('.')) + || !options.temporary && stem.starts_with('#')) + }) .unwrap_or(false) }) .filter_map(|tpl_path| { @@ -330,7 +354,7 @@ impl<'reg> Registry<'reg> { .join("/"); tpl_name - .strip_suffix(tpl_extension) + .strip_suffix(options.tpl_extension.as_str()) .map(|s| s.to_owned()) .unwrap_or(tpl_name) }) @@ -828,6 +852,8 @@ mod test { fn test_register_templates_directory() { use std::fs::DirBuilder; + use crate::registry::DirectorySourceOptions; + let mut r = Registry::new(); { let dir = tempdir().unwrap(); @@ -850,7 +876,7 @@ mod test { let mut file4: File = File::create(&file4_path).unwrap(); writeln!(file4, "

Hallo {{world}}!

").unwrap(); - r.register_templates_directory(".hbs", dir.path(), false) + r.register_templates_directory(dir.path(), DirectorySourceOptions::default()) .unwrap(); assert_eq!(r.templates.len(), 3); @@ -881,7 +907,7 @@ mod test { let mut file3: File = File::create(&file3_path).unwrap(); writeln!(file3, "

Hello world!

").unwrap(); - r.register_templates_directory(".hbs", dir.path(), false) + r.register_templates_directory(dir.path(), DirectorySourceOptions::default()) .unwrap(); assert_eq!(r.templates.len(), 4); @@ -917,7 +943,7 @@ mod test { let mut file3: File = File::create(&file3_path).unwrap(); writeln!(file3, "

Ciao {{world}}!

").unwrap(); - r.register_templates_directory(".hbs", dir.path(), false) + r.register_templates_directory(dir.path(), DirectorySourceOptions::default()) .unwrap(); assert_eq!(r.templates.len(), 7); @@ -946,7 +972,7 @@ mod test { if !dir_path.ends_with("/") { dir_path.push('/'); } - r.register_templates_directory(".hbs", dir_path, false) + r.register_templates_directory(dir_path, DirectorySourceOptions::default()) .unwrap(); assert_eq!(r.templates.len(), 8); @@ -971,8 +997,14 @@ mod test { if !dir_path.ends_with("/") { dir_path.push('/'); } - r.register_templates_directory(".hbs.html", dir_path, false) - .unwrap(); + r.register_templates_directory( + dir_path, + DirectorySourceOptions { + tpl_extension: ".hbs.html".to_owned(), + ..Default::default() + }, + ) + .unwrap(); assert_eq!(r.templates.len(), 1); assert_eq!(r.templates.contains_key("t11"), true); @@ -991,8 +1023,14 @@ mod test { let mut file1: File = File::create(&file1_path).unwrap(); writeln!(file1, "

Hello {{world}}!

").unwrap(); - r.register_templates_directory(".hbs", dir.path(), true) - .unwrap(); + r.register_templates_directory( + dir.path(), + DirectorySourceOptions { + hidden: true, + ..Default::default() + }, + ) + .unwrap(); assert_eq!(r.templates.len(), 1); assert_eq!(r.templates.contains_key(".t12"), true); From ee83fd044d9fda72362b9cb980df0d6b6c03dba1 Mon Sep 17 00:00:00 2001 From: Yousif Akbar <11247449+yhakbar@users.noreply.github.com> Date: Mon, 28 Aug 2023 10:37:22 -0400 Subject: [PATCH 4/6] Updating documentation for `register_templates_directory` --- src/registry.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/registry.rs b/src/registry.rs index 250e25cd4..1f7080694 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -298,16 +298,16 @@ impl<'reg> Registry<'reg> { /// * `dir_path`: the path of directory /// /// Hidden files and tempfile (starts with `#`) will be ignored by default. - /// Set `hidden` to `true` to avoid ignoring hidden files. - /// All registered will use their relative name as template name. - /// For example, when `dir_path` is `templates/` and `tpl_extension` is `.hbs`, the file + /// Set `DirectorySourceOptions` to something other than `DirectorySourceOptions::default()` to adjust this. + /// All registered templates will use their relative path to determine their template name. + /// For example, when `dir_path` is `templates/` and `DirectorySourceOptions.tpl_extension` is `.hbs`, the file /// `templates/some/path/file.hbs` will be registered as `some/path/file`. /// /// This method is not available by default. /// You will need to enable the `dir_source` feature to use it. /// - /// When dev_mode enabled, like `register_template_file`, templates is reloaded - /// from file system everytime it's visied. + /// When dev_mode is enabled, like with `register_template_file`, templates are reloaded + /// from the file system every time they're visited. #[cfg(feature = "dir_source")] #[cfg_attr(docsrs, doc(cfg(feature = "dir_source")))] pub fn register_templates_directory

( From 9bf0f1e7a2e48e2df09c52f39dfaf0a7a318b50b Mon Sep 17 00:00:00 2001 From: Yousif Akbar <11247449+yhakbar@users.noreply.github.com> Date: Tue, 29 Aug 2023 08:34:31 -0400 Subject: [PATCH 5/6] Applying De Morgan's Law per lint rule --- src/registry.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/registry.rs b/src/registry.rs index 1f7080694..efa5ef9be 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -337,8 +337,7 @@ impl<'reg> Registry<'reg> { .file_stem() .map(|stem| stem.to_string_lossy()) .map(|stem| { - !((!options.hidden && stem.starts_with('.')) - || !options.temporary && stem.starts_with('#')) + (!stem.starts_with('#') || options.temporary) && (!stem.starts_with('.') || options.hidden) }) .unwrap_or(false) }) From 8de1ca76756ddd85b6b339572f06b0707224e666 Mon Sep 17 00:00:00 2001 From: Yousif Akbar <11247449+yhakbar@users.noreply.github.com> Date: Wed, 30 Aug 2023 07:44:09 -0400 Subject: [PATCH 6/6] Formatting --- src/registry.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/registry.rs b/src/registry.rs index efa5ef9be..c89c34388 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -337,7 +337,8 @@ impl<'reg> Registry<'reg> { .file_stem() .map(|stem| stem.to_string_lossy()) .map(|stem| { - (!stem.starts_with('#') || options.temporary) && (!stem.starts_with('.') || options.hidden) + (!stem.starts_with('#') || options.temporary) + && (!stem.starts_with('.') || options.hidden) }) .unwrap_or(false) })