use super::IntrospectionPair;
use crate::introspection::sanitize_datamodel_names::{EnumVariantName, ModelName};
use psl::{
parser_database::walkers,
schema_ast::ast::{self, WithDocumentation},
};
use sql_schema_describer as sql;
use std::borrow::Cow;
pub(crate) type EnumPair<'a> = IntrospectionPair<'a, Option<walkers::EnumWalker<'a>>, sql::EnumWalker<'a>>;
pub(crate) type EnumVariantPair<'a> =
IntrospectionPair<'a, Option<walkers::EnumValueWalker<'a>>, sql::EnumVariantWalker<'a>>;
impl<'a> EnumPair<'a> {
pub(crate) fn documentation(self) -> Option<&'a str> {
self.previous.and_then(|enm| enm.ast_enum().documentation())
}
pub(crate) fn mapped_name(self) -> Option<&'a str> {
self.context.enum_prisma_name(self.next.id).mapped_name()
}
pub(crate) fn name(self) -> Cow<'a, str> {
self.context.enum_prisma_name(self.next.id).prisma_name()
}
pub(crate) fn name_from_psl(self) -> bool {
matches!(
self.context.enum_prisma_name(self.next.id),
ModelName::FromPsl {
mapped_name: Some(_),
..
}
)
}
pub(crate) fn namespace(self) -> Option<&'a str> {
self.context.uses_namespaces().then(|| self.next.namespace()).flatten()
}
pub(crate) fn previous_position(self) -> Option<ast::EnumId> {
self.previous.map(|e| e.id)
}
pub(crate) fn uses_duplicate_name(self) -> bool {
self.previous.is_none() && !self.context.name_is_unique(self.next.name())
}
pub(crate) fn description(self) -> Option<&'a str> {
self.next.description()
}
pub(crate) fn adds_a_description(self) -> bool {
self.previous.is_none() && self.description().is_some()
}
pub(crate) fn variants(self) -> impl ExactSizeIterator<Item = EnumVariantPair<'a>> + 'a {
self.next.variants().map(move |next| {
let variant_name = self.context.enum_variant_name(next.id);
let prisma_name = variant_name.prisma_name();
let previous = self.previous.and_then(|prev| {
prev.values()
.find(|val| val.database_name() == variant_name.mapped_name().unwrap_or(&prisma_name))
});
IntrospectionPair::new(self.context, previous, next)
})
}
}
impl<'a> EnumVariantPair<'a> {
pub(crate) fn documentation(self) -> Option<&'a str> {
self.previous.and_then(|variant| variant.documentation())
}
pub(crate) fn mapped_name(self) -> Option<&'a str> {
self.context.enum_variant_name(self.next.id).mapped_name()
}
pub(crate) fn name_from_psl(self) -> bool {
matches!(
self.context.enum_variant_name(self.next.id),
EnumVariantName::FromPsl {
mapped_name: Some(_),
..
}
)
}
pub(crate) fn name(self) -> Cow<'a, str> {
let name = self.context.enum_variant_name(self.next.id).prisma_name();
if name.is_empty() {
Cow::Borrowed(self.next.name())
} else {
name
}
}
}