use std::fs::File;
use std::io::{self, Write};
use std::path::Path;
use crate::error::Result;
use crate::util::create_file;
#[derive(Clone, Debug)]
pub struct CursorWriter<W> {
wtr: W,
pos: usize,
}
impl CursorWriter<io::BufWriter<File>> {
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self> {
let file = create_file(path)?;
Ok(CursorWriter::new(io::BufWriter::new(file)))
}
}
impl<W: io::Write> CursorWriter<W> {
pub fn new(wtr: W) -> CursorWriter<W> {
CursorWriter { wtr, pos: 0 }
}
pub fn position(&self) -> usize {
self.pos
}
pub fn write_u16(&mut self, n: u16) -> io::Result<()> {
self.write_all(&n.to_le_bytes())
}
pub fn write_u32(&mut self, n: u32) -> io::Result<()> {
self.write_all(&n.to_le_bytes())
}
pub fn write_u64(&mut self, n: u64) -> io::Result<()> {
self.write_all(&n.to_le_bytes())
}
}
impl<W: io::Write> io::Write for CursorWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let n = self.wtr.write(buf)?;
self.pos += n;
Ok(n)
}
fn flush(&mut self) -> io::Result<()> {
self.wtr.flush()
}
}