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

Code before:
    /// Returns [Error::UndetectedSyntax] if it was not possible detect syntax
    /// based on path/file name/extension (or if the path was mapped to
    /// [MappingTarget::MapToUnknown]). In this case it is appropriate to fall
    /// back to other methods to detect syntax. Such as using the contents of
    /// the first line of the file.
    ///
    /// Returns [Error::UnknownSyntax] if a syntax mapping exist, but the mapped
    /// syntax does not exist.
    pub fn get_syntax_for_path(
        &self,
        path: impl AsRef<Path>,
        mapping: &SyntaxMapping,
    ) -> Result<SyntaxReferenceInSet> {
        let path = path.as_ref();
        match mapping.get_syntax_for(path) {
            Some(MappingTarget::MapToUnknown) => {
                Err(Error::UndetectedSyntax(path.to_string_lossy().into()))
            }

            Some(MappingTarget::MapTo(syntax_name)) => self
                .find_syntax_by_name(syntax_name)?
                .ok_or_else(|| Error::UnknownSyntax(syntax_name.to_owned())),

            Some(MappingTarget::MapExtensionToUnknown) => {
                let file_name = path.file_name().unwrap_or_default();
                self.get_extension_syntax_by_file_name(file_name)?
                    .ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into()))
            }

            None => {
                let file_name = path.file_name().unwrap_or_default();
                let mut syntax_match = self
                    .get_extension_syntax_by_file_name(file_name)?
                    .ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into()));

                if syntax_match.is_err() {
                    syntax_match = self
                        .get_extension_syntax_by_file_extension(file_name)?
                        .ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into()))
                }

                syntax_match
            }
        }
    }

    pub(crate) fn get_theme(&self, theme: &str) -> &Theme {
        match self.get_theme_set().themes.get(theme) {
            Some(theme) => theme,
            None => {
                if theme == "ansi-light" || theme == "ansi-dark" {

Review comment (suggestion):
It would be very nice if we could restructure the code so that `self.get_extension_syntax_by_file_name()` is only called once. It is totally fine if you have to do some bigger restructuring first. For example, I'm not sure we can keep the `match`. We might need an `if`. Not repeating code has higher prio than keeping the current structure.

Let me know if you find no way to achieve this. I can help out if you get stuck.

Code after:
    /// detecting syntax based on file name extension, only the file name
    /// extension itself matters.
    ///
    /// Returns [Error::UndetectedSyntax] if it was not possible detect syntax
    /// based on path/file name/extension (or if the path was mapped to
    /// [MappingTarget::MapToUnknown] or [MappingTarget::MapExtensionToUnknown]).
    /// In this case it is appropriate to fall back to other methods to detect
    /// syntax. Such as using the contents of the first line of the file.
    ///
    /// Returns [Error::UnknownSyntax] if a syntax mapping exist, but the mapped
    /// syntax does not exist.
    pub fn get_syntax_for_path(
        &self,
        path: impl AsRef<Path>,
        mapping: &SyntaxMapping,
    ) -> Result<SyntaxReferenceInSet> {
        let path = path.as_ref();

        let syntax_match = mapping.get_syntax_for(path);

        if let Some(MappingTarget::MapToUnknown) = syntax_match {
            return Err(Error::UndetectedSyntax(path.to_string_lossy().into()));
        }

        if let Some(MappingTarget::MapTo(syntax_name)) = syntax_match {
            return self
                .find_syntax_by_name(syntax_name)?
                .ok_or_else(|| Error::UnknownSyntax(syntax_name.to_owned()));
        }

        let file_name = path.file_name().unwrap_or_default();

        match (self.get_syntax_for_file_name(file_name)?, syntax_match) {
            (Some(syntax), _) => Ok(syntax),

            (_, Some(MappingTarget::MapExtensionToUnknown)) => {
                Err(Error::UndetectedSyntax(path.to_string_lossy().into()))
            }

            _ => self
                .get_syntax_for_file_extension(file_name)?
                .ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into())),
        }
    }

    pub(crate) fn get_theme(&self, theme: &str) -> &Theme {
        match self.get_theme_set().themes.get(theme) {
            Some(theme) => theme,
            None => {
                if theme == "ansi-light" || theme == "ansi-dark" {
                    bat_warning!("Theme '{}' is deprecated, using 'ansi' instead.", theme);