Skip to main content

tilo_paths/
caps.rs

1//! What this crate provides on each target, as data an app can query.
2
3pub use tilo_platform_caps::{Availability, availability, available};
4
5pub type Unsupported = tilo_platform_caps::Unsupported<Feature>;
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
8#[non_exhaustive]
9pub enum Feature {
10    AppStorage,
11    Runtime,
12    UserDirs,
13    WorkspaceRoot,
14    FontRoot,
15    NativePaths,
16}
17
18pub(crate) const WEB_USER_DIRS: &str = "Browsers grant files through pickers, not folders.";
19pub(crate) const WEB_WORKSPACE: &str = "Browsers have no mounted workspace.";
20pub(crate) const WEB_FONTS: &str = "The web bundle delivers its fonts as data, not a folder.";
21pub(crate) const WEB_NATIVE_PATHS: &str = "Browser storage has no file system paths.";
22
23impl tilo_platform_caps::Feature for Feature {
24    /// The feature's availability on Linux, macOS and the web, in that
25    /// order. A host may provide less at run time (no `$XDG_RUNTIME_DIR`, no
26    /// workspace mounted), never more.
27    fn targets(self) -> [Availability; 3] {
28        use Availability::{Available, Unsupported};
29        match self {
30            Self::AppStorage | Self::Runtime => [Available, Available, Available],
31            Self::UserDirs => [Available, Available, Unsupported(WEB_USER_DIRS)],
32            Self::WorkspaceRoot => [Available, Available, Unsupported(WEB_WORKSPACE)],
33            Self::FontRoot => [Available, Available, Unsupported(WEB_FONTS)],
34            Self::NativePaths => [Available, Available, Unsupported(WEB_NATIVE_PATHS)],
35        }
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42    use tilo_platform_caps::Feature as _;
43
44    #[test]
45    fn this_target_reads_its_own_column() {
46        assert_eq!(availability(Feature::AppStorage), Availability::Available);
47        let web = Feature::UserDirs.targets()[2];
48        assert_eq!(web, Availability::Unsupported(WEB_USER_DIRS));
49        assert_eq!(availability(Feature::UserDirs), Availability::Available);
50    }
51}