use crate::{Entity, Record, TeaqlEntity};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EntityGraphOperation {
Save,
Delete,
}
#[derive(Debug, Clone)]
pub struct EntityGraphNode {
pub entity_type: String,
pub record: Record,
pub comment: Option<String>,
pub operation: EntityGraphOperation,
pub children: Vec<(String, EntityGraphNode)>,
}
pub struct EntityGraphBuilder {
node: EntityGraphNode,
}
impl EntityGraphBuilder {
pub fn comment(mut self, comment: impl Into<String>) -> Self {
self.node.comment = Some(comment.into());
self
}
pub fn delete(mut self) -> Self {
self.node.operation = EntityGraphOperation::Delete;
self
}
pub fn child(mut self, relation: impl Into<String>, child: EntityGraphBuilder) -> Self {
self.node.children.push((relation.into(), child.node));
self
}
pub fn build(self) -> EntityGraph {
EntityGraph { root: self.node }
}
}
pub struct EntityGraph {
pub root: EntityGraphNode,
}
impl EntityGraph {
pub fn new<T: Entity + TeaqlEntity>(entity: T) -> EntityGraphBuilder {
EntityGraphBuilder {
node: EntityGraphNode {
entity_type: T::entity_descriptor().name.clone(),
record: entity.into_record(),
comment: None,
operation: EntityGraphOperation::Save,
children: Vec::new(),
},
}
}
}