Skip to main content

rust_dynamic/
value.rs

1use std::collections::HashMap;
2use serde::{Deserialize, Serialize};
3use std::time::{SystemTime, UNIX_EPOCH};
4use nanoid::nanoid;
5use crate::types::*;
6
7pub fn timestamp_ms() -> f64 {
8    SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as f64
9}
10
11pub fn timestamp_ns() -> u128 {
12    SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos() as u128
13}
14
15#[derive(Clone, Debug, Serialize, Deserialize)]
16pub struct Value {
17    pub id:     String,
18    pub stamp:  f64,
19    pub dt:     u16,
20    pub q:      f64,
21    pub data:   Val,
22    pub attr:   Vec::<Value>,
23    pub curr:   i32,
24    pub tags:   HashMap<String, String>,
25}
26
27#[derive(Clone, Debug)]
28pub struct Applicative {
29    pub f:      AppFn,
30}
31
32impl Value {
33    pub fn new() -> Self {
34        Self {
35            id:     nanoid!(),
36            stamp:  timestamp_ms(),
37            dt:     NONE,
38            q:      0.0,
39            data:   Val::Null,
40            attr:   Vec::new(),
41            curr:   -1,
42            tags:   HashMap::new(),
43        }
44    }
45}
46
47impl Applicative {
48    pub fn new(f: AppFn) -> Self {
49        Self {
50            f:     f,
51        }
52    }
53}