1use derive_more::From;
14use ref_cast::RefCast;
15use thiserror::Error;
16
17use crate::one::{Graph, path::Path};
18
19pub trait VDblGraph {
25 type V: Eq + Clone;
27
28 type E: Eq + Clone;
30
31 type ProE: Eq + Clone;
33
34 type Sq: Eq + Clone;
36
37 fn has_vertex(&self, v: &Self::V) -> bool;
39
40 fn has_edge(&self, e: &Self::E) -> bool;
42
43 fn has_proedge(&self, p: &Self::ProE) -> bool;
45
46 fn has_square(&self, sq: &Self::Sq) -> bool;
48
49 fn dom(&self, e: &Self::E) -> Self::V;
51
52 fn cod(&self, e: &Self::E) -> Self::V;
54
55 fn src(&self, p: &Self::ProE) -> Self::V;
57
58 fn tgt(&self, p: &Self::ProE) -> Self::V;
60
61 fn square_dom(&self, sq: &Self::Sq) -> Path<Self::V, Self::ProE>;
63
64 fn square_cod(&self, sq: &Self::Sq) -> Self::ProE;
66
67 fn square_src(&self, sq: &Self::Sq) -> Self::E;
69
70 fn square_tgt(&self, sq: &Self::Sq) -> Self::E;
72
73 fn arity(&self, sq: &Self::Sq) -> usize {
77 self.square_dom(sq).len()
78 }
79}
80
81#[derive(From, RefCast)]
85#[repr(transparent)]
86pub struct EdgeGraph<VDG: VDblGraph>(VDG);
87
88impl<VDG: VDblGraph> Graph for EdgeGraph<VDG> {
89 type V = VDG::V;
90 type E = VDG::E;
91
92 fn has_vertex(&self, v: &Self::V) -> bool {
93 self.0.has_vertex(v)
94 }
95 fn has_edge(&self, e: &Self::E) -> bool {
96 self.0.has_edge(e)
97 }
98 fn src(&self, e: &Self::E) -> Self::V {
99 self.0.dom(e)
100 }
101 fn tgt(&self, e: &Self::E) -> Self::V {
102 self.0.cod(e)
103 }
104}
105
106#[derive(From, RefCast)]
110#[repr(transparent)]
111pub struct ProedgeGraph<VDG: VDblGraph>(VDG);
112
113impl<VDG: VDblGraph> Graph for ProedgeGraph<VDG> {
114 type V = VDG::V;
115 type E = VDG::ProE;
116
117 fn has_vertex(&self, v: &Self::V) -> bool {
118 self.0.has_vertex(v)
119 }
120 fn has_edge(&self, e: &Self::E) -> bool {
121 self.0.has_proedge(e)
122 }
123 fn src(&self, e: &Self::E) -> Self::V {
124 self.0.src(e)
125 }
126 fn tgt(&self, e: &Self::E) -> Self::V {
127 self.0.tgt(e)
128 }
129}
130
131#[derive(Debug, Error)]
133pub enum InvalidVDblGraph<E, ProE, Sq> {
134 #[error("Domain of edge `{0}` is not a vertex in the double graph")]
136 Dom(E),
137
138 #[error("Codomain of edge `{0}` is not a vertex in the double graph")]
140 Cod(E),
141
142 #[error("Source of proedge `{0}` is not a vertex in the double graph")]
144 Src(ProE),
145
146 #[error("Target of proedge `{0}` is not a vertex in the double graph")]
148 Tgt(ProE),
149
150 #[error("Domain of square `{0}` is not a proedge in the double graph")]
152 SquareDom(Sq),
153
154 #[error("Codomain of square `{0}` is not a proedge in the double graph")]
156 SquareCod(Sq),
157
158 #[error("Source of square `{0}` is not an edge in the double graph")]
160 SquareSrc(Sq),
161
162 #[error("Target of cell `{0}` is not an edge in the double graph")]
164 SquareTgt(Sq),
165
166 #[error("Square `{0}` has sides with incompatible endpoints")]
168 NotSquare(Sq),
169}