Repository: sharkdp/bat
File: src/assets.rs (Rust)

Code before:
                        self.get_extension_syntax(file_name)
                            .or(line_syntax)
                            .ok_or_else(|| {
                                ErrorKind::UndetectedSyntax(path.to_string_lossy().into()).into()
                            })
                    }
                }
            } else {
                // If a path wasn't provided, we fall back to the detect first-line syntax.
                line_syntax.ok_or_else(|| ErrorKind::UndetectedSyntax("[unknown]".into()).into())
            }
        }
    }

    fn get_extension_syntax(&self, file_name: &OsStr) -> Option<&SyntaxReference> {
        self.syntax_set
            .find_syntax_by_extension(file_name.to_str().unwrap_or_default())
            .or_else(|| {
                let file_path = Path::new(file_name);
                self.syntax_set.find_syntax_by_extension(
                    file_path
                        .extension()
                        .and_then(|x| x.to_str())
                        .unwrap_or_default(),
                ).or_else(|| {
                    let file_str = file_path.to_str().unwrap_or_default();
                    for suffix in IGNORED_SUFFIXES.iter() {
                        if file_str.ends_with(suffix) {
                            return self.get_extension_syntax(
                                OsStr::new(
                                    file_str
                                        .strip_suffix(suffix)
                                        .unwrap_or_default()
                                ));
                        }
                    }
                    None
                })
            })
    }

    fn get_first_line_syntax(&self, reader: &mut InputReader) -> Option<&SyntaxReference> {
        String::from_utf8(reader.first_line.clone())
            .ok()
            .and_then(|l| self.syntax_set.find_syntax_by_first_line(&l))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

Review comment (suggestion):
This will lead to an empty string if `file_path` contains invalid UTF-8 (which is possible). Instead of directly aborting here, we would still run the full `for` loop below. I think the behavior would still be okay, but it feels a bit strange. And could possibly lead to errors in the future if the code below is changed. I think I would prefer something like:
```rust
if let Some(file_str) = file_path.to_str() {
  // …
}
```

Code after:
                        self.get_extension_syntax(file_name)
                            .or(line_syntax)
                            .ok_or_else(|| {
                                ErrorKind::UndetectedSyntax(path.to_string_lossy().into()).into()
                            })
                    }
                }
            } else {
                // If a path wasn't provided, we fall back to the detect first-line syntax.
                line_syntax.ok_or_else(|| ErrorKind::UndetectedSyntax("[unknown]".into()).into())
            }
        }
    }

    fn get_extension_syntax(&self, file_name: &OsStr) -> Option<&SyntaxReference> {
        self.syntax_set
            .find_syntax_by_extension(file_name.to_str().unwrap_or_default())
            .or_else(|| {
                let file_path = Path::new(file_name);
                self.syntax_set.find_syntax_by_extension(
                    file_path
                        .extension()
                        .and_then(|x| x.to_str())
                        .unwrap_or_default(),
                ).or_else(|| {
                    if let Some(file_str) = file_path.to_str() {
                        for suffix in IGNORED_SUFFIXES.iter() {
                            if let Some(stripped_filename) = file_str.strip_suffix(suffix) {
                                return self.get_extension_syntax(OsStr::new(stripped_filename));
                            }
                        }
                    }
                    None
                })
            })
    }

    fn get_first_line_syntax(&self, reader: &mut InputReader) -> Option<&SyntaxReference> {
        String::from_utf8(reader.first_line.clone())
            .ok()
            .and_then(|l| self.syntax_set.find_syntax_by_first_line(&l))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::ffi::OsStr;

    use std::fs::File;