#[cfg(all(feature = "native", feature = "browser"))]
compile_error!(
"features `native` and `browser` are mutually exclusive; \
use --no-default-features --features browser for browser/WASM builds"
);
pub mod config_loader;
pub mod env;
pub mod fs;
pub mod http;
pub mod process;
#[cfg(feature = "browser")]
pub mod browser;
#[cfg(feature = "browser")]
pub use browser::BrowserPlatform;
use async_trait::async_trait;
#[cfg_attr(not(feature = "browser"), async_trait)]
#[cfg_attr(feature = "browser", async_trait(?Send))]
pub trait Platform: Send + Sync {
fn http(&self) -> &dyn http::HttpClient;
fn fs(&self) -> &dyn fs::FileSystem;
fn env(&self) -> &dyn env::Environment;
fn process(&self) -> Option<&dyn process::ProcessSpawner>;
}
#[cfg(feature = "native")]
pub struct NativePlatform {
http: http::NativeHttpClient,
fs: fs::NativeFileSystem,
env: env::NativeEnvironment,
process: process::NativeProcessSpawner,
}
#[cfg(feature = "native")]
impl NativePlatform {
pub fn new() -> Self {
Self {
http: http::NativeHttpClient::new(),
fs: fs::NativeFileSystem,
env: env::NativeEnvironment,
process: process::NativeProcessSpawner,
}
}
}
#[cfg(feature = "native")]
impl Default for NativePlatform {
fn default() -> Self {
Self::new()
}
}
#[cfg(feature = "native")]
#[async_trait]
impl Platform for NativePlatform {
fn http(&self) -> &dyn http::HttpClient {
&self.http
}
fn fs(&self) -> &dyn fs::FileSystem {
&self.fs
}
fn env(&self) -> &dyn env::Environment {
&self.env
}
fn process(&self) -> Option<&dyn process::ProcessSpawner> {
Some(&self.process)
}
}
#[cfg(all(test, feature = "native"))]
mod tests {
use super::*;
#[test]
fn test_native_platform_creation() {
let platform = NativePlatform::new();
let _http = platform.http();
let _fs = platform.fs();
let _env = platform.env();
assert!(platform.process().is_some());
}
#[test]
fn test_native_platform_default() {
let platform = NativePlatform::default();
assert!(platform.process().is_some());
}
#[test]
fn test_platform_is_send_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<NativePlatform>();
}
}