use std::io::BufRead;
use std::path::Path;
use super::Digest;
use crate::load::open;
pub(super) fn canonicalise(path: &Path, digest: &mut Digest) -> Result<(), String> {
let mut reader = open(path).map_err(|error| error.to_string())?;
let mut line = String::new();
let mut found = false;
loop {
line.clear();
if reader
.read_line(&mut line)
.map_err(|error| error.to_string())?
== 0
{
break;
}
let text = line.trim_end_matches(['\n', '\r']);
if text.is_empty() || is_decoration(text) {
continue;
}
let fields: Vec<&str> = text.split('\t').collect();
if fields.len() < 3 {
return Err(format!(
"a line with {} columns, not 3 or more",
fields.len()
));
}
if fields[1].parse::<u64>().is_err() || fields[2].parse::<u64>().is_err() {
return Err("a start or end that is not a number".to_owned());
}
let parts: Vec<&[u8]> = fields.iter().map(|field| field.as_bytes()).collect();
digest.record(&parts);
found = true;
}
if !found {
return Err("no intervals at all".to_owned());
}
Ok(())
}
fn is_decoration(line: &str) -> bool {
line.starts_with('#') || line.starts_with("track") || line.starts_with("browser")
}