Skip to main content

leo_ast/functions/
variant.rs

1// Copyright (C) 2019-2026 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use serde::{Deserialize, Serialize};
18
19use std::fmt;
20
21/// Functions are always one of six variants.
22/// A transition function is permitted the ability to manipulate records.
23/// An asynchronous transition function is a transition function that calls an asynchronous function.
24/// A regular function is not permitted to manipulate records.
25/// An asynchronous function contains on-chain operations.
26/// An inline function is directly copied at the call site.
27#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
28pub enum Variant {
29    #[default]
30    Fn,
31    FinalFn,
32    EntryPoint,
33    Finalize,
34}
35
36impl Variant {
37    /// Returns true if the variant is an entry point.
38    pub fn is_entry(self) -> bool {
39        matches!(self, Variant::EntryPoint)
40    }
41
42    pub fn is_finalize(self) -> bool {
43        matches!(self, Variant::Finalize)
44    }
45
46    pub fn is_onchain(self) -> bool {
47        matches!(self, Variant::Finalize | Variant::FinalFn)
48    }
49}
50
51impl fmt::Display for Variant {
52    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53        match self {
54            Self::FinalFn => write!(f, "final fn"),
55            Self::Fn => write!(f, "fn"),
56            Self::EntryPoint => write!(f, "entry"),
57            Self::Finalize => write!(f, "finalize"),
58        }
59    }
60}