on master help sign in
Reduce nesting in if blocks by short circuiting
372e42fMarcin Puc
src/assets.rs+46−47
@@ -255,57 +255,56 @@ impl HighlightingAssets {
255 ) -> Result<SyntaxReferenceInSet> {
255 ) -> Result<SyntaxReferenceInSet> {
256 if let Some(language) = language {
256 if let Some(language) = language {
257 let syntax_set = self.get_syntax_set_by_name(language)?;
257 let syntax_set = self.get_syntax_set_by_name(language)?;
258− syntax_set
258+ return syntax_set
259 .find_syntax_by_token(language)
259 .find_syntax_by_token(language)
260 .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set })
260 .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set })
261− .ok_or_else(|| Error::UnknownSyntax(language.to_owned()))
261+ .ok_or_else(|| Error::UnknownSyntax(language.to_owned()));
262− } else {
262+ }
263+
263− let line_syntax = self.get_first_line_syntax(&mut input.reader)?;
264+ let line_syntax = self.get_first_line_syntax(&mut input.reader)?;
264−
265+
265− // Get the path of the file:
266+ // Get the path of the file:
266− // If this was set by the metadata, that will take priority.
267+ // If this was set by the metadata, that will take priority.
267− // If it wasn't, it will use the real file path (if available).
268+ // If it wasn't, it will use the real file path (if available).
268− let path_str =
269+ let path_str = input
269− input
270− .metadata
270+ .metadata
271− .user_provided_name
271+ .user_provided_name
272− .as_ref()
272+ .as_ref()
273− .or_else(|| match input.kind {
273+ .or_else(|| match input.kind {
274− OpenedInputKind::OrdinaryFile(ref path) => Some(path),
274+ OpenedInputKind::OrdinaryFile(ref path) => Some(path),
275− _ => None,
275+ _ => None,
276− });
276+ });
277−
277+
278− if let Some(path_str) = path_str {
278+ if let Some(path_str) = path_str {
279− // If a path was provided, we try and detect the syntax based on extension mappings.
279+ // If a path was provided, we try and detect the syntax based on extension mappings.
280− let path = Path::new(path_str);
280+ let path = Path::new(path_str);
281− let absolute_path = PathAbs::new(path)
281+ let absolute_path = PathAbs::new(path)
282− .ok()
282+ .ok()
283− .map(|p| p.as_path().to_path_buf())
283+ .map(|p| p.as_path().to_path_buf())
284− .unwrap_or_else(|| path.to_owned());
284+ .unwrap_or_else(|| path.to_owned());
285−
285+
286− match mapping.get_syntax_for(absolute_path) {
286+ match mapping.get_syntax_for(absolute_path) {
287− Some(MappingTarget::MapToUnknown) => line_syntax
287+ Some(MappingTarget::MapToUnknown) => line_syntax
288− .ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into())),
288+ .ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into())),
289−
289+
290− Some(MappingTarget::MapTo(syntax_name)) => {
290+ Some(MappingTarget::MapTo(syntax_name)) => {
291− let syntax_set = self.get_syntax_set()?;
291+ let syntax_set = self.get_syntax_set()?;
292− syntax_set
292+ syntax_set
293− .find_syntax_by_name(syntax_name)
293+ .find_syntax_by_name(syntax_name)
294− .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set })
294+ .map(|syntax| SyntaxReferenceInSet { syntax, syntax_set })
295− .ok_or_else(|| Error::UnknownSyntax(syntax_name.to_owned()))
295+ .ok_or_else(|| Error::UnknownSyntax(syntax_name.to_owned()))
296− }
296+ }
297−
297+
298− None => {
298+ None => {
299− let file_name = path.file_name().unwrap_or_default();
299+ let file_name = path.file_name().unwrap_or_default();
300− self.get_extension_syntax(file_name)?
300+ self.get_extension_syntax(file_name)?
301− .or(line_syntax)
301+ .or(line_syntax)
302− .ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into()))
302+ .ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into()))
303− }
304 }
303 }
305− } else {
306− // If a path wasn't provided, we fall back to the detect first-line syntax.
307− line_syntax.ok_or_else(|| Error::UndetectedSyntax("[unknown]".into()))
308 }
304 }
305+ } else {
306+ // If a path wasn't provided, we fall back to the detect first-line syntax.
307+ line_syntax.ok_or_else(|| Error::UndetectedSyntax("[unknown]".into()))
309 }
308 }
310 }
309 }
311
310
src/bin/bat/main.rs+7−10
@@ -100,17 +100,14 @@ pub fn get_languages(config: &Config) -> Result<String> {
100 // Also skip if the 'extension' contains another real extension, likely
100 // Also skip if the 'extension' contains another real extension, likely
101 // that is a full match file name like 'CMakeLists.txt' and 'Cargo.lock'
101 // that is a full match file name like 'CMakeLists.txt' and 'Cargo.lock'
102 if extension.starts_with('.') || Path::new(extension).extension().is_some() {
102 if extension.starts_with('.') || Path::new(extension).extension().is_some() {
103− true
103+ return true;
104− } else {
105− let test_file = Path::new("test").with_extension(extension);
106− let syntax_in_set = assets
107− .get_syntax_for_file_name(test_file, &config.syntax_mapping)
108− .unwrap(); // safe since .get_syntaxes() above worked
109− match syntax_in_set {
110− Some(syntax_in_set) => syntax_in_set.syntax.name == lang_name,
111− None => false,
112− }
113 }
104 }
105+
106+ let test_file = Path::new("test").with_extension(extension);
107+ let syntax_in_set = assets
108+ .get_syntax_for_file_name(test_file, &config.syntax_mapping)
109+ .unwrap(); // safe since .get_syntaxes() above worked
110+ matches!(syntax_in_set, Some(syntax_in_set) if syntax_in_set.syntax.name == lang_name)
114 });
111 });
115 }
112 }
116
113
src/input.rs+9−9
@@ -256,18 +256,18 @@ impl<'a> InputReader<'a> {
256 }
256 }
257
257
258 pub(crate) fn read_line(&mut self, buf: &mut Vec<u8>) -> io::Result<bool> {
258 pub(crate) fn read_line(&mut self, buf: &mut Vec<u8>) -> io::Result<bool> {
259− if self.first_line.is_empty() {
259+ if !self.first_line.is_empty() {
260− let res = self.inner.read_until(b'\n', buf).map(|size| size > 0)?;
260+ buf.append(&mut self.first_line);
261+ return Ok(true);
262+ }
261
263
262− if self.content_type == Some(ContentType::UTF_16LE) {
263− self.inner.read_until(0x00, buf).ok();
264+ let res = self.inner.read_until(b'\n', buf).map(|size| size > 0)?;
264− }
265
265
266− Ok(res)
266+ if self.content_type == Some(ContentType::UTF_16LE) {
267− } else {
267+ let _ = self.inner.read_until(0x00, buf);
268− buf.append(&mut self.first_line);
269− Ok(true)
270 }
268 }
269+
270+ Ok(res)
271 }
271 }
272}
272}
273
273
src/less.rs+6−6
@@ -11,13 +11,13 @@ pub fn retrieve_less_version(less_path: &dyn AsRef<OsStr>) -> Option<usize> {
11}
11}
12
12
13fn parse_less_version(output: &[u8]) -> Option<usize> {
13fn parse_less_version(output: &[u8]) -> Option<usize> {
14− if output.starts_with(b"less ") {
14+ if !output.starts_with(b"less ") {
15− let version = std::str::from_utf8(&output[5..]).ok()?;
16− let end = version.find(|c: char| !c.is_ascii_digit())?;
17− version[..end].parse::<usize>().ok()
18− } else {
19− None
15+ return None;
20 }
16 }
17+
18+ let version = std::str::from_utf8(&output[5..]).ok()?;
19+ let end = version.find(|c: char| !c.is_ascii_digit())?;
20+ version[..end].parse::<usize>().ok()
21}
21}
22
22
23#[test]
23#[test]
src/printer.rs+16−16
@@ -226,29 +226,29 @@ impl<'a> InteractivePrinter<'a> {
226
226
227 fn create_fake_panel(&self, text: &str) -> String {
227 fn create_fake_panel(&self, text: &str) -> String {
228 if self.panel_width == 0 {
228 if self.panel_width == 0 {
229− "".to_string()
229+ return "".to_string();
230+ }
231+
232+ let text_truncated: String = text.chars().take(self.panel_width - 1).collect();
233+ let text_filled: String = format!(
234+ "{}{}",
235+ text_truncated,
236+ " ".repeat(self.panel_width - 1 - text_truncated.len())
237+ );
238+ if self.config.style_components.grid() {
239+ format!("{} │ ", text_filled)
230 } else {
240 } else {
231− let text_truncated: String = text.chars().take(self.panel_width - 1).collect();
232− let text_filled: String = format!(
233− "{}{}",
234− text_truncated,
235− " ".repeat(self.panel_width - 1 - text_truncated.len())
236− );
237− if self.config.style_components.grid() {
238− format!("{} │ ", text_filled)
239− } else {
240− text_filled
241+ text_filled
241− }
242 }
242 }
243 }
243 }
244
244
245 fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
245 fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
246 if self.config.tab_width > 0 {
246 if self.config.tab_width > 0 {
247− expand_tabs(text, self.config.tab_width, cursor)
247+ return expand_tabs(text, self.config.tab_width, cursor);
248− } else {
249− *cursor += text.len();
250− text.to_string()
251 }
248 }
249+
250+ *cursor += text.len();
251+ text.to_string()
252 }
252 }
253}
253}
254
254