firestore_sdk/
store_field.rs1use chrono::NaiveDateTime;
2use firestore_grpc::v1::{value::ValueType, ArrayValue, MapValue, Value as FsValue};
3use prost_types::Timestamp;
4use std::collections::HashMap;
5
6#[derive(Default, Clone)]
7pub struct FromValues {
8 fields: HashMap<String, FsValue>,
9}
10impl FromValues {
11 pub fn get_string(self, key: &str) -> String {
12 let value = self.fields.get(&key.to_string());
13
14 match value {
15 Some(value) => {
16 let value_type = value.value_type.clone().unwrap();
17
18 match value_type {
19 ValueType::StringValue(value) => value,
20 _ => "".to_string(),
21 }
22 }
23 None => "".to_string(),
24 }
25 }
26}
27pub fn from_values(fields: HashMap<String, FsValue>) -> FromValues {
28 FromValues { fields }
29}
30
31#[derive(Default)]
32pub struct ToValues {
33 fields: HashMap<String, FsValue>,
34}
35impl ToValues {
36 pub fn add(mut self, key: &str, value: FsValue) -> Self {
37 self.fields.insert(key.to_string(), value);
38 self
39 }
40
41 pub fn get_fields(self) -> HashMap<String, FsValue> {
42 self.fields
43 }
44}
45
46pub fn to_values() -> ToValues {
47 ToValues {
48 fields: HashMap::new(),
49 }
50}
51
52pub struct Value {}
53impl Value {
54 pub fn null() -> FsValue {
55 FsValue {
56 value_type: Some(ValueType::NullValue(0)),
57 }
58 }
59
60 pub fn boolean(value: bool) -> FsValue {
61 FsValue {
62 value_type: Some(ValueType::BooleanValue(value)),
63 }
64 }
65
66 pub fn integer(value: i64) -> FsValue {
67 FsValue {
68 value_type: Some(ValueType::IntegerValue(value)),
69 }
70 }
71
72 pub fn double(value: f64) -> FsValue {
73 FsValue {
74 value_type: Some(ValueType::DoubleValue(value)),
75 }
76 }
77
78 pub fn timestamp(value: NaiveDateTime) -> FsValue {
79 FsValue {
80 value_type: Some(ValueType::TimestampValue(Timestamp {
81 seconds: value.timestamp(),
82 nanos: value.timestamp_subsec_nanos() as i32,
83 })),
84 }
85 }
86
87 pub fn string(value: &str) -> FsValue {
88 FsValue {
89 value_type: Some(ValueType::StringValue(value.to_string())),
90 }
91 }
92
93 pub fn reference(value: String) -> FsValue {
100 FsValue {
101 value_type: Some(ValueType::ReferenceValue(value)),
102 }
103 }
104
105 pub fn array(value: ArrayValue) -> FsValue {
112 FsValue {
113 value_type: Some(ValueType::ArrayValue(value)),
114 }
115 }
116
117 pub fn map(value: MapValue) -> FsValue {
118 FsValue {
119 value_type: Some(ValueType::MapValue(value)),
120 }
121 }
122}