use super::{Attribute, Comment, Identifier, Span, WithAttributes, WithDocumentation, WithIdentifier, WithSpan};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EnumValueId(pub(super) u32);
impl EnumValueId {
pub const MIN: EnumValueId = EnumValueId(0);
pub const MAX: EnumValueId = EnumValueId(u32::MAX);
}
impl std::ops::Index<EnumValueId> for Enum {
type Output = EnumValue;
fn index(&self, index: EnumValueId) -> &Self::Output {
&self.values[index.0 as usize]
}
}
#[derive(Debug, Clone)]
pub struct Enum {
pub(crate) name: Identifier,
pub values: Vec<EnumValue>,
pub attributes: Vec<Attribute>,
pub(crate) documentation: Option<Comment>,
pub span: Span,
pub inner_span: Span,
}
impl Enum {
pub fn iter_values(&self) -> impl ExactSizeIterator<Item = (EnumValueId, &EnumValue)> {
self.values
.iter()
.enumerate()
.map(|(idx, field)| (EnumValueId(idx as u32), field))
}
}
impl WithIdentifier for Enum {
fn identifier(&self) -> &Identifier {
&self.name
}
}
impl WithSpan for Enum {
fn span(&self) -> Span {
self.span
}
}
impl WithAttributes for Enum {
fn attributes(&self) -> &[Attribute] {
&self.attributes
}
}
impl WithDocumentation for Enum {
fn documentation(&self) -> Option<&str> {
self.documentation.as_ref().map(|doc| doc.text.as_str())
}
}
#[derive(Debug, Clone)]
pub struct EnumValue {
pub name: Identifier,
pub attributes: Vec<Attribute>,
pub(crate) documentation: Option<Comment>,
pub span: Span,
}
impl WithIdentifier for EnumValue {
fn identifier(&self) -> &Identifier {
&self.name
}
}
impl WithAttributes for EnumValue {
fn attributes(&self) -> &[Attribute] {
&self.attributes
}
}
impl WithSpan for EnumValue {
fn span(&self) -> Span {
self.span
}
}
impl WithDocumentation for EnumValue {
fn documentation(&self) -> Option<&str> {
self.documentation.as_ref().map(|doc| doc.text.as_str())
}
}