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

Code before:
                    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'.
                        self.syntax_set.find_syntax_by_name(syntax_name)
                    }
                    Some(MappingTarget::MapToUnknown) => line_syntax,
                    None => ext_syntax.or(line_syntax),
                }
            }
            (None, InputFile::StdIn(None)) => String::from_utf8(reader.first_line.clone())
                .ok()
                .and_then(|l| self.syntax_set.find_syntax_by_first_line(&l)),
            (None, InputFile::StdIn(Some(file_name))) => self
                .syntax_set
                .find_syntax_by_extension(file_name.to_str().unwrap())
                .or_else(|| {
                    self.syntax_set.find_syntax_by_extension(
                        Path::new(file_name)
                            .extension()
                            .and_then(|x| x.to_str())
                            .unwrap_or(""),
                    )
                })
                .or(String::from_utf8(reader.first_line.clone())
                    .ok()
                    .and_then(|l| self.syntax_set.find_syntax_by_first_line(&l))),
            (_, InputFile::ThemePreviewFile) => self.syntax_set.find_syntax_by_name("Rust"),
        };

        syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
    }
}

#[cfg(test)]
mod tests {
    use std::ffi::OsStr;
    use std::fs::File;
    use std::io;
    use std::io::Write;

    use tempdir::TempDir;

    use crate::assets::HighlightingAssets;
    use crate::inputfile::{InputFile, OrdinaryFile};
    use crate::syntax_mapping::{MappingTarget, SyntaxMapping};

    struct SyntaxDetectionTest<'a> {
        assets: HighlightingAssets,
        pub syntax_mapping: SyntaxMapping<'a>,
        temp_dir: TempDir,
    }

Review comment (refactor):
I don't like the fact that the whole logic from the `(None, InputFile::Ordinary(ofile))` case above is repeated here. We should extract this into a separate method or try to move both cases into the same match arm.

Code after:
                    }
                }
            }
            (None, InputFile::StdIn(None)) => String::from_utf8(reader.first_line.clone())
                .ok()
                .and_then(|l| self.syntax_set.find_syntax_by_first_line(&l)),
            (None, InputFile::StdIn(Some(file_name))) => self
                .get_extension_syntax(file_name)
                .or(self.get_first_line_syntax(reader)),
            (_, InputFile::ThemePreviewFile) => self.syntax_set.find_syntax_by_name("Rust"),
        };

        syntax.unwrap_or_else(|| self.syntax_set.find_syntax_plain_text())
    }

    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(|| {
                self.syntax_set.find_syntax_by_extension(
                    Path::new(file_name)
                        .extension()
                        .and_then(|x| x.to_str())
                        .unwrap_or_default(),
                )
            })
    }

    fn get_first_line_syntax(&self, reader: &mut InputFileReader) -> 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 std::ffi::OsStr;
    use std::fs::File;
    use std::io;
    use std::io::Write;

    use tempdir::TempDir;

    use crate::assets::HighlightingAssets;
    use crate::inputfile::{InputFile, OrdinaryFile};
    use crate::syntax_mapping::{MappingTarget, SyntaxMapping};

    struct SyntaxDetectionTest<'a> {
        assets: HighlightingAssets,
        pub syntax_mapping: SyntaxMapping<'a>,