mod.rs
 1  // Copyright (c) 2025-2026 ACDC Network
 2  // This file is part of the alphavm library.
 3  //
 4  // Alpha Chain | Delta Chain Protocol
 5  // International Monetary Graphite.
 6  //
 7  // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com).
 8  // They built world-class ZK infrastructure. We installed the EASY button.
 9  // Their cryptography: elegant. Our modifications: bureaucracy-compatible.
10  // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours.
11  //
12  // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0
13  // All modifications and new work: CC0 1.0 Universal Public Domain Dedication.
14  // No rights reserved. No permission required. No warranty. No refunds.
15  //
16  // https://creativecommons.org/publicdomain/zero/1.0/
17  // SPDX-License-Identifier: CC0-1.0
18  
19  mod bytes;
20  mod parse;
21  mod serialize;
22  
23  use crate::{EntryType, Identifier, Locator, PlaintextType};
24  use alphavm_console_network::prelude::*;
25  
26  use enum_index::EnumIndex;
27  
28  pub type Variant = u8;
29  
30  #[derive(Clone, PartialEq, Eq, Hash, EnumIndex)]
31  pub enum ValueType<N: Network> {
32      /// A constant type.
33      Constant(PlaintextType<N>),
34      /// A publicly-visible type.
35      Public(PlaintextType<N>),
36      /// A private type decrypted with the account owner's address.
37      Private(PlaintextType<N>),
38      /// A record type inherits its visibility from the record definition.
39      Record(Identifier<N>),
40      /// An external record type inherits its visibility from its record definition.
41      ExternalRecord(Locator<N>),
42      /// A publicly-visible future.
43      Future(Locator<N>),
44  }
45  
46  impl<N: Network> ValueType<N> {
47      /// Returns the variant of the value type.
48      pub const fn variant(&self) -> Variant {
49          match self {
50              ValueType::Constant(..) => 0,
51              ValueType::Public(..) => 1,
52              ValueType::Private(..) => 2,
53              ValueType::Record(..) => 3,
54              ValueType::ExternalRecord(..) => 4,
55              ValueType::Future(..) => 5,
56          }
57      }
58  }
59  
60  impl<N: Network> From<EntryType<N>> for ValueType<N> {
61      fn from(entry: EntryType<N>) -> Self {
62          match entry {
63              EntryType::Constant(plaintext) => ValueType::Constant(plaintext),
64              EntryType::Public(plaintext) => ValueType::Public(plaintext),
65              EntryType::Private(private) => ValueType::Private(private),
66          }
67      }
68  }
69  
70  impl<N: Network> ValueType<N> {
71      /// Returns `true` if the value type contains a string type.
72      /// Record, external record, and future types are checked elsewhere.
73      pub fn contains_string_type(&self) -> bool {
74          use ValueType::*;
75          matches!(
76              self,
77              Constant(plaintext) | Public(plaintext) | Private(plaintext) if plaintext.contains_string_type()
78          )
79      }
80  
81      /// Returns `true` if the value type is an array and the size exceeds the given maximum.
82      pub fn exceeds_max_array_size(&self, max_array_size: u32) -> bool {
83          use ValueType::*;
84          matches!(
85              self,
86              Constant(plaintext) | Public(plaintext) | Private(plaintext) if plaintext.exceeds_max_array_size(max_array_size)
87          )
88      }
89  }