sunng87 / handlebars-iron

Handlebars middleware for Iron web framework
MIT License
119 stars 20 forks source link

DirectorySource does not use correct path types #62

Closed cmbrandenburg closed 7 years ago

cmbrandenburg commented 7 years ago

The DirectorySource type is constructed using UTF-8 strings. At the least, it should use std::ffi::OsStr, but probably it should use std::path::Path.

This issue is related to https://github.com/sunng87/handlebars-iron/issues/23, where the idea was mentioned but seems to have been dropped.

Because this would be a breaking change, maybe it would make sense to rethink the DirectorySource API. Consider the following constructors:

pub fn new_with_extension<D, E>(directory: D, extension: E) -> DirectorySource
    where D: Into<std::path::PathBuf>,
          E: AsRef<std::ffi::OsStr>
{ unimplemented!(); }

pub fn new_with_filter<D, F>(directory: D, filter: F) -> DirectorySource
    where D: Into<std::path::PathBuf>,
          F: Fn(&std::path::Path) -> bool
{ unimplemented!(); }

The new_with_extension method would replace the current new method, whereby the application wants to use all files in a directory (and its subdirectories) with a given extension.

Whereas, the new_with_filter method would allow applications more flexibility in terms of filtering files based on any custom criteria.

sunng87 commented 7 years ago

Hi @cmbrandenburg , thank you for your suggestion. I totally agree with you that we should change the directory path type to something like Into<PathBuf>.

But for the filter one, currently we are using name pattern based on the prefix and suffix schema. We will name the template using the filename between prefix and suffix. If we provide a generic filter, we will lost this schema.