Skip to main content

wright/ast/decl/
mod.rs

1//! Abstract syntax trees related to declarations (items) in source code.
2
3use super::{AstNode, DocComment};
4use crate::source_tracking::fragment::Fragment;
5
6pub mod constant;
7pub mod enumeration;
8pub mod function;
9pub mod implementation;
10pub mod import;
11pub mod module;
12pub mod record;
13pub mod type_alias;
14pub mod union;
15
16/// A declaration, such as a function, record, or import, along with its documentation.
17#[derive(Debug, Clone, PartialEq)]
18pub struct Item {
19    /// Outer doc comments (`///` and `/** */`) documenting this item.
20    pub docs: Vec<DocComment>,
21
22    /// The visibility of this item.
23    pub visibility: Visibility,
24
25    /// The declaration itself.
26    pub kind: ItemKind,
27}
28
29impl Item {
30    /// Is this item public?
31    pub fn is_public(&self) -> bool {
32        matches!(self.visibility, Visibility::Public(_))
33    }
34}
35
36impl AstNode for Item {
37    fn matching_source(&self) -> &Fragment {
38        self.kind.matching_source()
39    }
40}
41
42/// The visibility of an [Item] or record field.
43#[derive(Debug, Clone, PartialEq)]
44pub enum Visibility {
45    /// Not visible outside of the enclosing module (the default).
46    Private,
47    /// Marked with `pub`. The fragment contains the `pub` keyword.
48    Public(Fragment),
49}
50
51/// The different kinds of [Item]s.
52#[derive(Debug, Clone, PartialEq)]
53#[allow(missing_docs)]
54pub enum ItemKind {
55    Import(import::ImportDecl),
56    Const(constant::ConstDecl),
57    TypeAlias(type_alias::TypeAlias),
58    Function(function::FunctionDecl),
59    Record(record::RecordDecl),
60    Enum(enumeration::EnumDecl),
61    Union(union::UnionDecl),
62    Impl(implementation::ImplDecl),
63    Module(module::ModuleDecl),
64}
65
66impl ItemKind {
67    /// Get the source of the whole declaration (including any `pub` keyword, but not doc comments).
68    pub fn matching_source(&self) -> &Fragment {
69        match self {
70            ItemKind::Import(decl) => &decl.matching_source,
71            ItemKind::Const(decl) => &decl.matching_source,
72            ItemKind::TypeAlias(decl) => &decl.matching_source,
73            ItemKind::Function(decl) => &decl.matching_source,
74            ItemKind::Record(decl) => &decl.matching_source,
75            ItemKind::Enum(decl) => &decl.matching_source,
76            ItemKind::Union(decl) => &decl.matching_source,
77            ItemKind::Impl(decl) => &decl.matching_source,
78            ItemKind::Module(decl) => &decl.matching_source,
79        }
80    }
81
82    /// Get a short description of this kind of item, such as "function" or "record", for use in diagnostics.
83    pub const fn describe(&self) -> &'static str {
84        match self {
85            ItemKind::Import(_) => "import",
86            ItemKind::Const(_) => "constant",
87            ItemKind::TypeAlias(_) => "type alias",
88            ItemKind::Function(_) => "function",
89            ItemKind::Record(_) => "record",
90            ItemKind::Enum(_) => "enum",
91            ItemKind::Union(_) => "union",
92            ItemKind::Impl(_) => "impl block",
93            ItemKind::Module(_) => "module",
94        }
95    }
96}