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:
+5
-2
@@ -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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user