use crate::ast::{Comment, Field, FieldId, Identifier, SchemaAst, Span};
use super::{WithDocumentation, WithIdentifier};
#[derive(Debug, Clone)]
pub struct CompositeType {
pub(crate) name: Identifier,
pub(crate) fields: Vec<Field>,
pub(crate) documentation: Option<Comment>,
pub span: Span,
pub inner_span: Span,
}
impl CompositeType {
pub fn is_commented_out(&self) -> bool {
false
}
pub fn iter_fields(&self) -> impl ExactSizeIterator<Item = (FieldId, &Field)> + Clone {
self.fields
.iter()
.enumerate()
.map(|(idx, field)| (FieldId(idx as u32), field))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CompositeTypeId(pub(super) u32);
impl std::ops::Index<CompositeTypeId> for SchemaAst {
type Output = CompositeType;
fn index(&self, index: CompositeTypeId) -> &Self::Output {
self.tops[index.0 as usize].as_composite_type().unwrap()
}
}
impl std::ops::Index<FieldId> for CompositeType {
type Output = Field;
fn index(&self, index: FieldId) -> &Self::Output {
&self.fields[index.0 as usize]
}
}
impl WithDocumentation for CompositeType {
fn documentation(&self) -> Option<&str> {
self.documentation.as_ref().map(|doc| doc.text.as_str())
}
}
impl WithIdentifier for CompositeType {
fn identifier(&self) -> &Identifier {
&self.name
}
}