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

Code before:
    let mut result: String = String::new();

    let assets = assets_from_cache_or_binary()?;
    let mut languages = assets
        .syntaxes()
        .iter()
        .filter(|syntax| !syntax.hidden && !syntax.file_extensions.is_empty())
        .cloned()
        .collect::<Vec<_>>();

    // Handling of file-extension conflicts, see issue #1076
    for lang in languages.iter_mut() {
        let lang_name = lang.name.clone();
        lang.file_extensions.retain(|extension| {
            // The 'extension' variable is not certainly a real extension.
            //
            // Skip if 'extension' starts with '.', likely a hidden file like '.vimrc'
            // Also skip if the 'extension' contains another real extension, likely
            // that is a full match file name like 'CMakeLists.txt' and 'Cargo.lock'
            if extension.starts_with('.') || Path::new(extension).extension().is_some() {
                true
            } else {
                let test_file = Path::new("test").with_extension(extension);
                match assets.syntax_for_file_name(test_file, &config.syntax_mapping) {
                    Some(syntax) => syntax.name == lang_name,
                    None => false,
                }
            }
        });
    }

    languages.sort_by_key(|lang| lang.name.to_uppercase());

    let configured_languages = get_syntax_mapping_to_paths(config.syntax_mapping.mappings());

    for lang in languages.iter_mut() {
        if let Some(additional_paths) = configured_languages.get(lang.name.as_str()) {
            lang.file_extensions
                .extend(additional_paths.iter().cloned());
        }
    }

    if config.loop_through {
        for lang in languages {
            result += &format!("{}:{}\n", lang.name, lang.file_extensions.join(","));
        }
    } else {
        let longest = languages
            .iter()
            .map(|syntax| syntax.name.len())
            .max()

Review comment (suggestion):
definitely out of scope for this PR, but ideally we would have an API which would avoid this. Something like:
```
let syntaxes = assets.get_syntaxes()?;

// and then later:

syntaxes.get_syntax_for_file_name(…) // no unwrap here
```

Code after:
    let mut result: String = String::new();

    let assets = assets_from_cache_or_binary()?;
    let mut languages = assets
        .get_syntaxes()?
        .iter()
        .filter(|syntax| !syntax.hidden && !syntax.file_extensions.is_empty())
        .cloned()
        .collect::<Vec<_>>();

    // Handling of file-extension conflicts, see issue #1076
    for lang in languages.iter_mut() {
        let lang_name = lang.name.clone();
        lang.file_extensions.retain(|extension| {
            // The 'extension' variable is not certainly a real extension.
            //
            // Skip if 'extension' starts with '.', likely a hidden file like '.vimrc'
            // Also skip if the 'extension' contains another real extension, likely
            // that is a full match file name like 'CMakeLists.txt' and 'Cargo.lock'
            if extension.starts_with('.') || Path::new(extension).extension().is_some() {
                true
            } else {
                let test_file = Path::new("test").with_extension(extension);
                let syntax = assets
                    .get_syntax_for_file_name(test_file, &config.syntax_mapping)
                    .unwrap(); // safe since .get_syntaxes() above worked
                match syntax {
                    Some(syntax) => syntax.name == lang_name,
                    None => false,
                }
            }
        });
    }

    languages.sort_by_key(|lang| lang.name.to_uppercase());

    let configured_languages = get_syntax_mapping_to_paths(config.syntax_mapping.mappings());

    for lang in languages.iter_mut() {
        if let Some(additional_paths) = configured_languages.get(lang.name.as_str()) {
            lang.file_extensions
                .extend(additional_paths.iter().cloned());
        }
    }

    if config.loop_through {
        for lang in languages {
            result += &format!("{}:{}\n", lang.name, lang.file_extensions.join(","));
        }
    } else {
        let longest = languages