Skip to content

Commit

Permalink
Merge pull request #80 from magnusuMET/feature/char
Browse files Browse the repository at this point in the history
add char type
  • Loading branch information
magnusuMET authored Jan 8, 2021
2 parents 7cbd8d4 + d0c9765 commit ed83080
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use std::convert::TryInto;
pub enum BasicType {
/// Signed 1 byte integer
Byte,
/// ISO/ASCII character
Char,
/// Unsigned 1 byte integer
Ubyte,
/// Signed 2 byte integer
Expand All @@ -34,7 +36,7 @@ impl BasicType {
/// Size of the type in bytes
fn size(self) -> usize {
match self {
Self::Byte | Self::Ubyte => 1,
Self::Byte | Self::Ubyte | Self::Char => 1,
Self::Short | Self::Ushort => 2,
Self::Int | Self::Uint | Self::Float => 4,
Self::Int64 | Self::Uint64 | Self::Double => 8,
Expand All @@ -45,6 +47,7 @@ impl BasicType {
use super::Numeric;
match self {
Self::Byte => i8::NCTYPE,
Self::Char => NC_CHAR,
Self::Ubyte => u8::NCTYPE,
Self::Short => i16::NCTYPE,
Self::Ushort => u16::NCTYPE,
Expand All @@ -61,6 +64,7 @@ impl BasicType {
pub fn name(self) -> &'static str {
match self {
BasicType::Byte => "i8",
BasicType::Char => "char",
BasicType::Ubyte => "u8",
BasicType::Short => "i16",
BasicType::Ushort => "u16",
Expand All @@ -79,6 +83,9 @@ impl BasicType {
pub fn is_i8(self) -> bool {
self == Self::Byte
}
pub fn is_char(self) -> bool {
self == Self::Char
}
pub fn is_u8(self) -> bool {
self == Self::Ubyte
}
Expand Down Expand Up @@ -759,6 +766,7 @@ impl VariableType {
/// Get the variable type from the id
pub(crate) fn from_id(ncid: nc_type, xtype: nc_type) -> error::Result<Self> {
match xtype {
NC_CHAR => Ok(Self::Basic(BasicType::Char)),
NC_BYTE => Ok(Self::Basic(BasicType::Byte)),
NC_UBYTE => Ok(Self::Basic(BasicType::Ubyte)),
NC_SHORT => Ok(Self::Basic(BasicType::Short)),
Expand Down
24 changes: 24 additions & 0 deletions tests/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,27 @@ fn put_get_vlen() {
assert_eq!(v, &buf[i..]);
}
}

#[test]
fn char() {
use netcdf::types::BasicType;
let d = tempfile::tempdir().unwrap();
let path = d.path().join("test_char.nc");

let mut f = netcdf::create(&path).unwrap();

f.add_dimension("x", 2).unwrap();

let mut var = f
.add_variable_with_type("x", &["x"], &BasicType::Char.into())
.unwrap();

let vals = ['2' as char as u8, '3' as char as u8];
unsafe {
var.put_raw_values(&vals, &[0], &[vals.len()]).unwrap();
}

let mut retrieved_vals = [0, 0];
var.raw_values(&mut retrieved_vals, &[0], &[2]).unwrap();
assert_eq!(vals, retrieved_vals);
}

0 comments on commit ed83080

Please sign in to comment.