Skip to content

Commit 59264b2

Browse files
committed
Add Inet, Mac data type
1 parent 87df409 commit 59264b2

File tree

5 files changed

+71
-76
lines changed

5 files changed

+71
-76
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ repository = "https://github.com/crossdb-org/crossdb-rust.git"
1212
lru = "0.12"
1313
serde = "1.0"
1414
thiserror = "1.0"
15+
strum = { version = "0.26", features = ["derive"] }
1516

1617
[build-dependencies]
1718
bindgen = "0.70"

src/column.rs

+46-65
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,60 @@
11
use crate::*;
22
use std::rc::Rc;
33
use std::slice::Iter;
4+
use strum::{Display, FromRepr, IntoStaticStr};
45

56
// https://github.com/crossdb-org/crossdb/blob/main/include/crossdb.h
6-
#[derive(Debug, Clone, Copy)]
7+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, IntoStaticStr, FromRepr)]
8+
#[repr(u32)]
79
pub enum DataType {
8-
Null,
9-
TinyInt,
10-
SmallInt,
11-
Int,
12-
BigInt,
13-
UTinyInt,
14-
USmallInt,
15-
UInt,
16-
UBigInt,
17-
Float,
18-
Double,
19-
Timestamp,
20-
Char,
21-
Binary,
22-
VChar,
23-
VBinary,
24-
Bool,
25-
Max,
26-
}
27-
28-
impl Display for DataType {
29-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30-
match self {
31-
DataType::Null => write!(f, "NULL"),
32-
DataType::TinyInt => write!(f, "TINYINT"),
33-
DataType::SmallInt => write!(f, "SMALLINT"),
34-
DataType::Int => write!(f, "INT"),
35-
DataType::BigInt => write!(f, "BIGINT"),
36-
DataType::UTinyInt => write!(f, "UTINYINT"),
37-
DataType::USmallInt => write!(f, "USMALLINT"),
38-
DataType::UInt => write!(f, "UINT"),
39-
DataType::UBigInt => write!(f, "UBIGINT"),
40-
DataType::Float => write!(f, "FLOAT"),
41-
DataType::Double => write!(f, "DOUBLE"),
42-
DataType::Timestamp => write!(f, "TIMESTAMP"),
43-
DataType::Char => write!(f, "CHAR"),
44-
DataType::Binary => write!(f, "BINARY"),
45-
DataType::VChar => write!(f, "VCHAR"),
46-
DataType::VBinary => write!(f, "VBINARY"),
47-
DataType::Bool => write!(f, "BOOL"),
48-
DataType::Max => write!(f, "MAX"),
49-
}
50-
}
10+
#[strum(serialize = "NULL")]
11+
Null = xdb_type_t_XDB_TYPE_NULL,
12+
#[strum(serialize = "TINYINT")]
13+
TinyInt = xdb_type_t_XDB_TYPE_TINYINT,
14+
#[strum(serialize = "SMALLINT")]
15+
SmallInt = xdb_type_t_XDB_TYPE_SMALLINT,
16+
#[strum(serialize = "INT")]
17+
Int = xdb_type_t_XDB_TYPE_INT,
18+
#[strum(serialize = "BIGINT")]
19+
BigInt = xdb_type_t_XDB_TYPE_BIGINT,
20+
#[strum(serialize = "UTINYINT")]
21+
UTinyInt = xdb_type_t_XDB_TYPE_UTINYINT,
22+
#[strum(serialize = "USMALLINT")]
23+
USmallInt = xdb_type_t_XDB_TYPE_USMALLINT,
24+
#[strum(serialize = "UINT")]
25+
UInt = xdb_type_t_XDB_TYPE_UINT,
26+
#[strum(serialize = "UBIGINT")]
27+
UBigInt = xdb_type_t_XDB_TYPE_UBIGINT,
28+
#[strum(serialize = "FLOAT")]
29+
Float = xdb_type_t_XDB_TYPE_FLOAT,
30+
#[strum(serialize = "DOUBLE")]
31+
Double = xdb_type_t_XDB_TYPE_DOUBLE,
32+
#[strum(serialize = "TIMESTAMP")]
33+
Timestamp = xdb_type_t_XDB_TYPE_TIMESTAMP,
34+
#[strum(serialize = "CHAR")]
35+
Char = xdb_type_t_XDB_TYPE_CHAR,
36+
#[strum(serialize = "BINARY")]
37+
Binary = xdb_type_t_XDB_TYPE_BINARY,
38+
#[strum(serialize = "VCHAR")]
39+
VChar = xdb_type_t_XDB_TYPE_VCHAR,
40+
#[strum(serialize = "VBINARY")]
41+
VBinary = xdb_type_t_XDB_TYPE_VBINARY,
42+
#[strum(serialize = "BOOL")]
43+
Bool = xdb_type_t_XDB_TYPE_BOOL,
44+
#[strum(serialize = "INET")]
45+
Inet = xdb_type_t_XDB_TYPE_INET,
46+
#[strum(serialize = "MAC")]
47+
Mac = xdb_type_t_XDB_TYPE_MAC,
48+
#[strum(serialize = "MAX")]
49+
Max = xdb_type_t_XDB_TYPE_MAX,
5150
}
5251

