pub mod cdn;
pub mod ggpk;
pub mod steam;
use std::{
borrow::Cow,
path::{Path, PathBuf},
};
use bytes::Bytes;
use cdn::CDNFS;
use enum_dispatch::enum_dispatch;
use steam::SteamFS;
use url::Url;
use crate::fs::ggpk::GGPKBundleFS;
#[enum_dispatch]
pub trait FileSystem {
fn list(&self) -> Box<dyn Iterator<Item = String> + '_>;
#[allow(clippy::type_complexity)]
fn batch_read<'a>(
&'a self,
paths: &'a [impl AsRef<str>],
) -> Box<dyn Iterator<Item = (Cow<'a, str>, anyhow::Result<Bytes>)> + 'a>;
fn read(&self, path: &str) -> anyhow::Result<Bytes>;
}
#[enum_dispatch(FileSystem)]
pub enum FS {
Steam(SteamFS),
CDN(CDNFS),
GGPK(GGPKBundleFS),
}
impl FS {
pub fn from_steam(steam_folder: PathBuf) -> anyhow::Result<Self> {
SteamFS::new(steam_folder).map(Self::Steam)
}
pub fn from_cdn(base_url: &Url, cache_dir: &Path) -> anyhow::Result<FS> {
CDNFS::new(base_url, cache_dir).map(Self::CDN)
}
pub fn from_ggpk(ggpk_path: &Path) -> anyhow::Result<FS> {
GGPKBundleFS::new(ggpk_path).map(Self::GGPK)
}
}