Skip to content

Commit

Permalink
preliminary finish keyops implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Quba1 committed Jul 24, 2024
1 parent efc19cb commit a22d21e
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/keyed_message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ pub trait KeyOps<T: Clone> {
fn read_unchecked(&self, key_name: &str) -> Result<T, CodesError>;

fn write(&mut self, key: Key<T>) -> Result<(), CodesError>;
fn write_unchecked(&mut self, key: Key<T>) -> Result<(), CodesError>;
}

/// Structure representing a single key in the `KeyedMessage`
Expand Down
136 changes: 129 additions & 7 deletions src/keyed_message/ops.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use eccodes_sys::grib_handle;

use crate::{
intermediate_bindings::{
codes_get_bytes, codes_get_double, codes_get_double_array, codes_get_long,
codes_get_long_array, codes_get_native_type, codes_get_size, codes_get_string,
NativeKeyType,
codes_set_bytes, codes_set_double, codes_set_double_array, codes_set_long,
codes_set_long_array, codes_set_string, NativeKeyType,
},
CodesError, Key, KeyOps, KeyedMessage,
};
Expand Down Expand Up @@ -34,18 +33,141 @@ impl KeyOps<i64> for KeyedMessage {
return Err(CodesError::WrongRequestedKeySize);
}

todo!()
self.read_unchecked(key_name)
}

fn read_unchecked(&self, key_name: &str) -> Result<i64, CodesError> {
unsafe { codes_get_long(self.message_handle, key_name) }
}

fn write(&mut self, key: Key<i64>) -> Result<(), CodesError> {
todo!()
unsafe { codes_set_long(self.message_handle, &key.name, key.value) }
}
}

impl KeyOps<f64> for KeyedMessage {
fn read(&self, key_name: &str) -> Result<f64, CodesError> {
match self.get_key_native_type(key_name)? {
NativeKeyType::Double => (),
_ => return Err(CodesError::WrongRequestedKeyType),
}

let key_size = self.get_key_size(key_name)?;

if key_size < 1 {
return Err(CodesError::IncorrectKeySize);
} else if key_size > 1 {
return Err(CodesError::WrongRequestedKeySize);
}

self.read_unchecked(key_name)
}

fn read_unchecked(&self, key_name: &str) -> Result<f64, CodesError> {
unsafe { codes_get_double(self.message_handle, key_name) }
}

fn write(&mut self, key: Key<f64>) -> Result<(), CodesError> {
unsafe { codes_set_double(self.message_handle, &key.name, key.value) }
}
}

impl KeyOps<String> for KeyedMessage {
fn read(&self, key_name: &str) -> Result<String, CodesError> {
match self.get_key_native_type(key_name)? {
NativeKeyType::Str => (),
_ => return Err(CodesError::WrongRequestedKeyType),
}

let key_size = self.get_key_size(key_name)?;

if key_size < 1 {
return Err(CodesError::IncorrectKeySize);
}

self.read_unchecked(key_name)
}

fn read_unchecked(&self, key_name: &str) -> Result<String, CodesError> {
unsafe { codes_get_string(self.message_handle, key_name) }
}

fn write(&mut self, key: Key<String>) -> Result<(), CodesError> {
unsafe { codes_set_string(self.message_handle, &key.name, &key.value) }
}
}

impl KeyOps<Vec<i64>> for KeyedMessage {
fn read(&self, key_name: &str) -> Result<Vec<i64>, CodesError> {
match self.get_key_native_type(key_name)? {
NativeKeyType::Long => (),
_ => return Err(CodesError::WrongRequestedKeyType),
}

let key_size = self.get_key_size(key_name)?;

if key_size < 1 {
return Err(CodesError::IncorrectKeySize);
}

self.read_unchecked(key_name)
}

fn read_unchecked(&self, key_name: &str) -> Result<Vec<i64>, CodesError> {
unsafe { codes_get_long_array(self.message_handle, key_name) }
}

fn write(&mut self, key: Key<Vec<i64>>) -> Result<(), CodesError> {
unsafe { codes_set_long_array(self.message_handle, &key.name, &key.value) }
}
}

impl KeyOps<Vec<f64>> for KeyedMessage {
fn read(&self, key_name: &str) -> Result<Vec<f64>, CodesError> {
match self.get_key_native_type(key_name)? {
NativeKeyType::Double => (),
_ => return Err(CodesError::WrongRequestedKeyType),
}

let key_size = self.get_key_size(key_name)?;

if key_size < 1 {
return Err(CodesError::IncorrectKeySize);
}

self.read_unchecked(key_name)
}

fn read_unchecked(&self, key_name: &str) -> Result<Vec<f64>, CodesError> {
unsafe { codes_get_double_array(self.message_handle, key_name) }
}

fn write(&mut self, key: Key<Vec<f64>>) -> Result<(), CodesError> {
unsafe { codes_set_double_array(self.message_handle, &key.name, &key.value) }
}
}

impl KeyOps<Vec<u8>> for KeyedMessage {
fn read(&self, key_name: &str) -> Result<Vec<u8>, CodesError> {
match self.get_key_native_type(key_name)? {
NativeKeyType::Bytes => (),
_ => return Err(CodesError::WrongRequestedKeyType),
}

let key_size = self.get_key_size(key_name)?;

if key_size < 1 {
return Err(CodesError::IncorrectKeySize);
}

self.read_unchecked(key_name)
}

fn read_unchecked(&self, key_name: &str) -> Result<Vec<u8>, CodesError> {
unsafe { codes_get_bytes(self.message_handle, key_name) }
}

fn write_unchecked(&mut self, key: Key<i64>) -> Result<(), CodesError> {
todo!()
fn write(&mut self, key: Key<Vec<u8>>) -> Result<(), CodesError> {
unsafe { codes_set_bytes(self.message_handle, &key.name, &key.value) }
}
}

0 comments on commit a22d21e

Please sign in to comment.