use std::collections::HashSet;
use anyhow::{bail, Result};
use crate::link::Link;
use crate::link_storage::ChangeObserver;
use crate::named_type_links::NamedTypeLinks;
pub const ROOT_TYPE_NAME: &str = "Type";
pub const EXTENDS_NAME: &str = "Extends";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TypeMarkers {
pub root: u32,
pub extends: Option<u32>,
}
impl TypeMarkers {
pub fn ensure<L: NamedTypeLinks + ?Sized>(links: &mut L) -> Result<Self> {
Ok(Self {
root: ensure_point(links, ROOT_TYPE_NAME)?,
extends: Some(ensure_point(links, EXTENDS_NAME)?),
})
}
}
pub fn ensure_point<L: NamedTypeLinks + ?Sized>(links: &mut L, name: &str) -> Result<u32> {
if let Some(known) = links.get_by_name(name)? {
if known != 0 && links.exists(known) {
return Ok(known);
}
}
let id = links.try_create(0, 0)?;
links.update(id, id, id)?;
links.set_name(id, name)?;
Ok(id)
}
pub trait Types: NamedTypeLinks {
fn root(&self) -> u32;
fn extends(&self) -> Option<u32>;
fn type_of(&mut self, link: u32) -> Option<u32> {
stored(self, link).map(|link| link.source)
}
fn is_type(&mut self, link: u32) -> bool {
stored(self, link).is_some_and(|stored| stored.target == link)
}
fn parents_of(&mut self, type_id: u32) -> Vec<u32> {
let mut parents = Vec::new();
let Some(link) = stored(self, type_id) else {
return parents;
};
if link.source != type_id && link.source != 0 {
parents.push(link.source);
}
add_extended_types(self, type_id, &mut parents);
parents
}
fn ancestors_of(&mut self, type_id: u32) -> Vec<u32> {
let mut ancestors = vec![type_id];
let mut seen = HashSet::from([type_id]);
let mut i = 0;
while i < ancestors.len() {
for parent in self.parents_of(ancestors[i]) {
if seen.insert(parent) {
ancestors.push(parent);
}
}
i += 1;
}
ancestors
}
fn is_extension(&mut self, link: u32) -> bool {
match self.extends() {
Some(extends) if link != 0 => self.search(extends, link).is_some(),
_ => false,
}
}
fn is_of(&mut self, link: u32, type_id: u32) -> bool {
match stored(self, link) {
Some(link) => self.is_of_link(&link, type_id),
None => false,
}
}
fn is_of_link(&mut self, link: &Link, type_id: u32) -> bool {
let mut starts = Vec::new();
match self.extends() {
Some(extends) if self.is_extension(link.index) => starts.push(extends),
_ => {
starts.push(link.source);
if link.index != 0 && self.exists(link.index) {
add_extended_types(self, link.index, &mut starts);
}
}
}
let mut stack: Vec<u32> = starts.into_iter().rev().collect();
let mut seen = HashSet::new();
while let Some(at) = stack.pop() {
if at == type_id {
return true;
}
if at == 0 || !seen.insert(at) {
continue;
}
stack.extend(self.parents_of(at));
}
false
}
fn is_a(&mut self, link: u32, type_id: u32) -> bool {
stored(self, link).is_some() && (link == type_id || self.is_of(link, type_id))
}
fn is_a_link(&mut self, link: &Link, type_id: u32) -> bool {
link.index == type_id || self.is_of_link(link, type_id)
}
fn define(&mut self, parent: u32, more: &[u32]) -> Result<u32> {
if !more.is_empty() && self.extends().is_none() {
bail!("Multiple parents need an Extends marker.");
}
ensure_exists(self, parent)?;
for &other in more {
ensure_exists(self, other)?;
}
let id = self.try_create(0, 0)?;
self.update(id, parent, id)?;
for &other in more {
self.extend(id, other)?;
}
Ok(id)
}
fn define_root(&mut self) -> Result<u32> {
let root = self.root();
self.define(root, &[])
}
fn extend(&mut self, type_id: u32, parent: u32) -> Result<u32> {
let Some(extends) = self.extends() else {
bail!("Multiple parents need an Extends marker.");
};
ensure_exists(self, type_id)?;
let parent_link = ensure_exists(self, parent)?;
if parent_link.source == type_id && parent_link.target == parent {
bail!(
"Type {parent} is a direct sub-type of {type_id}: the pair ({type_id} {parent}) would be its definition."
);
}
let pair = self.try_get_or_create(type_id, parent)?;
self.try_get_or_create(extends, pair)?;
Ok(pair)
}
fn instance(&mut self, type_id: u32) -> Result<u32> {
ensure_exists(self, type_id)?;
let id = self.try_create(0, 0)?;
self.update(id, type_id, id)?;
Ok(id)
}
fn instance_with_target(&mut self, type_id: u32, target: u32) -> Result<u32> {
ensure_exists(self, type_id)?;
ensure_exists(self, target)?;
self.try_get_or_create(type_id, target)
}
}
fn stored<L: NamedTypeLinks + ?Sized>(links: &mut L, link: u32) -> Option<Link> {
if link == 0 {
return None;
}
links.get_link(link)
}
fn ensure_exists<L: NamedTypeLinks + ?Sized>(links: &mut L, link: u32) -> Result<Link> {
match stored(links, link) {
Some(stored) => Ok(stored),
None => bail!("Link {link} does not exist."),
}
}
fn add_extended_types<T: Types + ?Sized>(types: &mut T, type_id: u32, parents: &mut Vec<u32>) {
if types.extends().is_none() || type_id == 0 {
return;
}
for pair in types.query_links(None, Some(type_id), None) {
if pair.index != type_id && pair.target != 0 && types.is_extension(pair.index) {
parents.push(pair.target);
}
}
}
pub struct TypesDecorator<L: NamedTypeLinks> {
links: L,
markers: TypeMarkers,
}
impl<L: NamedTypeLinks> TypesDecorator<L> {
pub fn new(links: L, root: u32, extends: Option<u32>) -> Self {
Self::with_markers(links, TypeMarkers { root, extends })
}
pub fn with_markers(links: L, markers: TypeMarkers) -> Self {
Self { links, markers }
}
pub fn with_named_markers(mut links: L) -> Result<Self> {
let markers = TypeMarkers::ensure(&mut links)?;
Ok(Self::with_markers(links, markers))
}
pub fn markers(&self) -> TypeMarkers {
self.markers
}
pub fn links(&self) -> &L {
&self.links
}
pub fn links_mut(&mut self) -> &mut L {
&mut self.links
}
pub fn into_inner(self) -> L {
self.links
}
}
impl<L: NamedTypeLinks> Types for TypesDecorator<L> {
fn root(&self) -> u32 {
self.markers.root
}
fn extends(&self) -> Option<u32> {
self.markers.extends
}
}
impl<L: NamedTypeLinks> NamedTypeLinks for TypesDecorator<L> {
fn create(&mut self, source: u32, target: u32) -> u32 {
self.links.create(source, target)
}
fn ensure_created(&mut self, id: u32) -> u32 {
self.links.ensure_created(id)
}
fn try_ensure_created(&mut self, id: u32) -> Result<u32> {
self.links.try_ensure_created(id)
}
fn get_link(&mut self, id: u32) -> Option<Link> {
self.links.get_link(id)
}
fn exists(&mut self, id: u32) -> bool {
self.links.exists(id)
}
fn try_create(&mut self, source: u32, target: u32) -> Result<u32> {
self.links.try_create(source, target)
}
fn try_get_or_create(&mut self, source: u32, target: u32) -> Result<u32> {
self.links.try_get_or_create(source, target)
}
fn reference_exists(&mut self, id: u32) -> bool {
self.links.reference_exists(id)
}
fn next_created_addresses(&mut self, count: usize) -> Result<Vec<u32>> {
self.links.next_created_addresses(count)
}
fn update_observed(
&mut self,
id: u32,
source: u32,
target: u32,
observer: ChangeObserver<'_>,
) -> Result<Link> {
self.links.update_observed(id, source, target, observer)
}
fn delete_observed(&mut self, id: u32, observer: ChangeObserver<'_>) -> Result<Link> {
self.links.delete_observed(id, observer)
}
fn all_links(&mut self) -> Vec<Link> {
self.links.all_links()
}
fn query_links(
&mut self,
index: Option<u32>,
source: Option<u32>,
target: Option<u32>,
) -> Vec<Link> {
self.links.query_links(index, source, target)
}
fn search(&mut self, source: u32, target: u32) -> Option<u32> {
self.links.search(source, target)
}
fn get_or_create(&mut self, source: u32, target: u32) -> u32 {
self.links.get_or_create(source, target)
}
fn get_name(&mut self, id: u32) -> Result<Option<String>> {
self.links.get_name(id)
}
fn set_name(&mut self, id: u32, name: &str) -> Result<u32> {
self.links.set_name(id, name)
}
fn get_by_name(&mut self, name: &str) -> Result<Option<u32>> {
self.links.get_by_name(name)
}
fn remove_name(&mut self, id: u32) -> Result<()> {
self.links.remove_name(id)
}
fn save(&mut self) -> Result<()> {
self.links.save()
}
fn get_or_create_named(&mut self, name: &str) -> Result<u32> {
self.links.get_or_create_named(name)
}
}