use either::Either;
use crate::{
Column, ColumnArity, ColumnType, ColumnTypeFamily, EnumWalker, ViewColumnId, ViewDefaultValueId,
ViewDefaultValueWalker, ViewId, ViewWalker, Walker,
};
use super::ColumnWalker;
pub type ViewColumnWalker<'a> = Walker<'a, ViewColumnId>;
impl<'a> ViewColumnWalker<'a> {
fn get(self) -> &'a (ViewId, Column) {
&self.schema.view_columns[self.id.0 as usize]
}
pub fn coarsen(self) -> ColumnWalker<'a> {
self.walk(Either::Right(self.id))
}
pub fn name(self) -> &'a str {
self.coarsen().name()
}
pub fn arity(self) -> ColumnArity {
self.coarsen().arity()
}
pub fn column_has_enum_default_value(self, enum_name: &str, value: &str) -> bool {
self.coarsen().column_has_enum_default_value(enum_name, value)
}
pub fn column_type_is_enum(self, enum_name: &str) -> bool {
self.coarsen().column_type_is_enum(enum_name)
}
pub fn column_type_family(self) -> &'a ColumnTypeFamily {
self.coarsen().column_type_family()
}
pub fn column_type_family_as_enum(self) -> Option<EnumWalker<'a>> {
self.coarsen().column_type_family_as_enum()
}
pub fn default(self) -> Option<ViewDefaultValueWalker<'a>> {
self.schema
.view_default_values
.binary_search_by_key(&self.id, |(id, _)| *id)
.ok()
.map(|id| self.walk(ViewDefaultValueId(id as u32)))
}
pub fn column_type(self) -> &'a ColumnType {
self.coarsen().column_type()
}
pub fn column_native_type<T: std::any::Any + 'static>(self) -> Option<&'a T> {
self.coarsen().column_native_type()
}
pub fn is_autoincrement(self) -> bool {
self.coarsen().is_autoincrement()
}
pub fn is_same_column(self, other: ViewColumnWalker<'_>) -> bool {
self.name() == other.name()
&& self.view().name() == other.view().name()
&& other.view().namespace() == self.view().namespace()
}
pub fn view(self) -> ViewWalker<'a> {
self.walk(self.get().0)
}
}