mirror of
https://github.com/crate-ci/typos.git
synced 2024-11-25 18:41:05 -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,20 +112,13 @@ 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);
|
|
||||||
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 };
|
let msg = report::BinaryFile { path };
|
||||||
reporter.report(msg.into());
|
reporter.report(msg.into());
|
||||||
return Ok(typos_found);
|
return Ok(typos_found);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for line in buffer.lines() {
|
for line in buffer.lines() {
|
||||||
let msg = report::Parse {
|
let msg = report::Parse {
|
||||||
|
@ -188,20 +181,13 @@ 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);
|
|
||||||
// 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 };
|
let msg = report::BinaryFile { path };
|
||||||
reporter.report(msg.into());
|
reporter.report(msg.into());
|
||||||
return Ok(typos_found);
|
return Ok(typos_found);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for line in buffer.lines() {
|
for line in buffer.lines() {
|
||||||
let msg = report::Parse {
|
let msg = report::Parse {
|
||||||
|
@ -295,21 +281,13 @@ 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);
|
|
||||||
// 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 };
|
let msg = report::BinaryFile { path };
|
||||||
reporter.report(msg.into());
|
reporter.report(msg.into());
|
||||||
return Ok(typos_found);
|
return Ok(typos_found);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for (line_idx, line) in buffer.lines().enumerate() {
|
for (line_idx, line) in buffer.lines().enumerate() {
|
||||||
let line_num = line_idx + 1;
|
let line_num = line_idx + 1;
|
||||||
|
@ -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