use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, PoisonError};
use crate::error::IndexError;
use crate::live::decode;
#[derive(Debug, Default)]
pub struct Sources {
cache: Mutex<HashMap<PathBuf, Arc<Vec<String>>>>,
}
impl Sources {
pub fn new() -> Self {
Self::default()
}
pub fn lines(&self, path: impl AsRef<Path>) -> Result<Arc<Vec<String>>, IndexError> {
let path = path.as_ref();
if let Some(lines) = self.locked().get(path) {
return Ok(Arc::clone(lines));
}
let bytes = fs::read(path).map_err(|e| IndexError::io(path, &e))?;
let lines = Arc::new(split_lines(&bytes));
self.locked().insert(path.to_path_buf(), Arc::clone(&lines));
Ok(lines)
}
pub fn is_cached(&self, path: impl AsRef<Path>) -> bool {
self.locked().contains_key(path.as_ref())
}
pub fn cached_paths(&self) -> Vec<PathBuf> {
let mut paths: Vec<PathBuf> = self.locked().keys().cloned().collect();
paths.sort();
paths
}
fn locked(&self) -> std::sync::MutexGuard<'_, HashMap<PathBuf, Arc<Vec<String>>>> {
self.cache.lock().unwrap_or_else(PoisonError::into_inner)
}
}
pub fn split_lines(bytes: &[u8]) -> Vec<String> {
let text = decode(bytes);
if text.is_empty() {
return Vec::new();
}
let mut lines: Vec<String> = text
.split('\n')
.map(|line| line.strip_suffix('\r').unwrap_or(line).to_string())
.collect();
if text.ends_with('\n') {
lines.pop();
}
lines
}
#[cfg(test)]
mod tests {
use super::split_lines;
#[test]
fn lines_drop_the_final_newline_and_the_carriage_return() {
assert_eq!(split_lines(b"a\r\nb\n"), ["a", "b"]);
assert_eq!(split_lines(b"a\n\nb"), ["a", "", "b"]);
assert!(split_lines(b"").is_empty());
assert_eq!(split_lines(b"\xe9t\xe9\n"), ["\u{e9}t\u{e9}"]);
}
}