sway_core/language/parsed/
program.rs

1use strum::EnumString;
2
3use crate::Engines;
4
5use super::ParseModule;
6
7/// A parsed, but not yet type-checked, Sway program.
8///
9/// Includes all modules in the form of a `ParseModule` tree accessed via the `root`.
10#[derive(Debug, Clone)]
11pub struct ParseProgram {
12    pub kind: TreeType,
13    pub root: ParseModule,
14}
15
16/// A Sway program can be either a contract, script, predicate, or a library.
17///
18/// A submodule declared with `mod` can be only a [TreeType::Library].
19#[derive(Copy, Clone, Debug, PartialEq, Eq, EnumString)]
20pub enum TreeType {
21    #[strum(serialize = "predicate")]
22    Predicate,
23    #[strum(serialize = "script")]
24    Script,
25    #[strum(serialize = "contract")]
26    Contract,
27    #[strum(serialize = "library")]
28    Library,
29}
30
31impl TreeType {
32    pub const CFG: &'static [&'static str] = &["contract", "library", "predicate", "script"];
33}
34
35impl std::fmt::Display for TreeType {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        write!(
38            f,
39            "{}",
40            match self {
41                Self::Predicate => "predicate",
42                Self::Script => "script",
43                Self::Contract => "contract",
44                Self::Library => "library",
45            }
46        )
47    }
48}
49
50impl ParseProgram {
51    /// Excludes all test functions from the parse tree.
52    pub(crate) fn exclude_tests(&mut self, engines: &Engines) {
53        self.root.tree.exclude_tests(engines)
54    }
55}