From a22d21ef866fca708deaa229dc52019ab839fd03 Mon Sep 17 00:00:00 2001 From: Quba1 <22771850+Quba1@users.noreply.github.com> Date: Wed, 24 Jul 2024 14:50:00 +0200 Subject: [PATCH] preliminary finish keyops implementation --- src/keyed_message/mod.rs | 1 - src/keyed_message/ops.rs | 136 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 129 insertions(+), 8 deletions(-) diff --git a/src/keyed_message/mod.rs b/src/keyed_message/mod.rs index 2c30037..2342500 100644 --- a/src/keyed_message/mod.rs +++ b/src/keyed_message/mod.rs @@ -63,7 +63,6 @@ pub trait KeyOps { fn read_unchecked(&self, key_name: &str) -> Result; fn write(&mut self, key: Key) -> Result<(), CodesError>; - fn write_unchecked(&mut self, key: Key) -> Result<(), CodesError>; } /// Structure representing a single key in the `KeyedMessage` diff --git a/src/keyed_message/ops.rs b/src/keyed_message/ops.rs index 0876909..4ee9a07 100644 --- a/src/keyed_message/ops.rs +++ b/src/keyed_message/ops.rs @@ -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, }; @@ -34,7 +33,7 @@ impl KeyOps for KeyedMessage { return Err(CodesError::WrongRequestedKeySize); } - todo!() + self.read_unchecked(key_name) } fn read_unchecked(&self, key_name: &str) -> Result { @@ -42,10 +41,133 @@ impl KeyOps for KeyedMessage { } fn write(&mut self, key: Key) -> Result<(), CodesError> { - todo!() + unsafe { codes_set_long(self.message_handle, &key.name, key.value) } + } +} + +impl KeyOps for KeyedMessage { + fn read(&self, key_name: &str) -> Result { + 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 { + unsafe { codes_get_double(self.message_handle, key_name) } + } + + fn write(&mut self, key: Key) -> Result<(), CodesError> { + unsafe { codes_set_double(self.message_handle, &key.name, key.value) } + } +} + +impl KeyOps for KeyedMessage { + fn read(&self, key_name: &str) -> Result { + 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 { + unsafe { codes_get_string(self.message_handle, key_name) } + } + + fn write(&mut self, key: Key) -> Result<(), CodesError> { + unsafe { codes_set_string(self.message_handle, &key.name, &key.value) } + } +} + +impl KeyOps> for KeyedMessage { + fn read(&self, key_name: &str) -> Result, 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, CodesError> { + unsafe { codes_get_long_array(self.message_handle, key_name) } + } + + fn write(&mut self, key: Key>) -> Result<(), CodesError> { + unsafe { codes_set_long_array(self.message_handle, &key.name, &key.value) } + } +} + +impl KeyOps> for KeyedMessage { + fn read(&self, key_name: &str) -> Result, 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, CodesError> { + unsafe { codes_get_double_array(self.message_handle, key_name) } + } + + fn write(&mut self, key: Key>) -> Result<(), CodesError> { + unsafe { codes_set_double_array(self.message_handle, &key.name, &key.value) } + } +} + +impl KeyOps> for KeyedMessage { + fn read(&self, key_name: &str) -> Result, 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, CodesError> { + unsafe { codes_get_bytes(self.message_handle, key_name) } } - fn write_unchecked(&mut self, key: Key) -> Result<(), CodesError> { - todo!() + fn write(&mut self, key: Key>) -> Result<(), CodesError> { + unsafe { codes_set_bytes(self.message_handle, &key.name, &key.value) } } }