use crate::graph::GraphStorage;
use crate::*;
pub(crate) const PROPERTY_ELEMENT_ID_REASON: &str = "property elements have no id in Graphersal: a vertex property is identified by its vertex id and key";
#[derive(Default, Clone)]
struct StepId {}
impl fmt::Display for StepId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "id()")
}
}
impl<'g, S: GraphStorage + 'g> GraphTraversalSource<'g, S> {
pub fn id(self) -> Self {
self.push_step(GraphStep::Map("id", Box::new(StepId::default())))
}
}
impl __ {
pub fn id<'g, S: GraphStorage + 'g>() -> AnonymousTraversal<'g, S> {
AnonymousTraversal::new().id()
}
}
impl<'g, S: GraphStorage + 'g> AnonymousTraversal<'g, S> {
pub fn id(self) -> Self {
let step = GraphStep::Map("id", Box::new(StepId::default()));
self.push_step(step)
}
}
impl<'g, S: GraphStorage + 'g> GraphStepMap<'g, S> for StepId {
#[inline]
fn process(
&self,
context: &mut TraverserContext<'_, S>,
source: Traverser<S>,
) -> TraverserResult<Traverser<S>> {
match &source.value {
TraverserValue::Vertex(node_index) if !context.storage.contains_vertex(*node_index) => {
Err(Box::new(GraphError::vertex_removed().into()))
}
TraverserValue::Edge(edge_index) if !context.storage.contains_edge(*edge_index) => {
Err(Box::new(GraphError::edge_removed().into()))
}
TraverserValue::Vertex(node_index) => Ok(Traverser::with_parts(
TraverserValue::VertexProperty(
CompactString::new("id"),
*node_index,
PropertyHandleKind::Value,
),
source.path,
source.bulk,
source.sack,
)),
TraverserValue::Edge(edge_index) => Ok(Traverser::with_parts(
TraverserValue::EdgeProperty(
CompactString::new("id"),
*edge_index,
PropertyHandleKind::Value,
),
source.path,
source.bulk,
source.sack,
)),
TraverserValue::VertexProperty(_, _, PropertyHandleKind::Element)
| TraverserValue::PathProperty(_) => Err(Box::new(TraverserError::QueryExecution {
query: "id".to_string(),
reason: PROPERTY_ELEMENT_ID_REASON.to_string(),
})),
v => Err(Box::new(TraverserError::cast_exception(
&["vertex", "edge"],
v,
))),
}
}
fn clone_box(&self) -> Box<dyn GraphStepMap<'g, S> + 'g> {
Box::new(self.clone())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn v_simple() {
test_dual!(
graph: GraphSource::tinkerpop_modern(),
rust: |g| g.v("1").id().to_list(),
rhai: [
r#"g.V("1").id()"#,
r#"g.v("1").id()"#,
],
assert: |res| assert_eq!(res[0], ElementProperty::String("1".into())),
);
}
#[test]
fn e_simple() {
test_dual!(
graph: GraphSource::tinkerpop_modern(),
rust: |g| g.e("1").id().to_list(),
rhai: [
r#"g.E("1").id()"#,
r#"g.e("1").id()"#,
],
assert: |res| assert_eq!(res[0], ElementProperty::String("1".into())),
);
}
#[test]
fn profile() {
let graph = GraphSource::tinkerpop_modern();
let g_lock = graph.read();
let g = g_lock.traversal();
let result = g.e("1").id().profile().unwrap();
assert!(result.to_string().contains("id("));
}
}