mirror of
https://github.com/crate-ci/typos.git
synced 2024-11-22 00:51:11 -05:00
refactor: Pull out common policy
This commit is contained in:
parent
2ef1d02164
commit
fe282a0aea
1 changed files with 37 additions and 40 deletions
|
@ -112,19 +112,12 @@ impl ParseIdentifiers {
|
||||||
return Ok(typos_found);
|
return Ok(typos_found);
|
||||||
}
|
}
|
||||||
|
|
||||||
let buffer = std::fs::read(path)
|
let buffer = read_file(path)?;
|
||||||
.map_err(|e| crate::ErrorKind::IoError.into_error().with_source(e))?;
|
let (buffer, content_type) = massage_data(buffer)?;
|
||||||
if !explicit && !self.binary {
|
if !explicit && !self.binary && content_type.is_binary() {
|
||||||
let content_type = content_inspector::inspect(&buffer);
|
let msg = report::BinaryFile { path };
|
||||||
if content_type.is_binary()
|
reporter.report(msg.into());
|
||||||
// HACK: We only support UTF-8 at the moment
|
return Ok(typos_found);
|
||||||
|| (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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for line in buffer.lines() {
|
for line in buffer.lines() {
|
||||||
|
@ -188,19 +181,12 @@ impl ParseWords {
|
||||||
return Ok(typos_found);
|
return Ok(typos_found);
|
||||||
}
|
}
|
||||||
|
|
||||||
let buffer = std::fs::read(path)
|
let buffer = read_file(path)?;
|
||||||
.map_err(|e| crate::ErrorKind::IoError.into_error().with_source(e))?;
|
let (buffer, content_type) = massage_data(buffer)?;
|
||||||
if !explicit && !self.binary {
|
if !explicit && !self.binary && content_type.is_binary() {
|
||||||
let content_type = content_inspector::inspect(&buffer);
|
let msg = report::BinaryFile { path };
|
||||||
// HACK: We only support UTF-8 at the moment
|
reporter.report(msg.into());
|
||||||
if content_type.is_binary()
|
return Ok(typos_found);
|
||||||
|| (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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for line in buffer.lines() {
|
for line in buffer.lines() {
|
||||||
|
@ -295,20 +281,12 @@ impl Checks {
|
||||||
return Ok(typos_found);
|
return Ok(typos_found);
|
||||||
}
|
}
|
||||||
|
|
||||||
let buffer = std::fs::read(path)
|
let buffer = read_file(path)?;
|
||||||
.map_err(|e| crate::ErrorKind::IoError.into_error().with_source(e))?;
|
let (buffer, content_type) = massage_data(buffer)?;
|
||||||
if !explicit && !self.binary {
|
if !explicit && !self.binary && content_type.is_binary() {
|
||||||
let content_type = content_inspector::inspect(&buffer);
|
let msg = report::BinaryFile { path };
|
||||||
// HACK: We only support UTF-8 at the moment
|
reporter.report(msg.into());
|
||||||
if content_type.is_binary()
|
return Ok(typos_found);
|
||||||
|| (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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (line_idx, line) in buffer.lines().enumerate() {
|
for (line_idx, line) in buffer.lines().enumerate() {
|
||||||
|
@ -355,3 +333,22 @@ impl Checks {
|
||||||
Ok(typos_found)
|
Ok(typos_found)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_file(path: &std::path::Path) -> Result<Vec<u8>, crate::Error> {
|
||||||
|
std::fs::read(path).map_err(|e| crate::ErrorKind::IoError.into_error().with_source(e))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn massage_data(
|
||||||
|
buffer: Vec<u8>,
|
||||||
|
) -> Result<(Vec<u8>, 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))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue