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

Code before:
            Some(theme) => theme,
            None => {
                if theme != "" {
                    use ansi_term::Colour::Yellow;
                    eprintln!(
                        "{}: Unknown theme '{}', using default.",
                        Yellow.paint("[bat warning]"),
                        theme
                    );
                }
                &self.theme_set.themes[self.fallback_theme.unwrap_or_else(|| Self::default_theme())]
            }
        }
    }

    pub(crate) fn get_syntax(
        &self,
        language: Option<&str>,
        input: &mut OpenedInput,
        mapping: &SyntaxMapping,
    ) -> &SyntaxReference {
        let syntax = if match input.kind {
            OpenedInputKind::ThemePreviewFile => true,
            _ => false,
        } {
            // FIXME: replace the above match statement with matches macro when min Rust >= 1.42.0
            self.syntax_set.find_syntax_by_name("Rust")
        } else if let Some(language) = language {
            self.syntax_set.find_syntax_by_token(language)
        } else {
            let line_syntax = self.get_first_line_syntax(&mut input.reader);

            // Get the path of the file:
            // If this was set by the metadata, that will take priority.
            // If it wasn't, it will use the real file path (if available).
            let path_str =
                input
                    .metadata
                    .user_provided_name
                    .as_ref()
                    .or_else(|| match input.kind {
                        OpenedInputKind::OrdinaryFile(ref path) => Some(path),
                        _ => None,
                    });

            if let Some(path_str) = path_str {
                // If a path was provided, we try and detect the syntax based on extension mappings.
                let path = Path::new(path_str);
                let absolute_path = path.canonicalize().ok().unwrap_or_else(|| path.to_owned());

                match mapping.get_syntax_for(absolute_path) {

Review comment (refactor):
:+1: 

In the meantime, could we move this logic to a helper function in the `impl OpenedInputKind` block? This would be much easier to read:

``` rust
let syntax = if input.kind.is_theme_preview_file() {
```


Code after:
            Some(theme) => theme,
            None => {
                if theme != "" {
                    use ansi_term::Colour::Yellow;
                    eprintln!(
                        "{}: Unknown theme '{}', using default.",
                        Yellow.paint("[bat warning]"),
                        theme
                    );
                }
                &self.theme_set.themes[self.fallback_theme.unwrap_or_else(|| Self::default_theme())]
            }
        }
    }

    pub(crate) fn get_syntax(
        &self,
        language: Option<&str>,
        input: &mut OpenedInput,
        mapping: &SyntaxMapping,
    ) -> &SyntaxReference {
        let syntax = if input.kind.is_theme_preview_file() {
            self.syntax_set.find_syntax_by_name("Rust")
        } else if let Some(language) = language {
            self.syntax_set.find_syntax_by_token(language)
        } else {
            let line_syntax = self.get_first_line_syntax(&mut input.reader);

            // Get the path of the file:
            // If this was set by the metadata, that will take priority.
            // If it wasn't, it will use the real file path (if available).
            let path_str =
                input
                    .metadata
                    .user_provided_name
                    .as_ref()
                    .or_else(|| match input.kind {
                        OpenedInputKind::OrdinaryFile(ref path) => Some(path),
                        _ => None,
                    });

            if let Some(path_str) = path_str {
                // If a path was provided, we try and detect the syntax based on extension mappings.
                let path = Path::new(path_str);
                let absolute_path = path.canonicalize().ok().unwrap_or_else(|| path.to_owned());

                match mapping.get_syntax_for(absolute_path) {
                    Some(MappingTarget::MapToUnknown) => line_syntax,
                    Some(MappingTarget::MapTo(syntax_name)) => {
                        // TODO: we should probably return an error here if this syntax can not be
                        // found. Currently, we just fall back to 'plain'.