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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use crate::{KeyNotFoundError, ReadOnly, StringKey};
use std::borrow::Cow;
use std::convert::TryInto;
use std::ffi::{CStr, CString};
pub trait ReadableKey: AsRef<elektra_sys::Key> + PartialEq + Eq + PartialOrd + Ord {
/// The type returned by value.
type GetValue;
/// Returns the value this key holds.
/// # Examples
/// ```
/// # use elektra::{StringKey,WriteableKey,ReadableKey};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut key = StringKey::new("user:/sw/app")?;
/// key.set_value("myvalue");
/// assert_eq!(key.value(), "myvalue");
/// #
/// # Ok(())
/// # }
/// ```
fn value(&self) -> Self::GetValue;
/// Construct a new key from a raw key pointer
unsafe fn from_ptr(ptr: *mut elektra_sys::Key) -> Self
where
Self: Sized;
/// Return the name of the key as a borrowed slice.
fn name(&self) -> Cow<str> {
let c_str = unsafe { CStr::from_ptr(elektra_sys::keyName(self.as_ref())) };
c_str.to_string_lossy()
}
/// Return the basename of the key as a borrowed slice.
fn basename(&self) -> Cow<str> {
let c_str = unsafe { CStr::from_ptr(elektra_sys::keyBaseName(self.as_ref())) };
c_str.to_string_lossy()
}
/// Calculates number of bytes needed to store basename of key.
fn basename_size(&self) -> isize {
unsafe { elektra_sys::keyGetBaseNameSize(self.as_ref()) }
}
/// Return how many references the key has.
fn get_ref(&self) -> u16 {
unsafe { elektra_sys::keyGetRef(self.as_ref()) }
}
/// Returns the namespace of the name of this key.
/// Note that there are some convenience methods implemented.
///
/// # Examples
/// ```
/// # use elektra::{BinaryKey,WriteableKey,ReadableKey};
/// # use elektra_sys;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut key = BinaryKey::new("user:/sw/app")?;
/// assert_eq!(key.namespace(), elektra_sys::KEY_NS_USER);
/// #
/// # Ok(())
/// # }
/// ```
fn namespace(&self) -> u32 {
unsafe { elektra_sys::keyGetNamespace(self.as_ref()) as u32 }
}
/// Determines if the key is in the spec namespace
fn is_spec(&self) -> bool {
self.namespace() == elektra_sys::KEY_NS_SPEC
}
/// Determines if the key is in the dir namespace
fn is_dir(&self) -> bool {
self.namespace() == elektra_sys::KEY_NS_DIR
}
/// Determines if the key is in the proc namespace
fn is_proc(&self) -> bool {
self.namespace() == elektra_sys::KEY_NS_PROC
}
/// Determines if the key is in the user namespace
fn is_user(&self) -> bool {
self.namespace() == elektra_sys::KEY_NS_USER
}
/// Determines if the key is in the system namespace
fn is_system(&self) -> bool {
self.namespace() == elektra_sys::KEY_NS_SYSTEM
}
/// Determines if the key is a cascading key
fn is_cascading(&self) -> bool {
self.namespace() == elektra_sys::KEY_NS_CASCADING
}
/// Returns the number of bytes needed to store the key's value, including the
/// NULL terminator.
///
/// # Examples
/// ```
/// # use elektra::{BinaryKey,WriteableKey,ReadableKey};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut key = BinaryKey::new("user:/sw/app")?;
/// key.set_value(b"12345");
/// assert_eq!(key.value_size(), 5);
/// #
/// # Ok(())
/// # }
/// ```
fn value_size(&self) -> usize {
let ret_val = unsafe { elektra_sys::keyGetValueSize(self.as_ref()) };
// keyGetValueSize returns -1 on null pointers, but we can be sure self.ptr is valid
// so this conversion is safe
ret_val.try_into().unwrap()
}
/// Returns true if the key has a binary value.
///
/// # Notes
/// Note that this does not return true for a newly created BinaryKey,
/// but only when actual binary data has been set, due to the underlying
/// generic Key.
///
/// # Examples
/// ```
/// # use elektra::{BinaryKey,WriteableKey,ReadableKey};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut key = BinaryKey::new("user:/sw/app")?;
/// key.set_value(b"");
/// assert!(key.is_binary());
/// #
/// # Ok(())
/// # }
/// ```
fn is_binary(&self) -> bool {
unsafe { elektra_sys::keyIsBinary(self.as_ref()) == 1 }
}
/// Returns true if the key has a string value.
///
/// # Examples
/// ```
/// # use elektra::{StringKey,WriteableKey,ReadableKey};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let key = StringKey::new("user:/sw/app")?;
/// assert!(key.is_string());
/// #
/// # Ok(())
/// # }
/// ```
fn is_string(&self) -> bool {
unsafe { elektra_sys::keyIsString(self.as_ref()) == 1 }
}
/// Returns true if other is below self
///
/// # Examples
/// ```
/// # use elektra::{StringKey,WriteableKey,ReadableKey};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let key = StringKey::new("user:/sw/app")?;
/// let key2 = StringKey::new("user:/sw/app/folder/key")?;
/// assert!(key2.is_below(&key));
/// #
/// # Ok(())
/// # }
/// ```
fn is_below(&self, other: &Self) -> bool
where
Self: Sized,
{
unsafe { elektra_sys::keyIsBelow(other.as_ref(), self.as_ref()) == 1 }
}
/// Returns true if other is *directly* below self
///
/// # Examples
/// ```
/// # use elektra::{StringKey,WriteableKey,ReadableKey};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let key = StringKey::new("user:/sw/app")?;
/// let key2 = StringKey::new("user:/sw/app/key")?;
/// assert!(key2.is_directly_below(&key));
/// #
/// # Ok(())
/// # }
/// ```
fn is_directly_below(&self, other: &Self) -> bool
where
Self: Sized,
{
unsafe { elektra_sys::keyIsDirectlyBelow(other.as_ref(), self.as_ref()) == 1 }
}
/// Returns the metadata with the given metaname
///
/// # Errors
/// Returns `KeyNotFoundError` if no metakey with the given name was found.
///
/// # Panics
/// Panics if the provided string contains interior nul bytes.
fn meta(&self, metaname: &str) -> Result<ReadOnly<StringKey<'_>>, KeyNotFoundError>
where
Self: Sized,
{
let cstr = CString::new(metaname).unwrap();
let key_ptr = unsafe { elektra_sys::keyGetMeta(self.as_ref(), cstr.as_ptr()) };
if key_ptr.is_null() {
Err(KeyNotFoundError::new(metaname.to_owned()))
} else {
let key: ReadOnly<StringKey<'_>> = unsafe { ReadOnly::from_ptr(key_ptr as *mut elektra_sys::Key) };
Ok(key)
}
}
}