mod composite_type;
mod r#enum;
mod field;
mod index;
mod model;
mod relation;
mod relation_field;
mod scalar_field;
pub use crate::types::RelationFieldId;
pub use composite_type::*;
pub use field::*;
pub use index::*;
pub use model::*;
pub use r#enum::*;
pub use relation::*;
pub use relation_field::*;
pub use scalar_field::*;
#[derive(Clone, Copy)]
pub struct Walker<'db, I> {
pub db: &'db crate::ParserDatabase,
pub id: I,
}
impl<'db, I> Walker<'db, I> {
pub fn walk<J>(self, other: J) -> Walker<'db, J> {
self.db.walk(other)
}
}
impl<'db, I> PartialEq for Walker<'db, I>
where
I: PartialEq,
{
#[allow(unconditional_recursion)]
fn eq(&self, other: &Self) -> bool {
self.id.eq(&other.id)
}
}
impl crate::ParserDatabase {
pub fn find_enum<'db>(&'db self, name: &str) -> Option<EnumWalker<'db>> {
self.interner
.lookup(name)
.and_then(|name_id| self.names.tops.get(&name_id))
.and_then(|top_id| top_id.as_enum_id())
.map(|enum_id| self.walk(enum_id))
}
pub fn find_model<'db>(&'db self, name: &str) -> Option<ModelWalker<'db>> {
self.interner
.lookup(name)
.and_then(|name_id| self.names.tops.get(&name_id))
.and_then(|top_id| top_id.as_model_id())
.map(|model_id| self.walk(model_id))
}
pub fn walk<I>(&self, id: I) -> Walker<'_, I> {
Walker { db: self, id }
}
pub fn walk_enums(&self) -> impl Iterator<Item = EnumWalker<'_>> {
self.ast()
.iter_tops()
.filter_map(|(top_id, _)| top_id.as_enum_id())
.map(move |enum_id| Walker { db: self, id: enum_id })
}
pub fn walk_models(&self) -> impl Iterator<Item = ModelWalker<'_>> + '_ {
self.ast()
.iter_tops()
.filter_map(|(top_id, _)| top_id.as_model_id())
.map(move |model_id| self.walk(model_id))
.filter(|m| !m.ast_model().is_view())
}
pub fn walk_views(&self) -> impl Iterator<Item = ModelWalker<'_>> + '_ {
self.ast()
.iter_tops()
.filter_map(|(top_id, _)| top_id.as_model_id())
.map(move |model_id| self.walk(model_id))
.filter(|m| m.ast_model().is_view())
}
pub fn walk_composite_types(&self) -> impl Iterator<Item = CompositeTypeWalker<'_>> + '_ {
self.ast()
.iter_tops()
.filter_map(|(top_id, _)| top_id.as_composite_type_id())
.map(|id| self.walk(id))
}
pub fn walk_scalar_field_defaults_with_unknown_function(&self) -> impl Iterator<Item = DefaultValueWalker<'_>> {
self.types
.unknown_function_defaults
.iter()
.map(|id| DefaultValueWalker {
field_id: *id,
db: self,
default: self.types[*id].default.as_ref().unwrap(),
})
}
pub fn walk_relations(&self) -> impl ExactSizeIterator<Item = RelationWalker<'_>> + Clone + '_ {
self.relations.iter().map(move |relation_id| Walker {
db: self,
id: relation_id,
})
}
#[track_caller]
pub fn walk_complete_inline_relations(&self) -> impl Iterator<Item = CompleteInlineRelationWalker<'_>> + '_ {
self.relations
.iter_relations()
.filter(|(relation, _)| !relation.is_implicit_many_to_many())
.filter_map(move |(relation, _)| {
relation
.as_complete_fields()
.map(|(field_a, field_b)| CompleteInlineRelationWalker {
side_a: field_a,
side_b: field_b,
db: self,
})
})
}
}