1
0
mirror of https://github.com/sharkdp/bat synced 2026-08-05 19:11:42 +00:00

HighlightingAssets: Make .syntaxes() and syntax_for_file_name() failable

Or rather, introduce new versions of these methods and deprecate the old ones.

This is preparation to enable robust and user-friendly support for lazy-loading.
With lazy-loading, we don't know if the SyntaxSet is valid until after we try to
use it, so wherever we try to use it, we need to return a Result. See discussion
about panics in #1747.
This commit is contained in:
Martin Nordholts
2021-07-27 09:43:51 +02:00
parent c0e09662b4
commit a81009607a
6 changed files with 95 additions and 39 deletions
+5 -2
View File
@@ -82,7 +82,7 @@ pub fn get_languages(config: &Config) -> Result<String> {
let assets = assets_from_cache_or_binary()?;
let mut languages = assets
.syntaxes()
.get_syntaxes()?
.iter()
.filter(|syntax| !syntax.hidden && !syntax.file_extensions.is_empty())
.cloned()
@@ -101,7 +101,10 @@ pub fn get_languages(config: &Config) -> Result<String> {
true
} else {
let test_file = Path::new("test").with_extension(extension);
match assets.syntax_for_file_name(test_file, &config.syntax_mapping) {
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,
}