Skip to main content

wright/ast/decl/
union.rs

1//! Tagged union declarations, such as `union Shape { circle: Circle | square: Square | none }`.
2
3use crate::ast::{AstNode, identifier::Identifier, ty::Type};
4use crate::source_tracking::fragment::Fragment;
5
6/// A tagged union declaration.
7#[derive(Debug, Clone, PartialEq)]
8pub struct UnionDecl {
9    /// Full matching source of the declaration.
10    pub matching_source: Fragment,
11
12    /// The name of the union.
13    pub name: Identifier,
14
15    /// Generic type parameters, such as the `T` in `union Option<T> { some: T | none }`.
16    pub generics: Vec<Identifier>,
17
18    /// The variants of the union, in order.
19    pub variants: Vec<UnionVariant>,
20}
21
22/// A variant of a [UnionDecl], such as `some: T` or `none`.
23#[derive(Debug, Clone, PartialEq)]
24pub struct UnionVariant {
25    /// Full matching source of the variant.
26    pub matching_source: Fragment,
27
28    /// The name of the variant.
29    pub name: Identifier,
30
31    /// The type of data held by this variant, if any.
32    pub ty: Option<Type>,
33}
34
35impl AstNode for UnionVariant {
36    fn matching_source(&self) -> &Fragment {
37        &self.matching_source
38    }
39}