use crate::{CollectionData, CollectionId, IndexData, IndexField, IndexId, IndexType, MongoSchema};
#[derive(Clone, Copy)]
pub struct CollectionWalker<'schema> {
pub(crate) id: CollectionId,
pub(crate) schema: &'schema MongoSchema,
}
impl<'schema> CollectionWalker<'schema> {
fn get(self) -> &'schema CollectionData {
&self.schema[self.id]
}
pub fn id(self) -> CollectionId {
self.id
}
pub fn name(self) -> &'schema str {
&self.get().name
}
pub fn indexes(self) -> impl ExactSizeIterator<Item = IndexWalker<'schema>> + 'schema {
let create_walker = move |id: &IndexId| IndexWalker {
id: *id,
schema: self.schema,
};
match self.schema.collection_indexes.get(&self.id) {
Some(indexes) => indexes.iter().map(create_walker),
None => [].iter().map(create_walker),
}
}
pub fn has_schema(self) -> bool {
self.get().has_schema
}
pub fn is_capped(self) -> bool {
self.get().is_capped
}
}
#[derive(Clone, Copy)]
pub struct IndexWalker<'schema> {
pub(crate) id: IndexId,
pub(crate) schema: &'schema MongoSchema,
}
impl<'schema> IndexWalker<'schema> {
fn get(self) -> &'schema IndexData {
&self.schema[self.id]
}
pub fn id(self) -> IndexId {
self.id
}
pub fn collection(self) -> CollectionWalker<'schema> {
CollectionWalker {
id: self.get().collection_id,
schema: self.schema,
}
}
pub fn r#type(self) -> IndexType {
self.get().r#type
}
pub fn name(self) -> &'schema str {
&self.get().name
}
pub fn is_fulltext(self) -> bool {
matches!(self.r#type(), IndexType::Fulltext)
}
pub fn is_unique(self) -> bool {
matches!(self.r#type(), IndexType::Unique)
}
pub fn fields(self) -> impl ExactSizeIterator<Item = &'schema IndexField> + 'schema {
self.get().fields.iter()
}
}