1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(rustdoc::broken_intra_doc_links)]
#![doc = include_str!("../README.md")]
#[cfg(all(all(feature = "tokio", feature = "async-std"), not(doc)))]
compile_error!("You can't enable both async-std & tokio features at once");

use std::collections::{BTreeMap, HashMap};

mod error;
mod key;
mod migration;

#[cfg(feature = "unstable")]
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
pub use key::Key;
#[cfg(not(feature = "unstable"))]
pub(crate) use key::Key;

mod crypto;
pub mod dbus;
pub mod portal;

mod helpers;
mod keyring;

pub use error::{Error, Result};
pub use keyring::{Item, Keyring};
pub use migration::migrate;
pub use zbus;

/// Checks whether the application is sandboxed or not.
pub async fn is_sandboxed() -> bool {
    helpers::is_flatpak().await || helpers::is_snap().await
}

/// An item/collection attributes.
pub trait AsAttributes {
    fn as_attributes(&self) -> HashMap<&str, &str>;

    fn hash<'a>(&'a self, key: &Key) -> Vec<(&'a str, zeroize::Zeroizing<Vec<u8>>)> {
        self.as_attributes()
            .into_iter()
            .map(|(k, v)| (k, crate::portal::AttributeValue::from(v).mac(key)))
            .collect()
    }
}

impl<K, V> AsAttributes for &HashMap<K, V>
where
    K: AsRef<str>,
    V: AsRef<str>,
{
    fn as_attributes(&self) -> HashMap<&str, &str> {
        self.iter().map(|(k, v)| (k.as_ref(), v.as_ref())).collect()
    }
}

impl<K, V> AsAttributes for HashMap<K, V>
where
    K: AsRef<str>,
    V: AsRef<str>,
{
    fn as_attributes(&self) -> HashMap<&str, &str> {
        self.iter().map(|(k, v)| (k.as_ref(), v.as_ref())).collect()
    }
}

impl<K, V> AsAttributes for BTreeMap<K, V>
where
    K: AsRef<str>,
    V: AsRef<str>,
{
    fn as_attributes(&self) -> HashMap<&str, &str> {
        self.iter().map(|(k, v)| (k.as_ref(), v.as_ref())).collect()
    }
}

impl<K, V> AsAttributes for &BTreeMap<K, V>
where
    K: AsRef<str>,
    V: AsRef<str>,
{
    fn as_attributes(&self) -> HashMap<&str, &str> {
        self.iter().map(|(k, v)| (k.as_ref(), v.as_ref())).collect()
    }
}

impl<K, V> AsAttributes for Vec<(K, V)>
where
    K: AsRef<str>,
    V: AsRef<str>,
{
    fn as_attributes(&self) -> HashMap<&str, &str> {
        self.iter().map(|(k, v)| (k.as_ref(), v.as_ref())).collect()
    }
}

impl<K, V> AsAttributes for &Vec<(K, V)>
where
    K: AsRef<str>,
    V: AsRef<str>,
{
    fn as_attributes(&self) -> HashMap<&str, &str> {
        self.iter().map(|(k, v)| (k.as_ref(), v.as_ref())).collect()
    }
}