gremlin_client/structure/
vertex_property.rs

1use crate::structure::{GValue, Property, GID};
2use crate::{GremlinError, GremlinResult};
3
4use crate::conversion::{BorrowFromGValue, FromGValue};
5
6#[derive(Debug, PartialEq, Clone)]
7pub enum GProperty {
8    VertexProperty(VertexProperty),
9    Property(Property),
10}
11
12impl GProperty {
13    pub fn value(&self) -> &GValue {
14        match self {
15            GProperty::Property(p) => p.value(),
16            GProperty::VertexProperty(p) => p.value(),
17        }
18    }
19
20    pub fn take<T>(self) -> GremlinResult<T>
21    where
22        T: FromGValue,
23    {
24        match self {
25            GProperty::Property(p) => p.take(),
26            GProperty::VertexProperty(p) => p.take(),
27        }
28    }
29
30    pub fn get<'a, T>(&'a self) -> GremlinResult<&'a T>
31    where
32        T: BorrowFromGValue,
33    {
34        match self {
35            GProperty::Property(p) => p.get(),
36            GProperty::VertexProperty(p) => p.get(),
37        }
38    }
39
40    pub fn label(&self) -> &String {
41        match self {
42            GProperty::Property(p) => p.label(),
43            GProperty::VertexProperty(p) => p.label(),
44        }
45    }
46}
47
48impl FromGValue for GProperty {
49    fn from_gvalue(v: GValue) -> GremlinResult<Self> {
50        match v {
51            GValue::VertexProperty(p) => Ok(GProperty::VertexProperty(p)),
52            GValue::Property(p) => Ok(GProperty::Property(p)),
53            _ => Err(GremlinError::Cast(String::from(
54                "Value not allowed for a property",
55            ))),
56        }
57    }
58}
59#[derive(Debug, PartialEq, Clone)]
60pub struct VertexProperty {
61    label: String,
62    id: GID,
63    value: Box<GValue>,
64}
65
66impl VertexProperty {
67    pub fn new<G, T, GT>(id: G, label: T, value: GT) -> VertexProperty
68    where
69        G: Into<GID>,
70        T: Into<String>,
71        GT: Into<GValue>,
72    {
73        VertexProperty {
74            id: id.into(),
75            label: label.into(),
76            value: Box::new(value.into()),
77        }
78    }
79
80    pub fn id(&self) -> &GID {
81        &self.id
82    }
83
84    pub fn value(&self) -> &GValue {
85        &self.value
86    }
87
88    pub fn take<T>(self) -> GremlinResult<T>
89    where
90        T: FromGValue,
91    {
92        T::from_gvalue(*self.value)
93    }
94
95    pub fn get<'a, T>(&'a self) -> GremlinResult<&'a T>
96    where
97        T: BorrowFromGValue,
98    {
99        T::from_gvalue(&self.value)
100    }
101    pub fn label(&self) -> &String {
102        &self.label
103    }
104}