use std::io;
use std::path::Path;
use crate::vfs::Vfs;
const FIRST_WINDOW_BYTES: u64 = 4096;
pub(crate) fn first_line(vfs: &dyn Vfs, path: &Path) -> io::Result<Option<Vec<u8>>> {
let mut line = Vec::new();
let mut window = FIRST_WINDOW_BYTES;
loop {
let offset = u64::try_from(line.len()).unwrap_or(u64::MAX);
let chunk = vfs.read_at(path, offset, window)?;
if let Some(end) = chunk.iter().position(|byte| *byte == b'\n') {
line.extend(chunk.iter().take(end));
return Ok(Some(line));
}
if u64::try_from(chunk.len()).unwrap_or(u64::MAX) < window {
return Ok(None);
}
line.extend_from_slice(&chunk);
window = window.saturating_mul(2);
}
}