1
#![cfg_attr(docsrs, feature(doc_cfg))]
2
#![deny(rustdoc::broken_intra_doc_links)]
3
#![doc = include_str!("../README.md")]
4
#[cfg(all(all(feature = "tokio", feature = "async-std"), not(doc)))]
5
compile_error!("You can't enable both async-std & tokio features at once");
6
#[cfg(all(not(feature = "tokio"), not(feature = "async-std"), not(doc)))]
7
compile_error!("You have to enable either tokio or async-std feature");
8
#[cfg(all(all(feature = "native_crypto", feature = "openssl_crypto"), not(doc)))]
9
compile_error!("You can't enable both openssl_crypto & native_crypto features at once");
10
#[cfg(all(
11
    not(feature = "native_crypto"),
12
    not(feature = "openssl_crypto"),
13
    not(doc)
14
))]
15
compile_error!("You have to enable either openssl_crypto or native_crypto feature");
16

            
17
use std::collections::HashMap;
18

            
19
mod error;
20
mod key;
21
mod mac;
22
mod migration;
23

            
24
#[cfg(feature = "unstable")]
25
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
26
pub use key::Key;
27
#[cfg(not(feature = "unstable"))]
28
pub(crate) use key::Key;
29
pub use mac::Mac;
30

            
31
#[cfg(not(feature = "unstable"))]
32
mod crypto;
33
#[cfg(feature = "unstable")]
34
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
35
pub mod crypto;
36
pub mod dbus;
37
pub mod file;
38

            
39
mod keyring;
40
mod secret;
41

            
42
pub use ashpd;
43
pub use error::{Error, Result};
44
pub use keyring::{Item, Keyring};
45
pub use migration::migrate;
46
pub use secret::{ContentType, Secret};
47
pub use zbus;
48

            
49
/// A schema attribute.
50
///
51
/// Currently the key, is not really used but would allow
52
/// to map a Rust struct of simple types to an item attributes with type check.
53
pub const XDG_SCHEMA_ATTRIBUTE: &str = "xdg:schema";
54

            
55
/// A content type attribute.
56
///
57
/// Defines the type of the secret stored in the item.
58
pub const CONTENT_TYPE_ATTRIBUTE: &str = "xdg:content-type";
59

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

            
64
    #[allow(clippy::type_complexity)]
65
12
    fn hash<'a>(
66
        &'a self,
67
        key: &Key,
68
    ) -> Vec<(&'a str, std::result::Result<Mac, crate::crypto::Error>)> {
69
12
        self.as_attributes()
70
            .into_iter()
71
36
            .map(|(k, v)| (k, crate::file::AttributeValue::from(v).mac(key)))
72
            .collect()
73
    }
74
}
75

            
76
macro_rules! impl_as_attributes {
77
    ($rust_type:ty) => {
78
        impl<K, V> AsAttributes for $rust_type
79
        where
80
            K: AsRef<str>,
81
            V: AsRef<str>,
82
        {
83
18
            fn as_attributes(&self) -> std::collections::HashMap<&str, &str> {
84
47
                self.iter().map(|(k, v)| (k.as_ref(), v.as_ref())).collect()
85
            }
86
        }
87

            
88
        impl<K, V> AsAttributes for &$rust_type
89
        where
90
            K: AsRef<str>,
91
            V: AsRef<str>,
92
        {
93
2
            fn as_attributes(&self) -> std::collections::HashMap<&str, &str> {
94
6
                self.iter().map(|(k, v)| (k.as_ref(), v.as_ref())).collect()
95
            }
96
        }
97
    };
98
}
99

            
100
impl_as_attributes!([(K, V)]);
101
impl_as_attributes!(HashMap<K, V>);
102
impl_as_attributes!(std::collections::BTreeMap<K, V>);
103
impl_as_attributes!(Vec<(K, V)>);
104

            
105
impl<K, V, const N: usize> AsAttributes for [(K, V); N]
106
where
107
    K: AsRef<str>,
108
    V: AsRef<str>,
109
{
110
14
    fn as_attributes(&self) -> HashMap<&str, &str> {
111
42
        self.iter().map(|(k, v)| (k.as_ref(), v.as_ref())).collect()
112
    }
113
}
114

            
115
// Implementation for references to arrays
116
impl<K, V, const N: usize> AsAttributes for &[(K, V); N]
117
where
118
    K: AsRef<str>,
119
    V: AsRef<str>,
120
{
121
2
    fn as_attributes(&self) -> HashMap<&str, &str> {
122
6
        self.iter().map(|(k, v)| (k.as_ref(), v.as_ref())).collect()
123
    }
124
}