use std::path::Path;
use super::*;
use tokio::fs::{File, OpenOptions};
#[derive(Debug, Clone)]
pub struct FileConnector<P> {
pub path: P,
pub options: OpenOptions,
}
impl<P> FileConnector<P> {
pub fn open(path: P) -> Self {
let mut options = OpenOptions::new();
options.read(true);
Self { path, options }
}
pub fn create(path: P) -> Self {
let mut options = OpenOptions::new();
options.write(true).create(true).truncate(true);
Self { path, options }
}
pub fn create_new(path: P) -> Self {
let mut options = OpenOptions::new();
options.read(true).write(true).create_new(true);
Self { path, options }
}
}
impl<P, R> Tether<FileConnector<P>, R>
where
R: Resolver<FileConnector<P>>,
P: AsRef<Path>,
{
pub async fn connect_file(
connector: FileConnector<P>,
resolver: R,
) -> Result<Self, std::io::Error> {
Tether::connect(connector, resolver).await
}
}
impl<P> Connector for FileConnector<P>
where
P: AsRef<Path>,
{
type Output = File;
fn connect(&mut self) -> PinFut<Result<Self::Output, std::io::Error>> {
let path = self.path.as_ref().to_path_buf();
let options = self.options.clone();
Box::pin(async move { options.open(path).await })
}
}