Skip to content

Commit deff7a8

Browse files
authored
Make it more ergonmic to use PartiqlShapeBuilder and type macros (#490)
1 parent 298a7ac commit deff7a8

File tree

1 file changed

+68
-15
lines changed

1 file changed

+68
-15
lines changed

partiql-types/src/lib.rs

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,42 +46,60 @@ macro_rules! type_dynamic {
4646
#[macro_export]
4747
macro_rules! type_int {
4848
() => {
49-
$crate::PartiqlShapeBuilder::init_or_get().new_static($crate::Static::Int)
49+
type_int!($crate::PartiqlShapeBuilder::init_or_get())
50+
};
51+
($bld:expr) => {
52+
$bld.new_static($crate::Static::Int)
5053
};
5154
}
5255

5356
#[macro_export]
5457
macro_rules! type_int8 {
5558
() => {
56-
$crate::PartiqlShapeBuilder::init_or_get().new_static($crate::Static::Int8)
59+
type_int8!($crate::PartiqlShapeBuilder::init_or_get())
60+
};
61+
($bld:expr) => {
62+
$bld.new_static($crate::Static::Int8)
5763
};
5864
}
5965

6066
#[macro_export]
6167
macro_rules! type_int16 {
6268
() => {
63-
$crate::PartiqlShapeBuilder::init_or_get().new_static($crate::Static::Int16)
69+
type_int16!($crate::PartiqlShapeBuilder::init_or_get())
70+
};
71+
($bld:expr) => {
72+
$bld.new_static($crate::Static::Int16)
6473
};
6574
}
6675

6776
#[macro_export]
6877
macro_rules! type_int32 {
6978
() => {
70-
$crate::PartiqlShapeBuilder::init_or_get().new_static($crate::Static::Int32)
79+
type_int32!($crate::PartiqlShapeBuilder::init_or_get())
80+
};
81+
($bld:expr) => {
82+
$bld.new_static($crate::Static::Int32)
7183
};
7284
}
7385

7486
#[macro_export]
7587
macro_rules! type_int64 {
7688
() => {
77-
$crate::PartiqlShapeBuilder::init_or_get().new_static($crate::Static::Int64)
89+
type_int64!($crate::PartiqlShapeBuilder::init_or_get())
90+
};
91+
($bld:expr) => {
92+
$bld.new_static($crate::Static::Int64)
7893
};
7994
}
8095

8196
#[macro_export]
8297
macro_rules! type_decimal {
8398
() => {
84-
$crate::PartiqlShapeBuilder::init_or_get().new_static($crate::Static::Decimal)
99+
type_decimal!($crate::PartiqlShapeBuilder::init_or_get())
100+
};
101+
($bld:expr) => {
102+
$bld.new_static($crate::Static::Decimal)
85103
};
86104
}
87105

@@ -90,28 +108,40 @@ macro_rules! type_decimal {
90108
#[macro_export]
91109
macro_rules! type_float32 {
92110
() => {
93-
$crate::PartiqlShapeBuilder::init_or_get().new_static($crate::Static::Float32)
111+
type_float32!($crate::PartiqlShapeBuilder::init_or_get())
112+
};
113+
($bld:expr) => {
114+
$bld.new_static($crate::Static::Float32)
94115
};
95116
}
96117

97118
#[macro_export]
98119
macro_rules! type_float64 {
99120
() => {
100-
$crate::PartiqlShapeBuilder::init_or_get().new_static($crate::Static::Float64)
121+
type_float64!($crate::PartiqlShapeBuilder::init_or_get())
122+
};
123+
($bld:expr) => {
124+
$bld.new_static($crate::Static::Float64)
101125
};
102126
}
103127

104128
#[macro_export]
105129
macro_rules! type_string {
106130
() => {
107-
$crate::PartiqlShapeBuilder::init_or_get().new_static($crate::Static::String)
131+
type_string!($crate::PartiqlShapeBuilder::init_or_get())
132+
};
133+
($bld:expr) => {
134+
$bld.new_static($crate::Static::String)
108135
};
109136
}
110137

111138
#[macro_export]
112139
macro_rules! type_bool {
113140
() => {
114-
$crate::PartiqlShapeBuilder::init_or_get().new_static($crate::Static::Bool)
141+
type_bool!($crate::PartiqlShapeBuilder::init_or_get())
142+
};
143+
($bld:expr) => {
144+
$bld.new_static($crate::Static::Bool)
115145
};
116146
}
117147

@@ -130,7 +160,10 @@ macro_rules! type_numeric {
130160
#[macro_export]
131161
macro_rules! type_datetime {
132162
() => {
133-
$crate::PartiqlShapeBuilder::init_or_get().new_static($crate::Static::DateTime)
163+
type_datetime!($crate::PartiqlShapeBuilder::init_or_get())
164+
};
165+
($bld:expr) => {
166+
$bld.new_static($crate::Static::DateTime)
134167
};
135168
}
136169

@@ -505,6 +538,14 @@ impl PartiqlShapeBuilder {
505538
self.new_static(Static::Bag(b))
506539
}
507540

541+
#[must_use]
542+
pub fn new_bag_of<E>(&self, element_type: E) -> PartiqlShape
543+
where
544+
E: Into<PartiqlShape>,
545+
{
546+
self.new_bag(BagType::new_of(element_type))
547+
}
548+
508549
#[must_use]
509550
pub fn new_array(&self, a: ArrayType) -> PartiqlShape {
510551
self.new_static(Static::Array(a))
@@ -610,6 +651,12 @@ impl StaticType {
610651
}
611652
}
612653

654+
impl From<StaticType> for PartiqlShape {
655+
fn from(value: StaticType) -> Self {
656+
PartiqlShape::Static(value)
657+
}
658+
}
659+
613660
impl Display for StaticType {
614661
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
615662
let ty = &self.ty;
@@ -862,17 +909,23 @@ pub struct BagType {
862909
}
863910

864911
impl BagType {
865-
#[must_use]
912+
#[inline]
866913
pub fn new_any() -> Self {
867-
BagType::new(Box::new(PartiqlShape::Dynamic))
914+
Self::new_of(PartiqlShape::Dynamic)
868915
}
869916

870-
#[must_use]
917+
#[inline]
871918
pub fn new(typ: Box<PartiqlShape>) -> Self {
872919
BagType { element_type: typ }
873920
}
874921

875-
#[must_use]
922+
pub fn new_of<E>(element_type: E) -> Self
923+
where
924+
E: Into<PartiqlShape>,
925+
{
926+
Self::new(Box::new(element_type.into()))
927+
}
928+
876929
pub fn element_type(&self) -> &PartiqlShape {
877930
&self.element_type
878931
}

0 commit comments

Comments
 (0)