From fe282a0aea95d982a8fd193bf3483e39e5b9771e Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 4 Nov 2020 20:43:29 -0600 Subject: [PATCH] refactor: Pull out common policy --- crates/typos/src/checks.rs | 77 ++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/crates/typos/src/checks.rs b/crates/typos/src/checks.rs index ba196ca..9a2404d 100644 --- a/crates/typos/src/checks.rs +++ b/crates/typos/src/checks.rs @@ -112,19 +112,12 @@ impl ParseIdentifiers { return Ok(typos_found); } - let buffer = std::fs::read(path) - .map_err(|e| crate::ErrorKind::IoError.into_error().with_source(e))?; - if !explicit && !self.binary { - let content_type = content_inspector::inspect(&buffer); - if content_type.is_binary() - // HACK: We only support UTF-8 at the moment - || (content_type != content_inspector::ContentType::UTF_8_BOM - && content_type != content_inspector::ContentType::UTF_8) - { - let msg = report::BinaryFile { path }; - reporter.report(msg.into()); - return Ok(typos_found); - } + let buffer = read_file(path)?; + let (buffer, content_type) = massage_data(buffer)?; + if !explicit && !self.binary && content_type.is_binary() { + let msg = report::BinaryFile { path }; + reporter.report(msg.into()); + return Ok(typos_found); } for line in buffer.lines() { @@ -188,19 +181,12 @@ impl ParseWords { return Ok(typos_found); } - let buffer = std::fs::read(path) - .map_err(|e| crate::ErrorKind::IoError.into_error().with_source(e))?; - if !explicit && !self.binary { - let content_type = content_inspector::inspect(&buffer); - // HACK: We only support UTF-8 at the moment - if content_type.is_binary() - || (content_type != content_inspector::ContentType::UTF_8_BOM - && content_type != content_inspector::ContentType::UTF_8) - { - let msg = report::BinaryFile { path }; - reporter.report(msg.into()); - return Ok(typos_found); - } + let buffer = read_file(path)?; + let (buffer, content_type) = massage_data(buffer)?; + if !explicit && !self.binary && content_type.is_binary() { + let msg = report::BinaryFile { path }; + reporter.report(msg.into()); + return Ok(typos_found); } for line in buffer.lines() { @@ -295,20 +281,12 @@ impl Checks { return Ok(typos_found); } - let buffer = std::fs::read(path) - .map_err(|e| crate::ErrorKind::IoError.into_error().with_source(e))?; - if !explicit && !self.binary { - let content_type = content_inspector::inspect(&buffer); - // HACK: We only support UTF-8 at the moment - if content_type.is_binary() - || (content_type != content_inspector::ContentType::UTF_8_BOM - && content_type != content_inspector::ContentType::UTF_8) - { - // HACK: we don't support alternative encodings atm - let msg = report::BinaryFile { path }; - reporter.report(msg.into()); - return Ok(typos_found); - } + let buffer = read_file(path)?; + let (buffer, content_type) = massage_data(buffer)?; + if !explicit && !self.binary && content_type.is_binary() { + let msg = report::BinaryFile { path }; + reporter.report(msg.into()); + return Ok(typos_found); } for (line_idx, line) in buffer.lines().enumerate() { @@ -355,3 +333,22 @@ impl Checks { Ok(typos_found) } } + +fn read_file(path: &std::path::Path) -> Result, crate::Error> { + std::fs::read(path).map_err(|e| crate::ErrorKind::IoError.into_error().with_source(e)) +} + +fn massage_data( + buffer: Vec, +) -> Result<(Vec, content_inspector::ContentType), crate::Error> { + let mut content_type = content_inspector::inspect(&buffer); + + // HACK: We only support UTF-8 at the moment + if content_type != content_inspector::ContentType::UTF_8_BOM + && content_type != content_inspector::ContentType::UTF_8 + { + content_type = content_inspector::ContentType::BINARY; + } + + Ok((buffer, content_type)) +}