use crate::{ops::Net, res::Res, FavCoreResult};
use core::future::Future;
use protobuf::MessageFull;
use url::Url;
pub trait PathInfo {
const PATH: &'static str;
}
pub trait ProtoLocal: PathInfo + MessageFull {
fn write(self) -> FavCoreResult<()> {
let path = std::path::PathBuf::from(Self::PATH);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let mut file = std::fs::File::create(path)?;
self.write_to_writer(&mut file)?;
Ok(())
}
fn read() -> FavCoreResult<Self> {
let path = std::path::PathBuf::from(Self::PATH);
let mut file = std::fs::File::open(path)?;
Ok(Self::parse_from_reader(&mut file)?)
}
fn remove() {
let path = std::path::PathBuf::from(Self::PATH);
std::fs::remove_file(path).ok(); }
}
impl<T> ProtoLocal for T where T: PathInfo + MessageFull {}
pub trait SaveLocal: Net {
fn download<R, F, Fut, Any>(
&self,
res: &mut R,
urls: Vec<Url>,
cancelled: F,
) -> impl Future<Output = FavCoreResult<()>>
where
R: Res,
F: FnOnce() -> Fut + Send,
Fut: Future<Output = Any> + Send,
Any: Send;
}