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

Code before:

pub fn clear_assets() {
    clear_asset("themes.bin", "theme set cache");
    clear_asset("syntaxes.bin", "syntax set cache");
    clear_asset("metadata.yaml", "metadata file");
}

pub fn assets_from_cache_or_binary() -> Result<HighlightingAssets> {
    let cache_dir = PROJECT_DIRS.cache_dir();
    if let Some(metadata) = AssetsMetadata::load_from_folder(&cache_dir)? {
        if !metadata.is_compatible_with(crate_version!()) {
            return Err(format!(
                "The binary caches for the user-customized syntaxes and themes \
                 in '{}' are not compatible with this version of bat ({}). To solve this, \
                 either rebuild the cache (bat cache --build) or remove \
                 the custom syntaxes/themes (bat cache --clear).\n\
                 For more information, see:\n\n  \
                 https://github.com/sharkdp/bat#adding-new-syntaxes--language-definitions",
                cache_dir.to_string_lossy(),
                crate_version!()
            )
            .into());
        }
    }

    let no_custom_assets = wild::args_os().any(|arg| arg == "--no-custom-assets");
    let from_cache = if no_custom_assets {
        Err("Skipping custom assets due to --no-custom-assets arg".into())
    } else {
        HighlightingAssets::from_cache(&cache_dir)
    };

    Ok(from_cache.unwrap_or_else(|_| HighlightingAssets::from_binary()))
}

fn clear_asset(filename: &str, description: &str) {
    print!("Clearing {} ... ", description);
    fs::remove_file(PROJECT_DIRS.cache_dir().join(filename)).ok();
    println!("okay");
}


Review comment (bug):
It feels wrong to me to parse the entire set of environment arguments again here. It also ignores the possibility that `--no-custom-assets` could be part of `bat`s config file (even if unlikely).

Couldn't we pass a `custom_assets: bool` into this function as a parameter? Make that part of the `Config`, like other settings?

Code after:

pub fn clear_assets() {
    clear_asset("themes.bin", "theme set cache");
    clear_asset("syntaxes.bin", "syntax set cache");
    clear_asset("metadata.yaml", "metadata file");
}

pub fn assets_from_cache_or_binary(use_custom_assets: bool) -> Result<HighlightingAssets> {
    let cache_dir = PROJECT_DIRS.cache_dir();
    if let Some(metadata) = AssetsMetadata::load_from_folder(&cache_dir)? {
        if !metadata.is_compatible_with(crate_version!()) {
            return Err(format!(
                "The binary caches for the user-customized syntaxes and themes \
                 in '{}' are not compatible with this version of bat ({}). To solve this, \
                 either rebuild the cache (bat cache --build) or remove \
                 the custom syntaxes/themes (bat cache --clear).\n\
                 For more information, see:\n\n  \
                 https://github.com/sharkdp/bat#adding-new-syntaxes--language-definitions",
                cache_dir.to_string_lossy(),
                crate_version!()
            )
            .into());
        }
    }

    let custom_assets = if use_custom_assets {
        HighlightingAssets::from_cache(&cache_dir).ok()
    } else {
        None
    };
    Ok(custom_assets.unwrap_or_else(HighlightingAssets::from_binary))
}

fn clear_asset(filename: &str, description: &str) {
    print!("Clearing {} ... ", description);
    fs::remove_file(PROJECT_DIRS.cache_dir().join(filename)).ok();
    println!("okay");
}