gremlin_client/structure/
gid.rs

1use crate::{GremlinError, GremlinResult};
2use uuid::Uuid;
3
4#[derive(Debug, Clone)]
5pub struct GIDs(pub(crate) Vec<GID>);
6
7impl<T: Into<GID>> From<T> for GIDs {
8    fn from(val: T) -> GIDs {
9        GIDs(vec![val.into()])
10    }
11}
12
13impl<T: Into<GID>> From<Vec<T>> for GIDs {
14    fn from(val: Vec<T>) -> GIDs {
15        GIDs(val.into_iter().map(|gid| gid.into()).collect())
16    }
17}
18
19impl From<()> for GIDs {
20    fn from(_val: ()) -> GIDs {
21        GIDs(vec![])
22    }
23}
24
25#[derive(Debug, PartialEq, Eq, Hash, Clone)]
26pub enum GID {
27    String(String),
28    Int32(i32),
29    Int64(i64),
30}
31
32impl GID {
33    pub fn get<'a, T>(&'a self) -> GremlinResult<&'a T>
34    where
35        T: BorrowFromGID,
36    {
37        T::from_gid(self)
38    }
39}
40
41impl From<&'static str> for GID {
42    fn from(val: &str) -> Self {
43        GID::String(String::from(val))
44    }
45}
46
47impl From<String> for GID {
48    fn from(val: String) -> Self {
49        GID::String(val)
50    }
51}
52
53impl From<i32> for GID {
54    fn from(val: i32) -> Self {
55        GID::Int32(val)
56    }
57}
58
59impl From<i64> for GID {
60    fn from(val: i64) -> Self {
61        GID::Int64(val)
62    }
63}
64
65impl From<&GID> for GID {
66    fn from(val: &GID) -> Self {
67        val.clone()
68    }
69}
70
71impl From<Uuid> for GID {
72    fn from(val: Uuid) -> Self {
73        GID::String(val.to_string())
74    }
75}
76
77// Borrow from GID
78
79#[doc(hidden)]
80pub trait BorrowFromGID: Sized {
81    fn from_gid<'a>(v: &'a GID) -> GremlinResult<&'a Self>;
82}
83
84macro_rules! impl_borrow_from_gid {
85    ($t:ty, $v:path) => {
86        impl BorrowFromGID for $t {
87            fn from_gid<'a>(v: &'a GID) -> GremlinResult<&'a $t> {
88                match v {
89                    $v(e) => Ok(e),
90                    _ => Err(GremlinError::Cast(format!(
91                        "Cannot convert {:?} to {}",
92                        v,
93                        stringify!($t)
94                    ))),
95                }
96            }
97        }
98    };
99}
100
101impl_borrow_from_gid!(String, GID::String);
102impl_borrow_from_gid!(i32, GID::Int32);
103impl_borrow_from_gid!(i64, GID::Int64);