kanidmd_lib/be/
idxkey.rs

1use crate::prelude::entries::Attribute;
2use crate::value::IndexType;
3use std::borrow::Borrow;
4use std::cmp::Ordering;
5use std::hash::{Hash, Hasher};
6
7pub type IdxSlope = u8;
8
9// Huge props to https://github.com/sunshowers/borrow-complex-key-example/blob/master/src/lib.rs
10
11#[derive(Debug, Clone, PartialEq, Eq, Hash)]
12pub struct IdxKey {
13    pub attr: Attribute,
14    pub itype: IndexType,
15}
16
17impl IdxKey {
18    pub fn new(attr: Attribute, itype: IndexType) -> Self {
19        IdxKey { attr, itype }
20    }
21}
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
24pub struct IdxKeyRef<'a> {
25    pub attr: &'a Attribute,
26    pub itype: &'a IndexType,
27}
28
29impl<'a> IdxKeyRef<'a> {
30    pub fn new(attr: &'a Attribute, itype: &'a IndexType) -> Self {
31        IdxKeyRef { attr, itype }
32    }
33
34    pub fn as_key(&self) -> IdxKey {
35        IdxKey {
36            attr: self.attr.clone(),
37            itype: *self.itype,
38        }
39    }
40}
41
42pub trait IdxKeyToRef {
43    fn keyref(&self) -> IdxKeyRef<'_>;
44}
45
46impl IdxKeyToRef for IdxKeyRef<'_> {
47    fn keyref(&self) -> IdxKeyRef<'_> {
48        // Copy the self.
49        *self
50    }
51}
52
53impl IdxKeyToRef for IdxKey {
54    fn keyref(&self) -> IdxKeyRef<'_> {
55        IdxKeyRef {
56            attr: &self.attr,
57            itype: &self.itype,
58        }
59    }
60}
61
62impl<'a> Borrow<dyn IdxKeyToRef + 'a> for IdxKey {
63    fn borrow(&self) -> &(dyn IdxKeyToRef + 'a) {
64        self
65    }
66}
67
68impl PartialEq for (dyn IdxKeyToRef + '_) {
69    fn eq(&self, other: &Self) -> bool {
70        self.keyref().eq(&other.keyref())
71    }
72}
73
74impl Eq for (dyn IdxKeyToRef + '_) {}
75
76impl Hash for (dyn IdxKeyToRef + '_) {
77    fn hash<H: Hasher>(&self, state: &mut H) {
78        self.keyref().hash(state)
79    }
80}
81
82// ===== idlcachekey ======
83
84#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
85pub struct IdlCacheKey {
86    pub a: Attribute,
87    pub i: IndexType,
88    pub k: String,
89}
90
91#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash)]
92pub struct IdlCacheKeyRef<'a> {
93    pub a: &'a Attribute,
94    pub i: IndexType,
95    pub k: &'a str,
96}
97
98/*
99impl<'a> IdlCacheKeyRef<'a> {
100    pub fn new(a: &'a str, i: &'a IndexType, k: &'a str) -> Self {
101        IdlCacheKeyRef { a, i, k }
102    }
103}
104*/
105
106pub trait IdlCacheKeyToRef {
107    fn keyref(&self) -> IdlCacheKeyRef<'_>;
108}
109
110impl IdlCacheKeyToRef for IdlCacheKeyRef<'_> {
111    fn keyref(&self) -> IdlCacheKeyRef<'_> {
112        // Copy the self
113        *self
114    }
115}
116
117impl IdlCacheKeyToRef for IdlCacheKey {
118    fn keyref(&self) -> IdlCacheKeyRef<'_> {
119        IdlCacheKeyRef {
120            a: &self.a,
121            i: self.i,
122            k: self.k.as_str(),
123        }
124    }
125}
126
127impl<'a> Borrow<dyn IdlCacheKeyToRef + 'a> for IdlCacheKey {
128    fn borrow(&self) -> &(dyn IdlCacheKeyToRef + 'a) {
129        self
130    }
131}
132
133impl PartialEq for (dyn IdlCacheKeyToRef + '_) {
134    fn eq(&self, other: &Self) -> bool {
135        self.keyref().eq(&other.keyref())
136    }
137}
138
139impl Eq for (dyn IdlCacheKeyToRef + '_) {}
140
141impl Hash for (dyn IdlCacheKeyToRef + '_) {
142    fn hash<H: Hasher>(&self, state: &mut H) {
143        self.keyref().hash(state)
144    }
145}
146
147impl PartialOrd for (dyn IdlCacheKeyToRef + '_) {
148    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
149        Some(self.cmp(&other.keyref()))
150    }
151}
152
153impl Ord for (dyn IdlCacheKeyToRef + '_) {
154    fn cmp(&self, other: &Self) -> Ordering {
155        self.keyref().cmp(&other.keyref())
156    }
157}
158
159#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
160pub struct IdxNameKey {
161    pub a: Attribute,
162    pub i: IndexType,
163}