-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathkeys_iterator.rs
More file actions
300 lines (260 loc) · 10 KB
/
keys_iterator.rs
File metadata and controls
300 lines (260 loc) · 10 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//! Definition of `KeysIterator` used for iterating through keys in `KeyedMessage`
use eccodes_sys::codes_keys_iterator;
use fallible_iterator::FallibleIterator;
use log::error;
use std::{marker::PhantomData, ptr::null_mut};
use crate::{
errors::CodesError,
intermediate_bindings::{
codes_keys_iterator_delete, codes_keys_iterator_get_name, codes_keys_iterator_new,
codes_keys_iterator_next,
},
KeyedMessage,
};
/// Structure to iterate through key names in [`KeyedMessage`].
///
/// Mainly useful to discover what keys are present inside the message.
///
/// Implements [`FallibleIterator`] providing similar functionality to classic `Iterator`.
/// `FallibleIterator` is used because internal ecCodes functions can return internal error in some edge-cases.
/// The usage of `FallibleIterator` is sligthly different than usage of `Iterator`,
/// check the documentation for more details.
///
/// To discover key contents you need to [`read_key`](crate::KeyRead::read_key) with name given by the iterator.
///
/// ## Example
///
/// ```
/// use eccodes::{ProductKind, CodesHandle, KeyedMessage, KeysIteratorFlags};
/// # use std::path::Path;
/// # use anyhow::Context;
/// use eccodes::{FallibleIterator, FallibleStreamingIterator};
/// #
/// # fn main() -> anyhow::Result<()> {
/// #
/// let file_path = Path::new("./data/iceland.grib");
/// let product_kind = ProductKind::GRIB;
///
/// let mut handle = CodesHandle::new_from_file(file_path, product_kind)?;
/// let current_message = handle.next()?.context("no message")?;
///
/// let mut keys_iter = current_message.default_keys_iterator()?;
///
/// while let Some(key_name) = keys_iter.next()? {
/// println!("{key_name}");
/// }
/// # Ok(())
/// # }
/// ```
///
/// ## Errors
///
/// The `next()` method will return [`CodesInternal`](crate::errors::CodesInternal)
/// when internal ecCodes function returns non-zero code.
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub struct KeysIterator<'a> {
parent_message: PhantomData<&'a KeyedMessage>,
iterator_handle: *mut codes_keys_iterator,
next_item_exists: bool,
}
/// Flags to specify the subset of keys to iterate over in
/// `KeysIterator`. Flags can be combined as needed.
#[allow(clippy::module_name_repetitions)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum KeysIteratorFlags {
/// Iterate over all keys
AllKeys = eccodes_sys::CODES_KEYS_ITERATOR_ALL_KEYS as isize,
/// Iterate only over dump keys
DumpOnly = eccodes_sys::CODES_KEYS_ITERATOR_DUMP_ONLY as isize,
/// Exclude coded keys from iteration
SkipCoded = eccodes_sys::CODES_KEYS_ITERATOR_SKIP_CODED as isize,
/// Exclude computed keys from iteration
SkipComputed = eccodes_sys::CODES_KEYS_ITERATOR_SKIP_COMPUTED as isize,
/// Exclude function keys from iteration
SkipFunction = eccodes_sys::CODES_KEYS_ITERATOR_SKIP_FUNCTION as isize,
/// Exclude optional keys from iteration
SkipOptional = eccodes_sys::CODES_KEYS_ITERATOR_SKIP_OPTIONAL as isize,
/// Exclude read-only keys from iteration
SkipReadOnly = eccodes_sys::CODES_KEYS_ITERATOR_SKIP_READ_ONLY as isize,
/// Exclude duplicate keys from iteration
SkipDuplicates = eccodes_sys::CODES_KEYS_ITERATOR_SKIP_DUPLICATES as isize,
/// Exclude file edition specific keys from iteration
SkipEditionSpecific = eccodes_sys::CODES_KEYS_ITERATOR_SKIP_EDITION_SPECIFIC as isize,
}
impl KeyedMessage {
/// Creates new [`KeysIterator`] for the message with specified flags and namespace.
///
/// The flags are set by providing any combination of [`KeysIteratorFlags`]
/// inside a slice. Check the documentation for the details of each flag meaning.
///
/// Namespace is set simply as string, eg. `"ls"`, `"time"`, `"parameter"`, `"geography"`, `"statistics"`.
/// Invalid namespace will result in empty iterator.
///
/// # Example
///
/// ```
/// use eccodes::{ProductKind, CodesHandle, KeyedMessage, KeysIteratorFlags};
/// # use std::path::Path;
/// # use anyhow::Context;
/// use eccodes::{FallibleIterator, FallibleStreamingIterator};
/// #
/// # fn main() -> anyhow::Result<()> {
/// #
/// let file_path = Path::new("./data/iceland.grib");
/// let product_kind = ProductKind::GRIB;
///
/// let mut handle = CodesHandle::new_from_file(file_path, product_kind)?;
/// let current_message = handle.next()?.context("no message")?;
///
/// let flags = [
/// KeysIteratorFlags::AllKeys,
/// KeysIteratorFlags::SkipOptional,
/// KeysIteratorFlags::SkipReadOnly,
/// KeysIteratorFlags::SkipDuplicates,
/// ];
///
/// let namespace = "geography";
///
/// let mut keys_iter = current_message.new_keys_iterator(&flags, namespace)?;
///
/// while let Some(key_name) = keys_iter.next()? {
/// println!("{key_name}");
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// This function returns [`CodesInternal`](crate::errors::CodesInternal) when
/// internal ecCodes function returns non-zero code.
pub fn new_keys_iterator<'a>(
&'a self,
flags: &[KeysIteratorFlags],
namespace: &str,
) -> Result<KeysIterator<'a>, CodesError> {
let flags = flags.iter().map(|f| *f as u32).sum();
let iterator_handle =
unsafe { codes_keys_iterator_new(self.message_handle, flags, namespace)? };
let next_item_exists = unsafe { codes_keys_iterator_next(iterator_handle)? };
Ok(KeysIterator {
parent_message: PhantomData,
iterator_handle,
next_item_exists,
})
}
/// Same as [`new_keys_iterator()`](KeyedMessage::new_keys_iterator) but with default
/// parameters: [`AllKeys`](KeysIteratorFlags::AllKeys) flag and `""` namespace,
/// yeilding iterator over all keys in the message.
///
/// # Errors
///
/// This function returns [`CodesInternal`](crate::errors::CodesInternal) when
/// internal ecCodes function returns non-zero code.
pub fn default_keys_iterator(&self) -> Result<KeysIterator<'_>, CodesError> {
let iterator_handle = unsafe { codes_keys_iterator_new(self.message_handle, 0, "")? };
let next_item_exists = unsafe { codes_keys_iterator_next(iterator_handle)? };
Ok(KeysIterator {
parent_message: PhantomData,
iterator_handle,
next_item_exists,
})
}
}
impl FallibleIterator for KeysIterator<'_> {
type Item = String;
type Error = CodesError;
fn next(&mut self) -> Result<Option<Self::Item>, Self::Error> {
if self.next_item_exists {
let key_name;
let next_item_exists;
unsafe {
key_name = codes_keys_iterator_get_name(self.iterator_handle)?;
next_item_exists = codes_keys_iterator_next(self.iterator_handle)?;
}
self.next_item_exists = next_item_exists;
Ok(Some(key_name))
} else {
Ok(None)
}
}
}
#[doc(hidden)]
impl Drop for KeysIterator<'_> {
fn drop(&mut self) {
unsafe {
codes_keys_iterator_delete(self.iterator_handle).unwrap_or_else(|error| {
error!(
"codes_keys_iterator_delete() returned an error: {:?}",
&error
);
});
}
self.iterator_handle = null_mut();
}
}
#[cfg(test)]
mod tests {
use anyhow::{Context, Result};
use crate::codes_handle::{CodesHandle, ProductKind};
use crate::{FallibleIterator, FallibleStreamingIterator};
use std::path::Path;
use super::KeysIteratorFlags;
#[test]
fn keys_iterator_parameters() -> Result<()> {
let file_path = Path::new("./data/iceland.grib");
let product_kind = ProductKind::GRIB;
let mut handle = CodesHandle::new_from_file(file_path, product_kind)?;
let current_message = handle.next()?.context("Message not some")?;
let flags = [
KeysIteratorFlags::AllKeys, //0
KeysIteratorFlags::SkipOptional, //2
KeysIteratorFlags::SkipReadOnly, //1
KeysIteratorFlags::SkipDuplicates, //32
];
let namespace = "geography";
let mut kiter = current_message.new_keys_iterator(&flags, namespace)?;
while let Some(key_name) = kiter.next()? {
assert!(!key_name.is_empty());
}
Ok(())
}
#[test]
fn invalid_namespace() -> Result<()> {
let file_path = Path::new("./data/iceland.grib");
let product_kind = ProductKind::GRIB;
let mut handle = CodesHandle::new_from_file(file_path, product_kind)?;
let current_message = handle.next()?.context("Message not some")?;
let flags = vec![
KeysIteratorFlags::AllKeys, //0
];
let namespace = "blabla";
let mut kiter = current_message.new_keys_iterator(&flags, namespace)?;
while let Some(key_name) = kiter.next()? {
assert!(!key_name.is_empty());
}
Ok(())
}
#[test]
fn destructor() -> Result<()> {
let file_path = Path::new("./data/iceland.grib");
let product_kind = ProductKind::GRIB;
let mut handle = CodesHandle::new_from_file(file_path, product_kind)?;
let current_message = handle.next()?.context("Message not some")?;
let _kiter = current_message.default_keys_iterator()?;
drop(_kiter);
testing_logger::validate(|captured_logs| {
assert_eq!(captured_logs.len(), 1);
assert_eq!(captured_logs[0].body, "codes_keys_iterator_delete");
assert_eq!(captured_logs[0].level, log::Level::Trace);
});
drop(handle);
testing_logger::validate(|captured_logs| {
assert_eq!(captured_logs.len(), 1);
assert_eq!(captured_logs[0].body, "codes_handle_delete");
assert_eq!(captured_logs[0].level, log::Level::Trace);
});
Ok(())
}
}