Skip to main content

systemprompt_identifiers/db_value/
value.rs

1//! `DbValue`: the database-agnostic value enum bridged by the conversion
2//! traits.
3//!
4//! Copyright (c) systemprompt.io — Business Source License 1.1.
5//! See <https://systemprompt.io> for licensing details.
6
7use chrono::{DateTime, Utc};
8use std::collections::HashMap;
9
10pub type JsonRow = HashMap<String, serde_json::Value>;
11
12#[must_use]
13pub fn parse_database_datetime(value: &serde_json::Value) -> Option<DateTime<Utc>> {
14    if let Some(s) = value.as_str() {
15        if let Ok(dt) = DateTime::parse_from_rfc3339(s) {
16            return Some(dt.with_timezone(&Utc));
17        }
18
19        let with_tz = format!("{s}+00:00");
20        if let Ok(dt) = DateTime::parse_from_str(&with_tz, "%Y-%m-%d %H:%M:%S%.f%:z") {
21            return Some(dt.with_timezone(&Utc));
22        }
23
24        None
25    } else if let Some(ts) = value.as_i64() {
26        DateTime::from_timestamp(ts, 0)
27    } else {
28        None
29    }
30}
31
32#[derive(Debug, Clone, PartialEq)]
33pub enum DbValue {
34    String(String),
35    Int(i64),
36    Float(f64),
37    Bool(bool),
38    Bytes(Vec<u8>),
39    Timestamp(DateTime<Utc>),
40    StringArray(Vec<String>),
41    NullString,
42    NullInt,
43    NullFloat,
44    NullBool,
45    NullBytes,
46    NullTimestamp,
47    NullStringArray,
48}