use uuid::Uuid;
use crate::types::{NodeId, PropertyValue, NodeKind};
#[derive(Debug, Clone)]
pub enum Target {
Node(NodeId),
Class(String),
}
#[derive(Debug, Clone, PartialEq)]
pub enum DatatypeKind {
Int,
Float,
Str,
Bool,
Bytes,
Null,
}
impl DatatypeKind {
pub fn matches(&self, value: &PropertyValue) -> bool {
matches!(
(self, value),
(DatatypeKind::Int, PropertyValue::Int(_))
| (DatatypeKind::Float, PropertyValue::Float(_))
| (DatatypeKind::Str, PropertyValue::String(_))
| (DatatypeKind::Bool, PropertyValue::Bool(_))
| (DatatypeKind::Bytes, PropertyValue::Bytes(_))
| (DatatypeKind::Null, PropertyValue::Null)
)
}
}
#[derive(Debug, Clone)]
pub enum ConstraintType {
MinCount(usize),
MaxCount(usize),
Datatype(DatatypeKind),
MinInclusive(f64),
MaxInclusive(f64),
MinExclusive(f64),
MaxExclusive(f64),
MinLength(usize),
MaxLength(usize),
Pattern(String),
In(Vec<PropertyValue>),
HasValue(PropertyValue),
NodeKindConstraint(NodeKind),
Class(String),
}
#[derive(Debug, Clone)]
pub enum PathSpec {
Property(String),
Edge(String),
}
impl PathSpec {
pub fn as_str(&self) -> &str {
match self {
PathSpec::Property(s) | PathSpec::Edge(s) => s.as_str(),
}
}
}
#[derive(Debug, Clone)]
pub struct PropertyShape {
pub path: PathSpec,
pub constraints: Vec<ConstraintType>,
}
#[derive(Debug, Clone)]
pub struct Shape {
pub id: NodeId,
pub name: String,
pub targets: Vec<Target>,
pub constraints: Vec<ConstraintType>,
pub property_shapes: Vec<PropertyShape>,
}
impl Shape {
pub fn new(name: impl Into<String>) -> Self {
Self {
id: Uuid::new_v4(),
name: name.into(),
targets: vec![],
constraints: vec![],
property_shapes: vec![],
}
}
pub fn with_target(mut self, target: Target) -> Self {
self.targets.push(target);
self
}
pub fn with_constraint(mut self, constraint: ConstraintType) -> Self {
self.constraints.push(constraint);
self
}
pub fn with_property_shape(mut self, ps: PropertyShape) -> Self {
self.property_shapes.push(ps);
self
}
}
impl PropertyShape {
pub fn new(path: PathSpec, constraints: Vec<ConstraintType>) -> Self {
Self { path, constraints }
}
}