leo_ast/functions/variant.rs
1// Copyright (C) 2019-2025 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
19/// Functions are always one of six variants.
20/// A transition function is permitted the ability to manipulate records.
21/// An asynchronous transition function is a transition function that calls an asynchronous function.
22/// A regular function is not permitted to manipulate records.
23/// An asynchronous function contains on-chain operations.
24/// An inline function is directly copied at the call site.
25#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
26pub enum Variant {
27 #[default]
28 Inline,
29 Function,
30 Transition,
31 AsyncTransition,
32 AsyncFunction,
33 /// `script` can only appear in test files, and is used for
34 /// tests which are interpreted rather than run on snarkvm using a Ledger and VM.
35 Script,
36}
37
38impl Variant {
39 /// Returns true if the variant is async.
40 pub fn is_async(self) -> bool {
41 matches!(self, Variant::AsyncFunction | Variant::AsyncTransition)
42 }
43
44 /// Returns true if the variant is a transition.
45 pub fn is_transition(self) -> bool {
46 matches!(self, Variant::Transition | Variant::AsyncTransition)
47 }
48
49 /// Returns true if the variant is a function.
50 pub fn is_function(self) -> bool {
51 matches!(self, Variant::AsyncFunction | Variant::Function)
52 }
53
54 /// Is this a `script`?
55 pub fn is_script(self) -> bool {
56 matches!(self, Variant::Script)
57 }
58
59 /// Returns true if the variant is an async function.
60 pub fn is_async_function(self) -> bool {
61 matches!(self, Variant::AsyncFunction)
62 }
63}