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