use either::Either;
use petgraph::graph::NodeIndex;
use crate::ir::{graph::CookedGraph, types::GraphType};
use super::{View, container::ContainerView, inline::InlineTypeView, schema::SchemaTypeView};
#[derive(Debug)]
pub enum TypeView<'graph, 'a> {
Schema(SchemaTypeView<'graph, 'a>),
Inline(InlineTypeView<'graph, 'a>),
}
impl<'graph, 'a> TypeView<'graph, 'a> {
#[inline]
pub(in crate::ir) fn new(cooked: &'graph CookedGraph<'a>, index: NodeIndex<usize>) -> Self {
match cooked.graph[index] {
GraphType::Schema(ty) => Self::Schema(SchemaTypeView::new(cooked, index, ty)),
GraphType::Inline(ty) => Self::Inline(InlineTypeView::new(cooked, index, ty)),
}
}
#[inline]
pub fn into_schema(self) -> Either<Self, SchemaTypeView<'graph, 'a>> {
match self {
Self::Schema(view) => Either::Right(view),
other => Either::Left(other),
}
}
#[inline]
pub fn as_container(&self) -> Option<&ContainerView<'graph, 'a>> {
match self {
Self::Schema(SchemaTypeView::Container(_, view)) => Some(view),
Self::Inline(InlineTypeView::Container(_, view)) => Some(view),
_ => None,
}
}
#[inline]
pub fn dependencies(&self) -> impl Iterator<Item = TypeView<'graph, 'a>> + use<'graph, 'a> {
either!(match self {
Self::Schema(v) => v.dependencies(),
Self::Inline(v) => v.dependencies(),
})
}
}