use async_trait::async_trait;
use crate::error::GraphError;
use crate::types::Document;
use super::types::{
Direction, EntityId, EntityType, GraphEntity, GraphPath, GraphQuery, GraphRelationship,
RelationshipType,
};
#[async_trait]
pub trait EntityExtractor: Send + Sync {
async fn extract_entities(&self, text: &str) -> Result<Vec<GraphEntity>, GraphError>;
fn supported_entity_types(&self) -> Vec<EntityType>;
}
#[async_trait]
pub trait RelationshipExtractor: Send + Sync {
async fn extract_relationships(
&self,
text: &str,
entities: &[GraphEntity],
) -> Result<Vec<GraphRelationship>, GraphError>;
fn supported_relationship_types(&self) -> Vec<RelationshipType>;
}
#[async_trait]
pub trait GraphStore: Send + Sync {
async fn add_entity(&mut self, entity: GraphEntity) -> Result<EntityId, GraphError>;
async fn add_entities(
&mut self,
entities: Vec<GraphEntity>,
) -> Result<Vec<EntityId>, GraphError>;
async fn add_relationship(
&mut self,
relationship: GraphRelationship,
) -> Result<String, GraphError>;
async fn add_relationships(
&mut self,
relationships: Vec<GraphRelationship>,
) -> Result<Vec<String>, GraphError>;
async fn get_entity(&self, id: &EntityId) -> Result<Option<GraphEntity>, GraphError>;
async fn get_neighbors(
&self,
id: &EntityId,
direction: Direction,
) -> Result<Vec<(GraphRelationship, GraphEntity)>, GraphError>;
async fn find_entities_by_type(
&self,
entity_type: &EntityType,
) -> Result<Vec<GraphEntity>, GraphError>;
async fn find_entities_by_name(&self, name: &str) -> Result<Vec<GraphEntity>, GraphError>;
async fn traverse(&self, query: &GraphQuery) -> Result<Vec<GraphPath>, GraphError>;
async fn entity_count(&self) -> usize;
async fn relationship_count(&self) -> usize;
async fn clear(&mut self) -> Result<(), GraphError>;
}
#[async_trait]
pub trait Graph: Send + Sync {
async fn index_document(&mut self, document: &Document) -> Result<(), GraphError>;
async fn index_documents(&mut self, documents: &[Document]) -> Result<(), GraphError>;
async fn query(&self, query: &GraphQuery) -> Result<Vec<GraphPath>, GraphError>;
async fn find_related(
&self,
entity_name: &str,
max_hops: usize,
) -> Result<Vec<GraphPath>, GraphError>;
async fn entity_count(&self) -> usize;
async fn relationship_count(&self) -> usize;
async fn clear(&mut self) -> Result<(), GraphError>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_trait_object_safety() {
fn _assert_entity_extractor_object_safe(_: &dyn EntityExtractor) {}
fn _assert_relationship_extractor_object_safe(_: &dyn RelationshipExtractor) {}
fn _assert_graph_store_object_safe(_: &dyn GraphStore) {}
fn _assert_graph_object_safe(_: &dyn Graph) {}
}
}