use miette::Diagnostic;
use std::fmt::Debug;
use std::fmt::Display;
use thiserror::Error;
#[derive(Debug, Diagnostic, Error, PartialEq, Eq)]
pub enum TcError<K: Debug + Display> {
#[error("expected all transitive edges to exist, but `{}` -> `{}` and `{}` -> `{}` exists, while `{}` -> `{}` does not", .0.child, .0.parent, .0.parent, .0.grandparent, .0.child, .0.grandparent)]
MissingTcEdge(MissingTcEdge<K>),
#[error("input graph has a cycle containing vertex `{}`", .0.vertex_with_loop)]
HasCycle(HasCycle<K>),
}
impl<K: Debug + Display> TcError<K> {
pub(crate) fn missing_tc_edge(child: K, parent: K, grandparent: K) -> Self {
Self::MissingTcEdge(MissingTcEdge {
child,
parent,
grandparent,
})
}
pub(crate) fn has_cycle(vertex_with_loop: K) -> Self {
Self::HasCycle(HasCycle { vertex_with_loop })
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct MissingTcEdge<K> {
child: K,
parent: K,
grandparent: K,
}
#[derive(Debug, PartialEq, Eq)]
pub struct HasCycle<K> {
vertex_with_loop: K,
}
impl<K> HasCycle<K> {
pub fn vertex_with_loop(&self) -> &K {
&self.vertex_with_loop
}
}
pub type Result<T, K> = std::result::Result<T, TcError<K>>;