use std::{
fs::File,
io::{Read, Write},
path::{Path, PathBuf},
time::Duration,
};
use anyhow::Result;
use crate::Backend;
pub struct FileBackend {
file: File,
}
impl FileBackend {
pub fn list() -> Result<Vec<PathBuf>> {
Ok(vec![])
}
pub fn open(path: &Path) -> Result<Self> {
let file = File::open(path)?;
Ok(Self {
file
})
}
}
impl Backend for FileBackend {
fn send(&mut self, buf: &[u8], _timeout: Duration) -> anyhow::Result<()> {
self.file.write_all(buf)?;
Ok(())
}
fn recv(&mut self, buf: &mut [u8], _timeout: Duration) -> anyhow::Result<usize> {
let mut nr = 0;
while nr < buf.len() {
let n = self.file.read(&mut buf[nr..])?;
if n == 0 {
break;
}
nr += n;
}
Ok(nr)
}
}