/ compiler / ast / src / functions / variant.rs
variant.rs
 1  // Copyright (C) 2019-2025 ADnet Contributors
 2  // This file is part of the ADL library.
 3  
 4  // The ADL 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 ADL 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 ADL library. If not, see <https://www.gnu.org/licenses/>.
16  
17  use serde::{Deserialize, Serialize};
18  
19  use 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)]
28  pub enum Variant {
29      #[default]
30      Inline,
31      Function,
32      Transition,
33      AsyncTransition,
34      AsyncFunction,
35      /// `script` can only appear in test files, and is used for
36      /// tests which are interpreted rather than run on snarkvm using a Ledger and VM.
37      Script,
38  }
39  
40  impl Variant {
41      /// Returns true if the variant is async.
42      pub fn is_async(self) -> bool {
43          matches!(self, Variant::AsyncFunction | Variant::AsyncTransition)
44      }
45  
46      /// Returns true if the variant is a transition.
47      pub fn is_transition(self) -> bool {
48          matches!(self, Variant::Transition | Variant::AsyncTransition)
49      }
50  
51      /// Returns true if the variant is a function.
52      pub fn is_function(self) -> bool {
53          matches!(self, Variant::AsyncFunction | Variant::Function)
54      }
55  
56      /// Is this a `script`?
57      pub fn is_script(self) -> bool {
58          matches!(self, Variant::Script)
59      }
60  
61      /// Returns true if the variant is an async function.
62      pub fn is_async_function(self) -> bool {
63          matches!(self, Variant::AsyncFunction)
64      }
65  }
66  
67  impl fmt::Display for Variant {
68      fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69          match self {
70              Self::Inline => write!(f, "inline"),
71              Self::Function => write!(f, "function"),
72              Self::Transition => write!(f, "transition"),
73              Self::AsyncTransition => write!(f, "async transition"),
74              Self::AsyncFunction => write!(f, "async function"),
75              Self::Script => write!(f, "script"),
76          }
77      }
78  }