Skip to content

Commit

Permalink
initial preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
Quba1 committed Feb 3, 2024
1 parent 166d701 commit f5978dd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ errno = "0.3"
num-derive = "0.4.1"
num-traits = "0.2"
fallible-iterator = "0.3"
fallible-streaming-iterator = "0.1.9"

[dev-dependencies]
reqwest = { version = "0.11", features = ["rustls-tls"] }
Expand Down
47 changes: 46 additions & 1 deletion src/codes_handle/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::ptr;

use eccodes_sys::codes_handle;
use fallible_iterator::FallibleIterator;
use fallible_streaming_iterator::FallibleStreamingIterator;

use crate::{
codes_handle::{CodesHandle, KeyedMessage},
Expand Down Expand Up @@ -138,6 +141,44 @@ impl FallibleIterator for CodesHandle<CodesIndex> {
}
}

impl FallibleStreamingIterator for CodesHandle<GribFile> {
type Item = KeyedMessage;

type Error = CodesError;

fn advance(&mut self) -> Result<(), Self::Error> {
unsafe {
codes_handle_delete(self.unsafe_message.message_handle)?;
}

// nullify message handle so that destructor is harmless
// it might be excessive but it follows the correct pattern
self.unsafe_message.message_handle = ptr::null_mut();

let new_eccodes_handle =
unsafe { codes_handle_new_from_file(self.source.pointer, self.product_kind)? };

self.unsafe_message = KeyedMessage {
message_handle: new_eccodes_handle,
iterator_flags: None,
iterator_namespace: None,
keys_iterator: None,
keys_iterator_next_item_exists: false,
nearest_handle: None,
};

Ok(())
}

fn get(&self) -> Option<&Self::Item> {
if self.unsafe_message.message_handle.is_null() {
None
} else {
Some(&self.unsafe_message)
}
}
}

fn get_message_from_handle(handle: *mut codes_handle) -> KeyedMessage {
let new_handle;
let new_buffer;
Expand All @@ -163,7 +204,7 @@ fn get_message_from_handle(handle: *mut codes_handle) -> KeyedMessage {
#[cfg(test)]
mod tests {
use crate::codes_handle::{CodesHandle, KeyType, KeyedMessage, ProductKind};
use crate::FallibleIterator;
use fallible_streaming_iterator::FallibleStreamingIterator;
use std::path::Path;

#[test]
Expand All @@ -181,7 +222,11 @@ mod tests {
_ => panic!("Incorrect variant of string key"),
}
}
}

fn iterator_collected() {
let file_path = Path::new("./data/iceland-surface.grib");
let product_kind = ProductKind::GRIB;
let handle = CodesHandle::new_from_file(file_path, product_kind).unwrap();

let handle_collected: Vec<KeyedMessage> = handle.collect().unwrap();
Expand Down
17 changes: 17 additions & 0 deletions src/codes_handle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct CodesHandle<SOURCE: Debug + SpecialDrop> {
_data: DataContainer,
source: SOURCE,
product_kind: ProductKind,
unsafe_message: KeyedMessage,
}

///Structure used to access keys inside the GRIB file message.
Expand Down Expand Up @@ -196,6 +197,14 @@ impl CodesHandle<GribFile> {
pointer: file_pointer,
},
product_kind,
unsafe_message: KeyedMessage {
message_handle: null_mut(),
iterator_flags: None,
iterator_namespace: None,
keys_iterator: None,
keys_iterator_next_item_exists: false,
nearest_handle: None,
},
})
}

Expand Down Expand Up @@ -252,6 +261,14 @@ impl CodesHandle<GribFile> {
pointer: file_pointer,
},
product_kind,
unsafe_message: KeyedMessage {
message_handle: null_mut(),
iterator_flags: None,
iterator_namespace: None,
keys_iterator: None,
keys_iterator_next_item_exists: false,
nearest_handle: None,
},
})
}
}
Expand Down

0 comments on commit f5978dd

Please sign in to comment.