From aefc8fd8243b8270849aeecee7dc443210cd9b3b Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Mon, 27 Sep 2021 20:09:26 +0200 Subject: [PATCH] src/printer.rs: Simplify Plain Text fallback code By forwarding the task to find the `Plain Text` syntax to `assets`. Not only does the code become simpler; we also get rid of a call to `self.get_syntax_set()` which is beneficial to the long term goal of replacing `syntaxes.bin` with `minimal_syntaxes.bin`. Note that the use of `.expect()` is not a regression in error handling. It was previously hidden in `.find_syntax_plain_text()`. --- src/assets.rs | 5 ++++- src/printer.rs | 8 +++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/assets.rs b/src/assets.rs index 1c820d6c..5b35d803 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -245,7 +245,10 @@ impl HighlightingAssets { } } - fn find_syntax_by_name(&self, syntax_name: &str) -> Result> { + pub(crate) fn find_syntax_by_name( + &self, + syntax_name: &str, + ) -> Result> { let syntax_set = self.get_syntax_set()?; Ok(syntax_set .find_syntax_by_name(syntax_name) diff --git a/src/printer.rs b/src/printer.rs index beaee4a2..1ecbbd74 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -187,11 +187,9 @@ impl<'a> InteractivePrinter<'a> { let syntax_in_set = match assets.get_syntax(config.language, input, &config.syntax_mapping) { Ok(syntax_in_set) => syntax_in_set, - Err(Error::UndetectedSyntax(_)) => { - let syntax_set = assets.get_syntax_set()?; - let syntax = syntax_set.find_syntax_plain_text(); - SyntaxReferenceInSet { syntax, syntax_set } - } + Err(Error::UndetectedSyntax(_)) => assets + .find_syntax_by_name("Plain Text")? + .expect("A plain text syntax is available"), Err(e) => return Err(e), };