mod.rs
 1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
 2  // This file is part of the alphavm library.
 3  
 4  // Licensed under the Apache License, Version 2.0 (the "License");
 5  // you may not use this file except in compliance with the License.
 6  // You may obtain a copy of the License at:
 7  
 8  // http://www.apache.org/licenses/LICENSE-2.0
 9  
10  // Unless required by applicable law or agreed to in writing, software
11  // distributed under the License is distributed on an "AS IS" BASIS,
12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  // See the License for the specific language governing permissions and
14  // limitations under the License.
15  
16  mod bytes;
17  mod parse;
18  mod serialize;
19  
20  use crate::{EntryType, Identifier, Locator, PlaintextType};
21  use alphavm_console_network::prelude::*;
22  
23  use enum_index::EnumIndex;
24  
25  pub type Variant = u8;
26  
27  #[derive(Clone, PartialEq, Eq, Hash, EnumIndex)]
28  pub enum ValueType<N: Network> {
29      /// A constant type.
30      Constant(PlaintextType<N>),
31      /// A publicly-visible type.
32      Public(PlaintextType<N>),
33      /// A private type decrypted with the account owner's address.
34      Private(PlaintextType<N>),
35      /// A record type inherits its visibility from the record definition.
36      Record(Identifier<N>),
37      /// An external record type inherits its visibility from its record definition.
38      ExternalRecord(Locator<N>),
39      /// A publicly-visible future.
40      Future(Locator<N>),
41  }
42  
43  impl<N: Network> ValueType<N> {
44      /// Returns the variant of the value type.
45      pub const fn variant(&self) -> Variant {
46          match self {
47              ValueType::Constant(..) => 0,
48              ValueType::Public(..) => 1,
49              ValueType::Private(..) => 2,
50              ValueType::Record(..) => 3,
51              ValueType::ExternalRecord(..) => 4,
52              ValueType::Future(..) => 5,
53          }
54      }
55  }
56  
57  impl<N: Network> From<EntryType<N>> for ValueType<N> {
58      fn from(entry: EntryType<N>) -> Self {
59          match entry {
60              EntryType::Constant(plaintext) => ValueType::Constant(plaintext),
61              EntryType::Public(plaintext) => ValueType::Public(plaintext),
62              EntryType::Private(private) => ValueType::Private(private),
63          }
64      }
65  }
66  
67  impl<N: Network> ValueType<N> {
68      /// Returns `true` if the value type contains a string type.
69      /// Record, external record, and future types are checked elsewhere.
70      pub fn contains_string_type(&self) -> bool {
71          use ValueType::*;
72          matches!(
73              self,
74              Constant(plaintext) | Public(plaintext) | Private(plaintext) if plaintext.contains_string_type()
75          )
76      }
77  
78      /// Returns `true` if the value type is an array and the size exceeds the given maximum.
79      pub fn exceeds_max_array_size(&self, max_array_size: u32) -> bool {
80          use ValueType::*;
81          matches!(
82              self,
83              Constant(plaintext) | Public(plaintext) | Private(plaintext) if plaintext.exceeds_max_array_size(max_array_size)
84          )
85      }
86  }