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

Code before:
    ) -> Result<Option<SyntaxReferenceInSet>> {
        let file_path = Path::new(file_name);
        let syntax_set = self.get_syntax_set()?;
        Ok(syntax_set
            .find_syntax_by_extension(
                file_path
                    .extension()
                    .and_then(|x| x.to_str())
                    .unwrap_or_default(),
            )
            .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set }))
    }

    /// If we find an ignored suffix on the file name, e.g. '~', we strip it and
    /// then try again to find a syntax without it. Note that we do this recursively.
    fn get_extension_syntax_with_stripped_suffix(
        &self,
        file_name: &OsStr,
    ) -> Result<Option<SyntaxReferenceInSet>> {
        let file_path = Path::new(file_name);

        if let Some(file_str) = file_path.to_str() {
            for suffix in &IGNORED_SUFFIXES {
                if let Some(stripped_filename) = file_str.strip_suffix(suffix) {
                    let syntax = self.get_extension_syntax(OsStr::new(stripped_filename))?;
                    return Ok(syntax);
                }
            }
        }
        Ok(None)
    }

    fn get_first_line_syntax(
        &self,
        reader: &mut InputReader,
    ) -> Result<Option<SyntaxReferenceInSet>> {
        let syntax_set = self.get_syntax_set()?;
        Ok(String::from_utf8(reader.first_line.clone())
            .ok()
            .and_then(|l| syntax_set.find_syntax_by_first_line(&l))
            .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set }))
    }
}

#[cfg(feature = "build-assets")]
pub use crate::build_assets::build_assets as build;

/// A SyntaxSet in serialized form, i.e. bincoded and flate2 compressed.
/// We keep it in this format since we want to load it lazily.
#[derive(Debug)]
enum SerializedSyntaxSet {

Review comment (refactor):
I do like your other refactorings where you change to early returns to reduce indentation level by one. Thanks for those!

But I do think in this case the older code is better, because:
* There is no reduction in indentation level
* I would not qualify this as "early return" which makes it "return in the middle" which is an anti-pattern
* There is now two exit points rather than one, which complicates e.g. adding a `println!` of the return value.

Sorry to bikeshed/nitpick. I really try to avoid doing that, but sometimes I can't stop myself :)

Code after:
    ) -> Result<Option<SyntaxReferenceInSet>> {
        let file_path = Path::new(file_name);
        let syntax_set = self.get_syntax_set()?;
        Ok(syntax_set
            .find_syntax_by_extension(
                file_path
                    .extension()
                    .and_then(|x| x.to_str())
                    .unwrap_or_default(),
            )
            .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set }))
    }

    /// If we find an ignored suffix on the file name, e.g. '~', we strip it and
    /// then try again to find a syntax without it. Note that we do this recursively.
    fn get_extension_syntax_with_stripped_suffix(
        &self,
        file_name: &OsStr,
    ) -> Result<Option<SyntaxReferenceInSet>> {
        let file_path = Path::new(file_name);
        let mut syntax = None;
        if let Some(file_str) = file_path.to_str() {
            for suffix in &IGNORED_SUFFIXES {
                if let Some(stripped_filename) = file_str.strip_suffix(suffix) {
                    syntax = self.get_extension_syntax(OsStr::new(stripped_filename))?;
                    break;
                }
            }
        }
        Ok(syntax)
    }

    fn get_first_line_syntax(
        &self,
        reader: &mut InputReader,
    ) -> Result<Option<SyntaxReferenceInSet>> {
        let syntax_set = self.get_syntax_set()?;
        Ok(String::from_utf8(reader.first_line.clone())
            .ok()
            .and_then(|l| syntax_set.find_syntax_by_first_line(&l))
            .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set }))
    }
}

#[cfg(feature = "build-assets")]
pub use crate::build_assets::build_assets as build;

/// A SyntaxSet in serialized form, i.e. bincoded and flate2 compressed.
/// We keep it in this format since we want to load it lazily.
#[derive(Debug)]
enum SerializedSyntaxSet {