Skip to main content

tilo_secrets/
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    Store,
11    /// Secrets held by the operating system's keychain rather than wrapped
12    /// by the browser ([`Protection::OsKeychain`](crate::Protection::OsKeychain)).
13    OsKeychain,
14    Blocking,
15}
16
17pub(crate) const WEB_KEYCHAIN: &str =
18    "Browsers have no keychain; Tilo encrypts secrets with a key the browser keeps.";
19pub(crate) const WEB_BLOCKING: &str = "The browser's storage answers asynchronously.";
20
21impl tilo_platform_caps::Feature for Feature {
22    /// The feature's availability on Linux (and Windows), macOS and the web, in that
23    /// order. A Linux session without a Secret Service provides less, and the macOS
24    /// Keychain may ask the user to allow access.
25    fn targets(self) -> [Availability; 3] {
26        use Availability::{Available, Unsupported};
27        match self {
28            Self::Store => [Available, Available, Available],
29            Self::OsKeychain => [Available, Available, Unsupported(WEB_KEYCHAIN)],
30            Self::Blocking => [Available, Available, Unsupported(WEB_BLOCKING)],
31        }
32    }
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38    use tilo_platform_caps::Feature as _;
39
40    #[test]
41    fn this_target_reads_its_own_column() {
42        assert_eq!(availability(Feature::Store), Availability::Available);
43        let web = Feature::Blocking.targets()[2];
44        assert_eq!(web, Availability::Unsupported(WEB_BLOCKING));
45    }
46}