5352
impl DataType {
54-
#[allow(non_upper_case_globals)]
5553
unsafe fn from_meta(meta: u64, col: u16) -> Self {
5654
let t = xdb_column_type(meta, col);
57-
match t {
58-
xdb_type_t_XDB_TYPE_NULL => Self::Null,
59-
xdb_type_t_XDB_TYPE_TINYINT => Self::TinyInt,
60-
xdb_type_t_XDB_TYPE_SMALLINT => Self::SmallInt,
61-
xdb_type_t_XDB_TYPE_INT => Self::Int,
62-
xdb_type_t_XDB_TYPE_BIGINT => Self::BigInt,
63-
xdb_type_t_XDB_TYPE_UTINYINT => Self::UTinyInt,
64-
xdb_type_t_XDB_TYPE_USMALLINT => Self::USmallInt,
65-
xdb_type_t_XDB_TYPE_UINT => Self::UInt,
66-
xdb_type_t_XDB_TYPE_UBIGINT => Self::UBigInt,
67-
xdb_type_t_XDB_TYPE_FLOAT => Self::Float,
68-
xdb_type_t_XDB_TYPE_DOUBLE => Self::Double,
69-
xdb_type_t_XDB_TYPE_TIMESTAMP => Self::Timestamp,
70-
xdb_type_t_XDB_TYPE_CHAR => Self::Char,
71-
xdb_type_t_XDB_TYPE_BINARY => Self::Binary,
72-
xdb_type_t_XDB_TYPE_VCHAR => Self::VChar,
73-
xdb_type_t_XDB_TYPE_VBINARY => Self::VBinary,
74-
xdb_type_t_XDB_TYPE_BOOL => Self::Bool,
75-
xdb_type_t_XDB_TYPE_MAX => Self::Max,
76-
_ => unreachable!(),
55+
match Self::from_repr(t) {
56+
Some(t) => t,
57+
None => unreachable!(),
7758
}
7859
}
7960
}

src/de.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ impl<'de> Deserializer<'de> for ValueDeserializer<'de> {
9393
Value::Timestamp(v) => visitor.visit_i64(v),
9494
Value::String(v) => visitor.visit_str(v),
9595
// TODO: Deserialize Binary to Vec<u8>
96-
Value::Binary(_) => unimplemented!(),
96+
Value::Binary(_) => todo!(),
9797
Value::Bool(v) => visitor.visit_bool(v),
98+
Value::Inet(_) => todo!(),
99+
Value::Mac(_) => todo!(),
98100
}
99101
}
100102

src/value.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub enum Value<'a> {
1313
String(&'a str),
1414
Binary(&'a [u8]),
1515
Bool(bool),
16+
Inet([u8; 18]),
17+
Mac([u8; 6]),
1618
}
1719

1820
impl Display for Value<'_> {
@@ -29,6 +31,8 @@ impl Display for Value<'_> {
2931
Value::String(v) => write!(f, "{}", v),
3032
Value::Binary(v) => write!(f, "{:?}", v),
3133
Value::Bool(v) => write!(f, "{}", v),
34+
Value::Inet(v) => write!(f, "{:?}", v),
35+
Value::Mac(v) => write!(f, "{:?}", v),
3236
}
3337
}
3438
}
@@ -43,13 +47,18 @@ impl<'a> Value<'a> {
4347
t: DataType,
4448
) -> Value<'a> {
4549
match t {
46-
DataType::TinyInt => Value::I8(xdb_column_int(meta, row, col) as _),
47-
DataType::SmallInt => Value::I16(xdb_column_int(meta, row, col) as _),
48-
DataType::Int => Value::I32(xdb_column_int(meta, row, col) as _),
49-
DataType::BigInt => Value::I64(xdb_column_int64(meta, row, col)),
50-
DataType::Float => Value::F32(xdb_column_float(meta, row, col)),
51-
DataType::Double => Value::F64(xdb_column_double(meta, row, col)),
52-
DataType::Timestamp => Value::Timestamp(xdb_column_int64(meta, row, col)),
50+
DataType::Null => Self::Null,
51+
DataType::TinyInt => Self::I8(xdb_column_int(meta, row, col) as _),
52+
DataType::UTinyInt => todo!(),
53+
DataType::SmallInt => Self::I16(xdb_column_int(meta, row, col) as _),
54+
DataType::USmallInt => todo!(),
55+
DataType::Int => Self::I32(xdb_column_int(meta, row, col) as _),
56+
DataType::UInt => todo!(),
57+
DataType::BigInt => Self::I64(xdb_column_int64(meta, row, col)),
58+
DataType::UBigInt => todo!(),
59+
DataType::Float => Self::F32(xdb_column_float(meta, row, col)),
60+
DataType::Double => Self::F64(xdb_column_double(meta, row, col)),
61+
DataType::Timestamp => Self::Timestamp(xdb_column_int64(meta, row, col)),
5362
DataType::Char | DataType::VChar => {
5463
let ptr = xdb_column_str(meta, row, col);
5564
if ptr.is_null() {
@@ -61,8 +70,10 @@ impl<'a> Value<'a> {
6170
// xdb_column_blob(meta, row, col, pLen);
6271
todo!()
6372
}
64-
DataType::Bool => Value::Bool(xdb_column_int(meta, row, col) == 1),
65-
_ => unimplemented!(),
73+
DataType::Bool => Self::Bool(xdb_column_int(meta, row, col) == 1),
74+
DataType::Inet => todo!(),
75+
DataType::Mac => todo!(),
76+
DataType::Max => todo!(),
6677
}
6778
}
6879
}

0 commit comments

Comments
 (0)