1
use std::ops::Deref;
2

            
3
use serde::{Deserialize, Serialize};
4
use zbus::zvariant::Type;
5
use zeroize::{Zeroize, ZeroizeOnDrop};
6

            
7
use crate::{Key, Mac, crypto};
8

            
9
/// An attribute value.
10
#[derive(Deserialize, Serialize, Type, Clone, Debug, Eq, PartialEq, Zeroize, ZeroizeOnDrop)]
11
pub struct AttributeValue(String);
12

            
13
impl AttributeValue {
14
4
    pub(crate) fn mac(&self, key: &Key) -> Result<Mac, crate::crypto::Error> {
15
4
        crypto::compute_mac(self.0.as_bytes(), key)
16
    }
17
}
18

            
19
impl<S: ToString> From<S> for AttributeValue {
20
4
    fn from(value: S) -> Self {
21
8
        Self(value.to_string())
22
    }
23
}
24

            
25
impl AsRef<str> for AttributeValue {
26
4
    fn as_ref(&self) -> &str {
27
4
        self.0.as_str()
28
    }
29
}
30

            
31
impl Deref for AttributeValue {
32
    type Target = str;
33
4
    fn deref(&self) -> &Self::Target {
34
4
        self.0.as_str()
35
    }
36
}
37

            
38
impl PartialEq<str> for AttributeValue {
39
2
    fn eq(&self, other: &str) -> bool {
40
2
        self.deref() == other
41
    }
42
}
43

            
44
impl PartialEq<&str> for AttributeValue {
45
    fn eq(&self, other: &&str) -> bool {
46
        self.deref() == *other
47
    }
48
}