@@ -77,6 +77,7 @@ pub enum KeyValue {
7777 Int64 ( i64 ) ,
7878 Range ( RangeValue ) ,
7979 Uuid ( uuid:: Uuid ) ,
80+ Date ( chrono:: NaiveDate ) ,
8081 Struct ( Vec < KeyValue > ) ,
8182}
8283
@@ -122,6 +123,18 @@ impl From<RangeValue> for KeyValue {
122123 }
123124}
124125
126+ impl From < uuid:: Uuid > for KeyValue {
127+ fn from ( value : uuid:: Uuid ) -> Self {
128+ KeyValue :: Uuid ( value)
129+ }
130+ }
131+
132+ impl From < chrono:: NaiveDate > for KeyValue {
133+ fn from ( value : chrono:: NaiveDate ) -> Self {
134+ KeyValue :: Date ( value)
135+ }
136+ }
137+
125138impl From < Vec < KeyValue > > for KeyValue {
126139 fn from ( value : Vec < KeyValue > ) -> Self {
127140 KeyValue :: Struct ( value)
@@ -143,6 +156,7 @@ impl std::fmt::Display for KeyValue {
143156 KeyValue :: Int64 ( v) => write ! ( f, "{}" , v) ,
144157 KeyValue :: Range ( v) => write ! ( f, "[{}, {})" , v. start, v. end) ,
145158 KeyValue :: Uuid ( v) => write ! ( f, "{}" , v) ,
159+ KeyValue :: Date ( v) => write ! ( f, "{}" , v) ,
146160 KeyValue :: Struct ( v) => {
147161 write ! (
148162 f,
@@ -172,17 +186,19 @@ impl KeyValue {
172186 KeyValue :: Bytes ( Arc :: from ( BASE64_STANDARD . decode ( v) ?) )
173187 }
174188 BasicValueType :: Str { .. } => KeyValue :: Str ( Arc :: from ( v) ) ,
175- BasicValueType :: Bool => KeyValue :: Bool ( v. parse :: < bool > ( ) ?) ,
176- BasicValueType :: Int64 => KeyValue :: Int64 ( v. parse :: < i64 > ( ) ?) ,
189+ BasicValueType :: Bool => KeyValue :: Bool ( v. parse ( ) ?) ,
190+ BasicValueType :: Int64 => KeyValue :: Int64 ( v. parse ( ) ?) ,
177191 BasicValueType :: Range => {
178192 let v2 = values_iter
179193 . next ( )
180194 . ok_or_else ( || api_error ! ( "Key parts less than expected" ) ) ?;
181195 KeyValue :: Range ( RangeValue {
182- start : v. parse :: < usize > ( ) ?,
183- end : v2. parse :: < usize > ( ) ?,
196+ start : v. parse ( ) ?,
197+ end : v2. parse ( ) ?,
184198 } )
185199 }
200+ BasicValueType :: Uuid => KeyValue :: Uuid ( v. parse ( ) ?) ,
201+ BasicValueType :: Date => KeyValue :: Date ( v. parse ( ) ?) ,
186202 schema => api_bail ! ( "Invalid key type {schema}" ) ,
187203 }
188204 }
@@ -208,6 +224,7 @@ impl KeyValue {
208224 output. push ( v. end . to_string ( ) ) ;
209225 }
210226 KeyValue :: Uuid ( v) => output. push ( v. to_string ( ) ) ,
227+ KeyValue :: Date ( v) => output. push ( v. to_string ( ) ) ,
211228 KeyValue :: Struct ( v) => {
212229 for part in v {
213230 part. parts_to_strs ( output) ;
@@ -239,6 +256,7 @@ impl KeyValue {
239256 KeyValue :: Int64 ( _) => "int64" ,
240257 KeyValue :: Range { .. } => "range" ,
241258 KeyValue :: Uuid ( _) => "uuid" ,
259+ KeyValue :: Date ( _) => "date" ,
242260 KeyValue :: Struct ( _) => "struct" ,
243261 }
244262 }
@@ -278,6 +296,20 @@ impl KeyValue {
278296 }
279297 }
280298
299+ pub fn uuid_value ( & self ) -> Result < uuid:: Uuid > {
300+ match self {
301+ KeyValue :: Uuid ( v) => Ok ( * v) ,
302+ _ => anyhow:: bail!( "expected uuid value, but got {}" , self . kind_str( ) ) ,
303+ }
304+ }
305+
306+ pub fn date_value ( & self ) -> Result < chrono:: NaiveDate > {
307+ match self {
308+ KeyValue :: Date ( v) => Ok ( * v) ,
309+ _ => anyhow:: bail!( "expected date value, but got {}" , self . kind_str( ) ) ,
310+ }
311+ }
312+
281313 pub fn struct_value ( & self ) -> Result < & Vec < KeyValue > > {
282314 match self {
283315 KeyValue :: Struct ( v) => Ok ( v) ,
@@ -304,6 +336,10 @@ pub enum BasicValue {
304336 Float64 ( f64 ) ,
305337 Range ( RangeValue ) ,
306338 Uuid ( uuid:: Uuid ) ,
339+ Date ( chrono:: NaiveDate ) ,
340+ Time ( chrono:: NaiveTime ) ,
341+ LocalDateTime ( chrono:: NaiveDateTime ) ,
342+ OffsetDateTime ( chrono:: DateTime < chrono:: FixedOffset > ) ,
307343 Json ( Arc < serde_json:: Value > ) ,
308344 Vector ( Arc < [ BasicValue ] > ) ,
309345}
@@ -356,6 +392,36 @@ impl From<f64> for BasicValue {
356392 }
357393}
358394
395+ impl From < uuid:: Uuid > for BasicValue {
396+ fn from ( value : uuid:: Uuid ) -> Self {
397+ BasicValue :: Uuid ( value)
398+ }
399+ }
400+
401+ impl From < chrono:: NaiveDate > for BasicValue {
402+ fn from ( value : chrono:: NaiveDate ) -> Self {
403+ BasicValue :: Date ( value)
404+ }
405+ }
406+
407+ impl From < chrono:: NaiveTime > for BasicValue {
408+ fn from ( value : chrono:: NaiveTime ) -> Self {
409+ BasicValue :: Time ( value)
410+ }
411+ }
412+
413+ impl From < chrono:: NaiveDateTime > for BasicValue {
414+ fn from ( value : chrono:: NaiveDateTime ) -> Self {
415+ BasicValue :: LocalDateTime ( value)
416+ }
417+ }
418+
419+ impl From < chrono:: DateTime < chrono:: FixedOffset > > for BasicValue {
420+ fn from ( value : chrono:: DateTime < chrono:: FixedOffset > ) -> Self {
421+ BasicValue :: OffsetDateTime ( value)
422+ }
423+ }
424+
359425impl From < serde_json:: Value > for BasicValue {
360426 fn from ( value : serde_json:: Value ) -> Self {
361427 BasicValue :: Json ( Arc :: from ( value) )
@@ -379,8 +445,12 @@ impl BasicValue {
379445 BasicValue :: Int64 ( v) => KeyValue :: Int64 ( v) ,
380446 BasicValue :: Range ( v) => KeyValue :: Range ( v) ,
381447 BasicValue :: Uuid ( v) => KeyValue :: Uuid ( v) ,
448+ BasicValue :: Date ( v) => KeyValue :: Date ( v) ,
382449 BasicValue :: Float32 ( _)
383450 | BasicValue :: Float64 ( _)
451+ | BasicValue :: Time ( _)
452+ | BasicValue :: LocalDateTime ( _)
453+ | BasicValue :: OffsetDateTime ( _)
384454 | BasicValue :: Json ( _)
385455 | BasicValue :: Vector ( _) => api_bail ! ( "invalid key value type" ) ,
386456 } ;
@@ -395,8 +465,12 @@ impl BasicValue {
395465 BasicValue :: Int64 ( v) => KeyValue :: Int64 ( * v) ,
396466 BasicValue :: Range ( v) => KeyValue :: Range ( * v) ,
397467 BasicValue :: Uuid ( v) => KeyValue :: Uuid ( * v) ,
468+ BasicValue :: Date ( v) => KeyValue :: Date ( * v) ,
398469 BasicValue :: Float32 ( _)
399470 | BasicValue :: Float64 ( _)
471+ | BasicValue :: Time ( _)
472+ | BasicValue :: LocalDateTime ( _)
473+ | BasicValue :: OffsetDateTime ( _)
400474 | BasicValue :: Json ( _)
401475 | BasicValue :: Vector ( _) => api_bail ! ( "invalid key value type" ) ,
402476 } ;
@@ -413,6 +487,10 @@ impl BasicValue {
413487 BasicValue :: Float64 ( _) => "float64" ,
414488 BasicValue :: Range ( _) => "range" ,
415489 BasicValue :: Uuid ( _) => "uuid" ,
490+ BasicValue :: Date ( _) => "date" ,
491+ BasicValue :: Time ( _) => "time" ,
492+ BasicValue :: LocalDateTime ( _) => "local_datetime" ,
493+ BasicValue :: OffsetDateTime ( _) => "offset_datetime" ,
416494 BasicValue :: Json ( _) => "json" ,
417495 BasicValue :: Vector ( _) => "vector" ,
418496 }
@@ -445,6 +523,7 @@ impl From<KeyValue> for Value {
445523 KeyValue :: Int64 ( v) => Value :: Basic ( BasicValue :: Int64 ( v) ) ,
446524 KeyValue :: Range ( v) => Value :: Basic ( BasicValue :: Range ( v) ) ,
447525 KeyValue :: Uuid ( v) => Value :: Basic ( BasicValue :: Uuid ( v) ) ,
526+ KeyValue :: Date ( v) => Value :: Basic ( BasicValue :: Date ( v) ) ,
448527 KeyValue :: Struct ( v) => Value :: Struct ( FieldValues {
449528 fields : v. into_iter ( ) . map ( Value :: from) . collect ( ) ,
450529 } ) ,
@@ -744,6 +823,12 @@ impl serde::Serialize for BasicValue {
744823 BasicValue :: Float64 ( v) => serializer. serialize_f64 ( * v) ,
745824 BasicValue :: Range ( v) => v. serialize ( serializer) ,
746825 BasicValue :: Uuid ( v) => serializer. serialize_str ( & v. to_string ( ) ) ,
826+ BasicValue :: Date ( v) => serializer. serialize_str ( & v. to_string ( ) ) ,
827+ BasicValue :: Time ( v) => serializer. serialize_str ( & v. to_string ( ) ) ,
828+ BasicValue :: LocalDateTime ( v) => serializer. serialize_str ( & v. to_string ( ) ) ,
829+ BasicValue :: OffsetDateTime ( v) => {
830+ serializer. serialize_str ( & v. to_rfc3339_opts ( chrono:: SecondsFormat :: AutoSi , true ) )
831+ }
747832 BasicValue :: Json ( v) => v. serialize ( serializer) ,
748833 BasicValue :: Vector ( v) => v. serialize ( serializer) ,
749834 }
@@ -774,8 +859,14 @@ impl BasicValue {
774859 . ok_or_else ( || anyhow:: anyhow!( "invalid fp64 value {v}" ) ) ?,
775860 ) ,
776861 ( v, BasicValueType :: Range ) => BasicValue :: Range ( serde_json:: from_value ( v) ?) ,
777- ( serde_json:: Value :: String ( v) , BasicValueType :: Uuid ) => {
778- BasicValue :: Uuid ( uuid:: Uuid :: parse_str ( v. as_str ( ) ) ?)
862+ ( serde_json:: Value :: String ( v) , BasicValueType :: Uuid ) => BasicValue :: Uuid ( v. parse ( ) ?) ,
863+ ( serde_json:: Value :: String ( v) , BasicValueType :: Date ) => BasicValue :: Date ( v. parse ( ) ?) ,
864+ ( serde_json:: Value :: String ( v) , BasicValueType :: Time ) => BasicValue :: Time ( v. parse ( ) ?) ,
865+ ( serde_json:: Value :: String ( v) , BasicValueType :: LocalDateTime ) => {
866+ BasicValue :: LocalDateTime ( v. parse ( ) ?)
867+ }
868+ ( serde_json:: Value :: String ( v) , BasicValueType :: OffsetDateTime ) => {
869+ BasicValue :: OffsetDateTime ( chrono:: DateTime :: parse_from_rfc3339 ( & v) ?)
779870 }
780871 ( v, BasicValueType :: Json ) => BasicValue :: Json ( Arc :: from ( v) ) ,
781872 (
0 commit comments