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

Code before:
/// structures like `by_name` are tiny. If we compress, deserialization can't do
/// efficient byte-by-byte copy of `serialized_syntax_sets`.
pub(crate) const COMPRESS_MINIMAL_SYNTAXES: bool = false;

impl HighlightingAssets {
    fn new(
        serialized_syntax_set: SerializedSyntaxSet,
        minimal_syntaxes: MinimalSyntaxes,
        theme_set: ThemeSet,
        ignored_suffixes: IgnoredSuffixes,
    ) -> Self {
        HighlightingAssets {
            syntax_set_cell: LazyCell::new(),
            serialized_syntax_set,
            minimal_assets: MinimalAssets::new(minimal_syntaxes),
            theme_set,
            fallback_theme: None,
            ignored_suffixes,
        }
    }

    pub fn default_theme() -> &'static str {
        "Monokai Extended"
    }

    pub fn from_cache(cache_path: &Path, ignored_suffixes: IgnoredSuffixes) -> Result<Self> {
        Ok(HighlightingAssets::new(
            SerializedSyntaxSet::FromFile(cache_path.join("syntaxes.bin")),
            asset_from_cache(
                &cache_path.join("minimal_syntaxes.bin"),
                "minimal syntax sets",
                COMPRESS_MINIMAL_SYNTAXES,
            )?,
            asset_from_cache(&cache_path.join("themes.bin"), "theme set", COMPRESS_THEMES)?,
            ignored_suffixes,
        ))
    }

    pub fn from_binary(ignored_suffixes: IgnoredSuffixes) -> Self {
        HighlightingAssets::new(
            SerializedSyntaxSet::FromBinary(get_serialized_integrated_syntaxset()),
            get_integrated_minimal_syntaxes(),
            get_integrated_themeset(),
            ignored_suffixes,
        )
    }

    pub fn set_fallback_theme(&mut self, theme: &'static str) {
        self.fallback_theme = Some(theme);
    }


Review comment (refactor):
Thanks for looking into this!

I don't have time for a detailed code review right now, but one spontaneous comment of mine is that I think this affects the public API too much. We can reduce the API surface by having a setter instead. I think that will also simplify the code.

I'm also spontaneously not a big fan of adding this to `HighlightingAssets`, since it is not related to assets per se. Maybe keep it in the config instead, and pass the config around?

That said, this PR conflicts a non-negligible amount with https://github.com/sharkdp/bat/pull/1889. Maybe let https://github.com/sharkdp/bat/pull/1889 has its course and then look into this again, to avoid conflicts?

Thanks again for wanting to help, I'm sure we can come up with something together that will be mergable eventually 👍 

Code after:
/// efficient byte-by-byte copy of `serialized_syntax_sets`.
pub(crate) const COMPRESS_MINIMAL_SYNTAXES: bool = false;

impl HighlightingAssets {
    fn new(
        serialized_syntax_set: SerializedSyntaxSet,
        minimal_syntaxes: MinimalSyntaxes,
        theme_set: ThemeSet,
    ) -> Self {
        HighlightingAssets {
            syntax_set_cell: LazyCell::new(),
            serialized_syntax_set,
            minimal_assets: MinimalAssets::new(minimal_syntaxes),
            theme_set,
            fallback_theme: None,
        }
    }

    pub fn default_theme() -> &'static str {
        "Monokai Extended"
    }

    pub fn from_cache(cache_path: &Path) -> Result<Self> {
        Ok(HighlightingAssets::new(
            SerializedSyntaxSet::FromFile(cache_path.join("syntaxes.bin")),
            asset_from_cache(
                &cache_path.join("minimal_syntaxes.bin"),
                "minimal syntax sets",
                COMPRESS_MINIMAL_SYNTAXES,
            )?,
            asset_from_cache(&cache_path.join("themes.bin"), "theme set", COMPRESS_THEMES)?,
        ))
    }

    pub fn from_binary() -> Self {
        HighlightingAssets::new(
            SerializedSyntaxSet::FromBinary(get_serialized_integrated_syntaxset()),
            get_integrated_minimal_syntaxes(),
            get_integrated_themeset(),
        )
    }

    pub fn set_fallback_theme(&mut self, theme: &'static str) {
        self.fallback_theme = Some(theme);
    }

    pub(crate) fn get_syntax_set(&self) -> Result<&SyntaxSet> {
        self.syntax_set_cell
            .try_borrow_with(|| self.serialized_syntax_set.deserialize())
    }