use schema_ast::ast::{IndentationType, NewlineType};
use crate::{ast, ast::WithDocumentation, types, walkers::Walker};
pub type EnumWalker<'db> = Walker<'db, ast::EnumId>;
pub type EnumValueWalker<'db> = Walker<'db, (ast::EnumId, usize)>;
impl<'db> EnumWalker<'db> {
fn attributes(self) -> &'db types::EnumAttributes {
&self.db.types.enum_attributes[&self.id]
}
pub fn name(self) -> &'db str {
&self.ast_enum().name.name
}
pub fn ast_enum(self) -> &'db ast::Enum {
&self.db.ast()[self.id]
}
pub fn database_name(self) -> &'db str {
self.mapped_name().unwrap_or_else(|| self.name())
}
pub fn mapped_name(self) -> Option<&'db str> {
self.attributes().mapped_name.map(|id| &self.db[id])
}
pub fn values(self) -> impl ExactSizeIterator<Item = EnumValueWalker<'db>> {
(0..self.ast_enum().values.len()).map(move |idx| self.walk((self.id, idx)))
}
pub fn indentation(self) -> IndentationType {
IndentationType::default()
}
pub fn newline(self) -> NewlineType {
NewlineType::Unix
}
pub fn schema(self) -> Option<(&'db str, ast::Span)> {
self.attributes().schema.map(|(id, span)| (&self.db[id], span))
}
}
impl<'db> EnumValueWalker<'db> {
fn r#enum(self) -> EnumWalker<'db> {
self.walk(self.id.0)
}
pub fn documentation(self) -> Option<&'db str> {
self.r#enum().ast_enum().values[self.id.1].documentation()
}
pub fn name(self) -> &'db str {
&self.r#enum().ast_enum().values[self.id.1].name.name
}
pub fn database_name(self) -> &'db str {
self.mapped_name().unwrap_or_else(|| self.name())
}
pub fn mapped_name(self) -> Option<&'db str> {
self.db.types.enum_attributes[&self.id.0]
.mapped_values
.get(&(self.id.1 as u32))
.map(|id| &self.db[*id])
}
}