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
#[cfg(feature = "schema")]
44
#[cfg_attr(docsrs, doc(cfg(feature = "schema")))]
45
pub use error::SchemaError;
46
pub use error::{Error, Result};
47
pub use keyring::{Item, Keyring};
48
pub use migration::migrate;
49
#[cfg(feature = "schema")]
50
#[cfg_attr(docsrs, doc(cfg(feature = "schema")))]
51
pub use oo7_macros::SecretSchema;
52
pub use secret::{ContentType, Secret};
53
pub use zbus;
54

            
55
/// A schema attribute.
56
///
57
/// Currently the key, is not really used but would allow
58
/// to map a Rust struct of simple types to an item attributes with type check.
59
pub const XDG_SCHEMA_ATTRIBUTE: &str = "xdg:schema";
60

            
61
/// A content type attribute.
62
///
63
/// Defines the type of the secret stored in the item.
64
pub const CONTENT_TYPE_ATTRIBUTE: &str = "xdg:content-type";
65

            
66
/// An item/collection attributes.
67
pub trait AsAttributes {
68
    fn as_attributes(&self) -> HashMap<String, String>;
69

            
70
4
    fn hash(&self, key: &Key) -> Vec<(String, std::result::Result<Mac, crate::crypto::Error>)> {
71
4
        self.as_attributes()
72
            .into_iter()
73
12
            .map(|(k, v)| (k, crypto::compute_mac(v.as_bytes(), key)))
74
            .collect()
75
    }
76
}
77

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

            
92
        impl<K, V> AsAttributes for &$rust_type
93
        where
94
            K: AsRef<str>,
95
            V: AsRef<str>,
96
        {
97
            fn as_attributes(&self) -> std::collections::HashMap<String, String> {
98
                self.iter()
99
                    .map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
100
                    .collect()
101
            }
102
        }
103
    };
104
}
105

            
106
impl_as_attributes!([(K, V)]);
107
impl_as_attributes!(HashMap<K, V>);
108
impl_as_attributes!(std::collections::BTreeMap<K, V>);
109
impl_as_attributes!(Vec<(K, V)>);
110

            
111
impl<K, V, const N: usize> AsAttributes for [(K, V); N]
112
where
113
    K: AsRef<str>,
114
    V: AsRef<str>,
115
{
116
8
    fn as_attributes(&self) -> HashMap<String, String> {
117
8
        self.iter()
118
24
            .map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
119
            .collect()
120
    }
121
}
122

            
123
// Implementation for references to arrays
124
impl<K, V, const N: usize> AsAttributes for &[(K, V); N]
125
where
126
    K: AsRef<str>,
127
    V: AsRef<str>,
128
{
129
    fn as_attributes(&self) -> HashMap<String, String> {
130
        self.iter()
131
            .map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
132
            .collect()
133
    }
134
}