Skip to content

Commit

Permalink
rename Key to DynamicKey
Browse files Browse the repository at this point in the history
  • Loading branch information
Quba1 committed Jul 23, 2024
1 parent b21db71 commit 7052b58
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 109 deletions.
16 changes: 8 additions & 8 deletions benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,36 @@ pub fn key_reading(c: &mut Criterion) {
let msg = handle.next().unwrap().unwrap();

c.bench_function("long reading", |b| {
b.iter(|| msg.read_key(black_box("dataDate")).unwrap())
b.iter(|| msg.read_key_dynamic(black_box("dataDate")).unwrap())
});

c.bench_function("double reading", |b| {
b.iter(|| {
msg.read_key(black_box("jDirectionIncrementInDegrees"))
msg.read_key_dynamic(black_box("jDirectionIncrementInDegrees"))
.unwrap()
})
});

c.bench_function("double array reading", |b| {
b.iter(|| msg.read_key(black_box("values")).unwrap())
b.iter(|| msg.read_key_dynamic(black_box("values")).unwrap())
});

c.bench_function("string reading", |b| {
b.iter(|| msg.read_key(black_box("name")).unwrap())
b.iter(|| msg.read_key_dynamic(black_box("name")).unwrap())
});

c.bench_function("bytes reading", |b| {
b.iter(|| msg.read_key(black_box("section1Padding")).unwrap())
b.iter(|| msg.read_key_dynamic(black_box("section1Padding")).unwrap())
});

c.bench_function("missing nul-byte termination reading", |b| {
b.iter(|| msg.read_key(black_box("experimentVersionNumber")).unwrap())
b.iter(|| msg.read_key_dynamic(black_box("experimentVersionNumber")).unwrap())
});

c.bench_function("problematic key reading", |b| {
b.iter(|| {
msg.read_key(black_box("zero"))
.unwrap_or_else(|_| msg.read_key(black_box("zeros")).unwrap())
msg.read_key_dynamic(black_box("zero"))
.unwrap_or_else(|_| msg.read_key_dynamic(black_box("zeros")).unwrap())
})
});
}
Expand Down
28 changes: 14 additions & 14 deletions src/codes_handle/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl FallibleStreamingIterator for CodesHandle<CodesIndex> {
mod tests {
use crate::{
codes_handle::{CodesHandle, ProductKind},
KeyType,
DynamicKeyType,
};
use anyhow::{Context, Ok, Result};
use fallible_streaming_iterator::FallibleStreamingIterator;
Expand All @@ -104,17 +104,17 @@ mod tests {
let mut handle = CodesHandle::new_from_file(file_path, product_kind)?;

let msg1 = handle.next()?.context("Message not some")?;
let key1 = msg1.read_key("typeOfLevel")?;
let key1 = msg1.read_key_dynamic("typeOfLevel")?;

let msg2 = handle.next()?.context("Message not some")?;
let key2 = msg2.read_key("typeOfLevel")?;
let key2 = msg2.read_key_dynamic("typeOfLevel")?;

let msg3 = handle.next()?.context("Message not some")?;
let key3 = msg3.read_key("typeOfLevel")?;
let key3 = msg3.read_key_dynamic("typeOfLevel")?;

assert_eq!(key1.value, KeyType::Str("isobaricInhPa".to_string()));
assert_eq!(key2.value, KeyType::Str("isobaricInhPa".to_string()));
assert_eq!(key3.value, KeyType::Str("isobaricInhPa".to_string()));
assert_eq!(key1.value, DynamicKeyType::Str("isobaricInhPa".to_string()));
assert_eq!(key2.value, DynamicKeyType::Str("isobaricInhPa".to_string()));
assert_eq!(key3.value, DynamicKeyType::Str("isobaricInhPa".to_string()));

Ok(())
}
Expand All @@ -127,10 +127,10 @@ mod tests {
let mut handle = CodesHandle::new_from_file(file_path, product_kind)?;

while let Some(msg) = handle.next()? {
let key = msg.read_key("shortName")?;
let key = msg.read_key_dynamic("shortName")?;

match key.value {
KeyType::Str(_) => {}
DynamicKeyType::Str(_) => {}
_ => panic!("Incorrect variant of string key"),
}
}
Expand All @@ -151,9 +151,9 @@ mod tests {
}

for msg in handle_collected {
let key = msg.read_key("name")?;
let key = msg.read_key_dynamic("name")?;
match key.value {
KeyType::Str(_) => {}
DynamicKeyType::Str(_) => {}
_ => panic!("Incorrect variant of string key"),
}
}
Expand Down Expand Up @@ -207,8 +207,8 @@ mod tests {
let mut level = vec![];

while let Some(msg) = handle.next()? {
if msg.read_key("shortName")?.value == KeyType::Str("msl".to_string())
&& msg.read_key("typeOfLevel")?.value == KeyType::Str("surface".to_string())
if msg.read_key_dynamic("shortName")?.value == DynamicKeyType::Str("msl".to_string())
&& msg.read_key_dynamic("typeOfLevel")?.value == DynamicKeyType::Str("surface".to_string())
{
level.push(msg.try_clone()?);
}
Expand All @@ -218,7 +218,7 @@ mod tests {
// Find nearest modifies internal KeyedMessage fields so we need mutable reference
let level = &level[0];

println!("{:?}", level.read_key("shortName"));
println!("{:?}", level.read_key_dynamic("shortName"));

// Get the four nearest gridpoints of Reykjavik
let nearest_gridpoints = level.codes_nearest()?.find_nearest(64.13, -21.89)?;
Expand Down
32 changes: 16 additions & 16 deletions src/keyed_message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ pub struct KeyedMessage {

/// Structure representing a single key in the `KeyedMessage`
#[derive(Clone, Debug, PartialEq)]
pub struct Key {
pub struct DynamicKey {
#[allow(missing_docs)]
pub name: String,
#[allow(missing_docs)]
pub value: KeyType,
pub value: DynamicKeyType,
}

/// Enum representing the value of [`Key`] from the `KeyedMessage`
Expand All @@ -64,7 +64,7 @@ pub struct Key {
/// ecCodes can return several different types of key, which are represented by this enum
/// and each variant contains the respective data type.
#[derive(Clone, Debug, PartialEq)]
pub enum KeyType {
pub enum DynamicKeyType {
#[allow(missing_docs)]
Float(f64),
#[allow(missing_docs)]
Expand Down Expand Up @@ -135,13 +135,13 @@ mod tests {
let mut handle = CodesHandle::new_from_file(file_path, product_kind)?;
let current_message = handle.next()?.context("Message not some")?;

let _ = current_message.read_key("validityDate")?;
let _ = current_message.read_key("validityTime")?;
let _ = current_message.read_key("level")?;
let _ = current_message.read_key("shortName")?;
let _ = current_message.read_key("units")?;
let _ = current_message.read_key("values")?;
let _ = current_message.read_key("typeOfLevel")?;
let _ = current_message.read_key_dynamic("validityDate")?;
let _ = current_message.read_key_dynamic("validityTime")?;
let _ = current_message.read_key_dynamic("level")?;
let _ = current_message.read_key_dynamic("shortName")?;
let _ = current_message.read_key_dynamic("units")?;
let _ = current_message.read_key_dynamic("values")?;
let _ = current_message.read_key_dynamic("typeOfLevel")?;

Ok(())
}
Expand Down Expand Up @@ -174,12 +174,12 @@ mod tests {

drop(handle);

let _ = msg.read_key("dataDate")?;
let _ = msg.read_key("jDirectionIncrementInDegrees")?;
let _ = msg.read_key("values")?;
let _ = msg.read_key("name")?;
let _ = msg.read_key("section1Padding")?;
let _ = msg.read_key("experimentVersionNumber")?;
let _ = msg.read_key_dynamic("dataDate")?;
let _ = msg.read_key_dynamic("jDirectionIncrementInDegrees")?;
let _ = msg.read_key_dynamic("values")?;
let _ = msg.read_key_dynamic("name")?;
let _ = msg.read_key_dynamic("section1Padding")?;
let _ = msg.read_key_dynamic("experimentVersionNumber")?;

Ok(())
}
Expand Down
58 changes: 29 additions & 29 deletions src/keyed_message/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
codes_get_long_array, codes_get_native_type, codes_get_size, codes_get_string,
NativeKeyType,
},
Key, KeyType, KeyedMessage,
DynamicKey, DynamicKeyType, KeyedMessage,
};

impl KeyedMessage {
Expand Down Expand Up @@ -57,7 +57,7 @@ impl KeyedMessage {
/// Returns [`CodesError::IncorrectKeySize`] when the size of given key is lower than 1. This indicates corrupted data file,
/// bug in the crate or bug in the ecCodes library. If you encounter this error please check
/// if your file is correct and report it on Github.
pub fn read_key(&self, key_name: &str) -> Result<Key, CodesError> {
pub fn read_key_dynamic(&self, key_name: &str) -> Result<DynamicKey, CodesError> {
let key_type;

unsafe {
Expand All @@ -76,7 +76,7 @@ impl KeyedMessage {
}

match value {
Ok(val) => Ok(KeyType::Int(val)),
Ok(val) => Ok(DynamicKeyType::Int(val)),
Err(err) => Err(err),
}
} else if key_size >= 2 {
Expand All @@ -86,7 +86,7 @@ impl KeyedMessage {
}

match value {
Ok(val) => Ok(KeyType::IntArray(val)),
Ok(val) => Ok(DynamicKeyType::IntArray(val)),
Err(err) => Err(err),
}
} else {
Expand All @@ -104,7 +104,7 @@ impl KeyedMessage {
}

match value {
Ok(val) => Ok(KeyType::Float(val)),
Ok(val) => Ok(DynamicKeyType::Float(val)),
Err(err) => Err(err),
}
} else if key_size >= 2 {
Expand All @@ -114,7 +114,7 @@ impl KeyedMessage {
}

match value {
Ok(val) => Ok(KeyType::FloatArray(val)),
Ok(val) => Ok(DynamicKeyType::FloatArray(val)),
Err(err) => Err(err),
}
} else {
Expand All @@ -128,7 +128,7 @@ impl KeyedMessage {
}

match value {
Ok(val) => Ok(KeyType::Bytes(val)),
Ok(val) => Ok(DynamicKeyType::Bytes(val)),
Err(err) => Err(err),
}
}
Expand All @@ -140,14 +140,14 @@ impl KeyedMessage {
}

match value {
Ok(val) => Ok(KeyType::Str(val)),
Ok(val) => Ok(DynamicKeyType::Str(val)),
Err(err) => Err(err),
}
}
};

if let Ok(value) = key_value {
Ok(Key {
Ok(DynamicKey {
name: key_name.to_owned(),
value,
})
Expand All @@ -157,9 +157,9 @@ impl KeyedMessage {
value = codes_get_bytes(self.message_handle, key_name)?;
}

Ok(Key {
Ok(DynamicKey {
name: key_name.to_owned(),
value: KeyType::Bytes(value),
value: DynamicKeyType::Bytes(value),
})
}
}
Expand All @@ -170,7 +170,7 @@ mod tests {
use anyhow::{Context, Result};

use crate::codes_handle::{CodesHandle, ProductKind};
use crate::{FallibleIterator, FallibleStreamingIterator, KeyType};
use crate::{FallibleIterator, FallibleStreamingIterator, DynamicKeyType};
use std::path::Path;

#[test]
Expand All @@ -182,36 +182,36 @@ mod tests {

let current_message = handle.next()?.context("Message not some")?;

let str_key = current_message.read_key("name")?;
let str_key = current_message.read_key_dynamic("name")?;

match str_key.value {
KeyType::Str(_) => {}
DynamicKeyType::Str(_) => {}
_ => panic!("Incorrect variant of string key"),
}

assert_eq!(str_key.name, "name");

let double_key = current_message.read_key("jDirectionIncrementInDegrees")?;
let double_key = current_message.read_key_dynamic("jDirectionIncrementInDegrees")?;
match double_key.value {
KeyType::Float(_) => {}
DynamicKeyType::Float(_) => {}
_ => panic!("Incorrect variant of double key"),
}

assert_eq!(double_key.name, "jDirectionIncrementInDegrees");

let long_key = current_message.read_key("numberOfPointsAlongAParallel")?;
let long_key = current_message.read_key_dynamic("numberOfPointsAlongAParallel")?;

match long_key.value {
KeyType::Int(_) => {}
DynamicKeyType::Int(_) => {}
_ => panic!("Incorrect variant of long key"),
}

assert_eq!(long_key.name, "numberOfPointsAlongAParallel");

let double_arr_key = current_message.read_key("values")?;
let double_arr_key = current_message.read_key_dynamic("values")?;

match double_arr_key.value {
KeyType::FloatArray(_) => {}
DynamicKeyType::FloatArray(_) => {}
_ => panic!("Incorrect variant of double array key"),
}

Expand Down Expand Up @@ -260,7 +260,7 @@ mod tests {
let mut handle = CodesHandle::new_from_file(file_path, product_kind)?;
let current_message = handle.next()?.context("Message not some")?;

let missing_key = current_message.read_key("doesNotExist");
let missing_key = current_message.read_key_dynamic("doesNotExist");

assert!(missing_key.is_err());

Expand All @@ -276,15 +276,15 @@ mod tests {

let msg = handle.next()?.context("Message not some")?;

let _ = msg.read_key("dataDate")?;
let _ = msg.read_key("jDirectionIncrementInDegrees")?;
let _ = msg.read_key("values")?;
let _ = msg.read_key("name")?;
let _ = msg.read_key("section1Padding")?;
let _ = msg.read_key("experimentVersionNumber")?;
let _ = msg.read_key_dynamic("dataDate")?;
let _ = msg.read_key_dynamic("jDirectionIncrementInDegrees")?;
let _ = msg.read_key_dynamic("values")?;
let _ = msg.read_key_dynamic("name")?;
let _ = msg.read_key_dynamic("section1Padding")?;
let _ = msg.read_key_dynamic("experimentVersionNumber")?;
let _ = msg
.read_key("zero")
.unwrap_or_else(|_| msg.read_key("zeros").unwrap());
.read_key_dynamic("zero")
.unwrap_or_else(|_| msg.read_key_dynamic("zeros").unwrap());

Ok(())
}
Expand Down
Loading

0 comments on commit 7052b58

Please sign in to comment.