use crate::script::*;
use crate::value::{EdgeValue, VertexRef, VertexValue};
#[derive(Debug, Clone)]
pub struct ScriptVertex(pub(crate) VertexValue);
impl ScriptVertex {
pub fn as_vertex_value(&self) -> &VertexValue {
&self.0
}
}
#[derive(Debug, Clone)]
pub struct ScriptEdge(pub(crate) EdgeValue);
impl ScriptEdge {
pub fn as_edge_value(&self) -> &EdgeValue {
&self.0
}
}
#[derive(Debug, Clone)]
pub struct ScriptVertexRef(pub(crate) VertexRef);
impl ScriptVertexRef {
pub fn as_vertex_ref(&self) -> &VertexRef {
&self.0
}
}
fn text_or_unit(text: Option<&str>) -> Dynamic {
text.map_or(Dynamic::UNIT, |text| Dynamic::from(text.to_string()))
}
fn properties_to_map(properties: &crate::value::PropertyMap) -> rhai::Map {
properties
.iter()
.map(|(key, value)| {
(
key.as_str().into(),
element_property_to_dynamic(value.clone()),
)
})
.collect()
}
fn property_of(properties: &crate::value::PropertyMap, key: &str) -> Dynamic {
properties.get(key).map_or(Dynamic::UNIT, |value| {
element_property_to_dynamic(value.clone())
})
}
pub(crate) fn vertex_text(vertex: &VertexValue) -> String {
format!("v[{}]", vertex.id())
}
pub(crate) fn edge_text(edge: &EdgeValue) -> String {
format!(
"e[{}][{}-{}->{}]",
edge.id(),
edge.out_v().id(),
edge.label().unwrap_or(""),
edge.in_v().id()
)
}
pub(crate) fn register_element_types(registry: &mut crate::script::register::Registry) {
registry
.register_type_with_name::<ScriptVertex>("Vertex")
.register_get("id", |v: &mut ScriptVertex| v.0.id().to_string())
.register_get("label", |v: &mut ScriptVertex| text_or_unit(v.0.label()))
.register_get("labels", |v: &mut ScriptVertex| {
v.0.labels()
.iter()
.map(|label| Dynamic::from(label.to_string()))
.collect::<rhai::Array>()
})
.register_get("properties", |v: &mut ScriptVertex| {
properties_to_map(v.0.properties())
})
.register_indexer_get(|v: &mut ScriptVertex, key: &str| property_of(v.0.properties(), key))
.register_fn_as(&["to_string", "to_debug"], |v: &mut ScriptVertex| {
vertex_text(&v.0)
})
.register_fn("==", |a: ScriptVertex, b: ScriptVertex| {
a.0.id() == b.0.id()
})
.register_fn("!=", |a: ScriptVertex, b: ScriptVertex| {
a.0.id() != b.0.id()
});
registry
.register_type_with_name::<ScriptEdge>("Edge")
.register_get("id", |e: &mut ScriptEdge| e.0.id().to_string())
.register_get("label", |e: &mut ScriptEdge| text_or_unit(e.0.label()))
.register_get("out_v", |e: &mut ScriptEdge| {
ScriptVertexRef(e.0.out_v().clone())
})
.register_get("outV", |e: &mut ScriptEdge| {
ScriptVertexRef(e.0.out_v().clone())
})
.register_get("in_v", |e: &mut ScriptEdge| {
ScriptVertexRef(e.0.in_v().clone())
})
.register_get("inV", |e: &mut ScriptEdge| {
ScriptVertexRef(e.0.in_v().clone())
})
.register_get("properties", |e: &mut ScriptEdge| {
properties_to_map(e.0.properties())
})
.register_indexer_get(|e: &mut ScriptEdge, key: &str| property_of(e.0.properties(), key))
.register_fn_as(&["to_string", "to_debug"], |e: &mut ScriptEdge| {
edge_text(&e.0)
})
.register_fn("==", |a: ScriptEdge, b: ScriptEdge| a.0.id() == b.0.id())
.register_fn("!=", |a: ScriptEdge, b: ScriptEdge| a.0.id() != b.0.id());
registry
.register_type_with_name::<ScriptVertexRef>("VertexRef")
.register_get("id", |v: &mut ScriptVertexRef| v.0.id().to_string())
.register_get("label", |v: &mut ScriptVertexRef| text_or_unit(v.0.label()))
.register_fn_as(&["to_string", "to_debug"], |v: &mut ScriptVertexRef| {
format!("v[{}]", v.0.id())
})
.register_fn("==", |a: ScriptVertexRef, b: ScriptVertexRef| {
a.0.id() == b.0.id()
})
.register_fn("!=", |a: ScriptVertexRef, b: ScriptVertexRef| {
a.0.id() != b.0.id()
})
.register_fn("==", |a: ScriptVertex, b: ScriptVertexRef| {
a.0.id() == b.0.id()
})
.register_fn("==", |a: ScriptVertexRef, b: ScriptVertex| {
a.0.id() == b.0.id()
});
}