Skip to main content

systemprompt_identifiers/db_value/
value.rs

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