swamp_asset_reader/
lib.rs

1pub mod debug;
2pub mod file;
3#[cfg(target_arch = "wasm32")]
4pub mod wasm;
5
6#[cfg(not(target_arch = "wasm32"))]
7use crate::file::FileAssetReader;
8
9#[cfg(target_arch = "wasm32")]
10use crate::wasm::HttpWasmAssetReader;
11
12use async_trait::async_trait;
13use std::path::PathBuf;
14
15#[derive(Debug)]
16pub enum AssetReaderError {
17    IoError(std::io::Error),
18    FetchPathError,
19    FetchResponseError,
20    WrongName,
21    NotFound,
22    PathWasNotFound(PathBuf),
23    HttpError(u16),
24}
25
26impl From<std::io::Error> for AssetReaderError {
27    fn from(value: std::io::Error) -> Self {
28        Self::IoError(value)
29    }
30}
31
32#[async_trait(?Send)]
33pub trait AssetReader {
34    async fn fetch_octets(&self, path: PathBuf) -> Result<Vec<u8>, AssetReaderError>;
35}
36
37#[must_use]
38pub fn get_platform_reader(path: &str) -> Box<dyn AssetReader> {
39    #[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
40    return Box::new(FileAssetReader::new(path));
41    #[cfg(target_arch = "wasm32")]
42    return Box::new(HttpWasmAssetReader::new(&_path));
43}