solomon_gremlin/structure/
vertex_property.rs

1use 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/// ## VertexProperty
62/// ### Description
63/// A VertexProperty is similar to a Property in that it denotes a key/value pair associated with an Vertex,
64/// however it is different in the sense that it also represents an entity that it is an Element that
65/// can have properties of its own.
66///
67/// ### TinkerPop Documentation
68/// TinkerPop introduces the concept of a VertexProperty<V>. All the properties of a Vertex are a VertexProperty.
69/// A VertexProperty implements Property and as such, it has a key/value pair. However, VertexProperty also implements
70/// Element and thus, can have a collection of key/value pairs. Moreover, while an Edge can only have one property of
71/// key "name" (for example), a Vertex can have multiple "name" properties. With the inclusion of vertex properties,
72/// two features are introduced which ultimately advance the graph modelers toolkit:
73#[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}