derive: don't panic if the extension is non-UTF-8

This commit is contained in:
René Kijewski 2024-08-07 06:17:21 +02:00
parent 1ff32cb0ab
commit ed692a087d
3 changed files with 5 additions and 4 deletions

View File

@ -478,11 +478,12 @@ fn ext_default_to_path<'a>(ext: Option<&'a str>, path: &'a Path) -> Option<&'a s
fn extension(path: &Path) -> Option<&str> { fn extension(path: &Path) -> Option<&str> {
const JINJA_EXTENSIONS: &[&str] = &["j2", "jinja", "jinja2", "rinja"]; const JINJA_EXTENSIONS: &[&str] = &["j2", "jinja", "jinja2", "rinja"];
let ext = path.extension().map(|s| s.to_str().unwrap())?; let ext = path.extension()?.to_str()?;
if JINJA_EXTENSIONS.contains(&ext) { if JINJA_EXTENSIONS.contains(&ext) {
// an extension was found: file stem cannot be absent
Path::new(path.file_stem().unwrap()) Path::new(path.file_stem().unwrap())
.extension() .extension()
.map(|s| s.to_str().unwrap()) .and_then(|s| s.to_str())
.or(Some(ext)) .or(Some(ext))
} else { } else {
Some(ext) Some(ext)

View File

@ -1 +1 @@
foo.html.rinja foo.html.rinja

View File

@ -1 +1 @@
foo.rinja foo.rinja