use ploidy_core::ir::{
ContainerView, EnumVariant, EnumView, ParameterView, QueryParameter, StructFieldView,
TaggedFieldView, TypeView, UntaggedFieldView,
};
pub(crate) trait EnumViewExt {
fn representable(&self) -> bool;
}
impl EnumViewExt for EnumView<'_, '_> {
fn representable(&self) -> bool {
self.variants().iter().all(|variant| match variant {
EnumVariant::String(s) => s.chars().any(unicode_ident::is_xid_continue),
_ => false,
})
}
}
pub(crate) trait FieldViewExt<'graph, 'a> {
fn inner(&self) -> TypeView<'graph, 'a>;
}
fn peel<'graph, 'a>(mut ty: TypeView<'graph, 'a>) -> TypeView<'graph, 'a> {
while let Some(ContainerView::Optional(inner)) = ty.as_container() {
ty = inner.ty();
}
ty
}
impl<'view, 'graph, 'a> FieldViewExt<'graph, 'a> for StructFieldView<'view, 'graph, 'a> {
fn inner(&self) -> TypeView<'graph, 'a> {
peel(self.ty())
}
}
impl<'view, 'graph, 'a> FieldViewExt<'graph, 'a> for TaggedFieldView<'view, 'graph, 'a> {
fn inner(&self) -> TypeView<'graph, 'a> {
peel(self.ty())
}
}
impl<'view, 'graph, 'a> FieldViewExt<'graph, 'a> for UntaggedFieldView<'view, 'graph, 'a> {
fn inner(&self) -> TypeView<'graph, 'a> {
peel(self.ty())
}
}
pub(crate) trait ParameterViewExt {
fn optional(&self) -> bool;
}
impl<'view, 'graph, 'a> ParameterViewExt for ParameterView<'view, 'graph, 'a, QueryParameter> {
fn optional(&self) -> bool {
!self.required() && !matches!(self.ty().as_container(), Some(ContainerView::Optional(_)))
}
}