engula_apis/
lib.rs

1// Copyright 2022 The Engula Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![allow(clippy::all)]
16
17tonic::include_proto!("engula.v1");
18
19use std::collections::HashMap;
20
21pub type Value = generic_value::Value;
22
23impl<T: Into<Value>> From<T> for GenericValue {
24    fn from(v: T) -> Self {
25        Self {
26            value: Some(v.into()),
27        }
28    }
29}
30
31impl From<Option<Value>> for GenericValue {
32    fn from(v: Option<Value>) -> Self {
33        Self { value: v }
34    }
35}
36
37impl From<GenericValue> for Option<Value> {
38    fn from(v: GenericValue) -> Self {
39        v.value
40    }
41}
42
43impl From<i64> for Value {
44    fn from(v: i64) -> Self {
45        Self::I64Value(v)
46    }
47}
48
49impl TryFrom<Value> for i64 {
50    type Error = Value;
51
52    fn try_from(v: Value) -> Result<Self, Self::Error> {
53        if let Value::I64Value(v) = v {
54            Ok(v)
55        } else {
56            Err(v)
57        }
58    }
59}
60
61impl From<&[u8]> for Value {
62    fn from(v: &[u8]) -> Self {
63        Self::BlobValue(v.to_owned())
64    }
65}
66
67impl From<Vec<u8>> for Value {
68    fn from(v: Vec<u8>) -> Self {
69        Self::BlobValue(v)
70    }
71}
72
73impl From<&str> for Value {
74    fn from(v: &str) -> Self {
75        Self::TextValue(v.to_owned())
76    }
77}
78
79impl From<String> for Value {
80    fn from(v: String) -> Self {
81        Self::TextValue(v)
82    }
83}
84
85impl From<MappingValue> for Value {
86    fn from(v: MappingValue) -> Self {
87        Value::MappingValue(v)
88    }
89}
90
91impl<K, V> From<HashMap<K, V>> for Value
92where
93    K: Into<Value> + Ord,
94    V: Into<Value>,
95{
96    fn from(map: HashMap<K, V>) -> Self {
97        let (keys, values) = map.into_iter().fold(
98            (Vec::new(), Vec::new()),
99            |(mut keys, mut values), (k, v)| {
100                keys.push(k.into().into());
101                values.push(v.into().into());
102                (keys, values)
103            },
104        );
105        MappingValue { keys, values }.into()
106    }
107}
108
109impl From<RepeatedValue> for Value {
110    fn from(v: RepeatedValue) -> Self {
111        Self::RepeatedValue(v)
112    }
113}
114
115impl<T> From<Vec<T>> for Value
116where
117    T: Into<Value>,
118{
119    fn from(values: Vec<T>) -> Self {
120        RepeatedValue {
121            values: values.into_iter().map(|v| v.into().into()).collect(),
122        }
123        .into()
124    }
125}