Skip to main content

wright/ast/decl/
enumeration.rs

1//! Enum declarations, such as `enum Color { Red, Green, Blue }`.
2
3use crate::ast::{AstNode, expr::Expr, identifier::Identifier};
4use crate::source_tracking::fragment::Fragment;
5
6/// An enumeration declaration. Enums in wright are a set of named values with no associated data -- see
7/// [UnionDecl](super::union::UnionDecl) for tagged unions.
8#[derive(Debug, Clone, PartialEq)]
9pub struct EnumDecl {
10    /// Full matching source of the declaration.
11    pub matching_source: Fragment,
12
13    /// The name of the enum.
14    pub name: Identifier,
15
16    /// The variants of the enum, in order.
17    pub variants: Vec<EnumVariant>,
18}
19
20/// A variant of an [EnumDecl], such as `Red` or `Red = 1`.
21#[derive(Debug, Clone, PartialEq)]
22pub struct EnumVariant {
23    /// Full matching source of the variant.
24    pub matching_source: Fragment,
25
26    /// The name of the variant.
27    pub name: Identifier,
28
29    /// The explicit discriminant value of the variant, if specified.
30    pub discriminant: Option<Expr>,
31}
32
33impl AstNode for EnumVariant {
34    fn matching_source(&self) -> &Fragment {
35        &self.matching_source
36    }
37}