use std::sync::{Arc, RwLock};
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Target {
pub r#type: String,
pub id: String,
}
impl Target {
pub fn new(r#type: &str, id: &str) -> Target {
Target {
r#type: r#type.to_owned(),
id: id.to_owned(),
}
}
}
pub struct RelationEvent {
pub data: Value,
pub aggregate_id: String,
pub tenant: String,
}
pub fn ids(value: &Value) -> Vec<String> {
match value {
Value::String(s) if !s.is_empty() => vec![s.clone()],
Value::Array(a) => a
.iter()
.filter_map(|v| v.as_str().map(str::to_owned))
.collect(),
_ => vec![],
}
}
type Ids = Arc<dyn Fn(&RelationEvent) -> Vec<String> + Send + Sync>;
type ParentOf = Arc<dyn Fn(&RelationEvent) -> Option<Target> + Send + Sync>;
#[derive(Clone)]
pub(crate) struct RelationRule {
pub r#type: String,
pub name: String,
pub event: String,
pub set: bool,
pub ids: Ids,
}
#[derive(Clone)]
pub(crate) struct ParentRule {
pub r#type: String,
pub event: String,
pub parent: ParentOf,
}
static RELATIONS: RwLock<Vec<RelationRule>> = RwLock::new(Vec::new());
static PARENTS: RwLock<Vec<ParentRule>> = RwLock::new(Vec::new());
fn qualified(t: &str) -> bool {
t.split_once('.').is_some_and(|(d, n)| {
d.starts_with(|c: char| c.is_ascii_lowercase())
&& [d, n].iter().all(|p| {
!p.is_empty()
&& p.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
})
})
}
fn check(r#type: &str, event: &str) {
assert!(
qualified(r#type),
"a relation's aggregate type is qualified (shop.order): {type}"
);
assert!(
qualified(event),
"a relation's event type is qualified (shop.OrderPlaced): {event}"
);
}
pub struct Relation {
r#type: String,
name: String,
}
impl Relation {
fn rule(
self,
event: &str,
set: bool,
ids: impl Fn(&RelationEvent) -> Vec<String> + Send + Sync + 'static,
) -> Relation {
check(&self.r#type, event);
RELATIONS.write().unwrap().push(RelationRule {
r#type: self.r#type.clone(),
name: self.name.clone(),
event: event.to_owned(),
set,
ids: Arc::new(ids),
});
self
}
pub fn set(
self,
event: &str,
ids: impl Fn(&RelationEvent) -> Vec<String> + Send + Sync + 'static,
) -> Relation {
self.rule(event, true, ids)
}
pub fn unset(
self,
event: &str,
ids: impl Fn(&RelationEvent) -> Vec<String> + Send + Sync + 'static,
) -> Relation {
self.rule(event, false, ids)
}
}
pub fn relation(r#type: &str, name: &str) -> Relation {
let lower = name.starts_with(|c: char| c.is_ascii_lowercase())
&& name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_');
assert!(lower, "a relation's name is lowercase: {name}");
assert!(!matches!(name, "self" | "owners"), "{name} is built in");
Relation {
r#type: r#type.to_owned(),
name: name.to_owned(),
}
}
pub struct Parent {
r#type: String,
}
impl Parent {
pub fn from(
self,
event: &str,
parent: impl Fn(&RelationEvent) -> Option<Target> + Send + Sync + 'static,
) {
check(&self.r#type, event);
PARENTS.write().unwrap().push(ParentRule {
r#type: self.r#type,
event: event.to_owned(),
parent: Arc::new(parent),
});
}
}
pub fn parent(r#type: &str) -> Parent {
Parent {
r#type: r#type.to_owned(),
}
}
pub(crate) fn relation_rules() -> Vec<RelationRule> {
RELATIONS.read().unwrap().clone()
}
pub(crate) fn parent_rules() -> Vec<ParentRule> {
PARENTS.read().unwrap().clone()
}
pub fn relation_events() -> Vec<String> {
let mut all: Vec<String> = relation_rules()
.into_iter()
.map(|r| r.event)
.chain(parent_rules().into_iter().map(|p| p.event))
.collect();
all.sort();
all.dedup();
all
}