1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use chrono::NaiveDateTime;
use firestore_grpc::v1::{value::ValueType, ArrayValue, MapValue, Value as FsValue};
use prost_types::Timestamp;
use std::collections::HashMap;

#[derive(Default, Clone)]
pub struct FromValues {
    fields: HashMap<String, FsValue>,
}
impl FromValues {
    pub fn get_string(self, key: &str) -> String {
        let value = self.fields.get(&key.to_string());

        match value {
            Some(value) => {
                let value_type = value.value_type.clone().unwrap();

                match value_type {
                    ValueType::StringValue(value) => value,
                    _ => "".to_string(),
                }
            }
            None => "".to_string(),
        }
    }
}
pub fn from_values(fields: HashMap<String, FsValue>) -> FromValues {
    FromValues { fields }
}

#[derive(Default)]
pub struct ToValues {
    fields: HashMap<String, FsValue>,
}
impl ToValues {
    pub fn add(mut self, key: &str, value: FsValue) -> Self {
        self.fields.insert(key.to_string(), value);
        self
    }

    pub fn get_fields(self) -> HashMap<String, FsValue> {
        self.fields
    }
}

pub fn to_values() -> ToValues {
    ToValues {
        fields: HashMap::new(),
    }
}

pub struct Value {}
impl Value {
    pub fn null() -> FsValue {
        FsValue {
            value_type: Some(ValueType::NullValue(0)),
        }
    }

    pub fn boolean(value: bool) -> FsValue {
        FsValue {
            value_type: Some(ValueType::BooleanValue(value)),
        }
    }

    pub fn integer(value: i64) -> FsValue {
        FsValue {
            value_type: Some(ValueType::IntegerValue(value)),
        }
    }

    pub fn double(value: f64) -> FsValue {
        FsValue {
            value_type: Some(ValueType::DoubleValue(value)),
        }
    }

    pub fn timestamp(value: NaiveDateTime) -> FsValue {
        FsValue {
            value_type: Some(ValueType::TimestampValue(Timestamp {
                seconds: value.timestamp(),
                nanos: value.timestamp_subsec_nanos() as i32,
            })),
        }
    }

    pub fn string(value: &str) -> FsValue {
        FsValue {
            value_type: Some(ValueType::StringValue(value.to_string())),
        }
    }

    // pub fn bytes(value: Vec) -> FsValue {
    //     FsValue {
    //         value_type: Some(ValueType::BytesValue(value)),
    //     }
    // }

    pub fn reference(value: String) -> FsValue {
        FsValue {
            value_type: Some(ValueType::ReferenceValue(value)),
        }
    }

    // pub fn geo_point(value: LatLng) -> FsValue {
    //     FsValue {
    //         value_type: Some(ValueType::GeoPointValue(value)),
    //     }
    // }

    pub fn array(value: ArrayValue) -> FsValue {
        FsValue {
            value_type: Some(ValueType::ArrayValue(value)),
        }
    }

    pub fn map(value: MapValue) -> FsValue {
        FsValue {
            value_type: Some(ValueType::MapValue(value)),
        }
    }
}