use anyhow::{Context, Result};
use std::fs;
use std::path::Path;
const IGNORE_MARKER: &[u8] = b"linecheck\x3aignore";
pub fn file_info(path: &Path) -> Result<(usize, bool)> {
let data = fs::read(path).with_context(|| format!("reading {}", path.display()))?;
Ok(content_info(&data))
}
#[must_use]
pub fn content_info(data: &[u8]) -> (usize, bool) {
let ignored = content_inspector::inspect(data).is_binary()
|| data
.windows(IGNORE_MARKER.len())
.any(|w| w == IGNORE_MARKER);
(count_newlines(data), ignored)
}
#[must_use]
pub fn count_newlines(data: &[u8]) -> usize {
if data.is_empty() {
return 0;
}
let newlines = data.iter().filter(|&&b| b == b'\n').count();
if data.last() != Some(&b'\n') {
newlines + 1
} else {
newlines
}
}
#[cfg(test)]
#[path = "lines_tests.rs"]
mod tests;